Home > Mobile >  How to set transactions per minute in Locust
How to set transactions per minute in Locust

Time:10-13

I am writing a LoadTestShape Class. I want to be able to set number of transaction per minute. If I try to set it by specifying user_count it doesn't work because the RPS will vary cpu to cpu .

If say I want to set 1000 transaction per minute, how can we achieve that in locust?

CodePudding user response:

The only way to "achieve" this in Locust is implementing Pacing, the logic is:

  • You know how much time one iteration takes for one user
  • If user manages to finish faster - you need to introduce some "think time" so your thread "sleeps" till the desired time
  • If iteration is slower - no sleep is required.

This answer shows how you can create a custom load shape

Alternatively you can consider switching to a load testing tool which provides such functionality, i.e. Apache JMeter with Constant Throughput Timer

CodePudding user response:

for this timing that's enough to add a LoadTestShape class to locustfile.py like the below code which I use for one of my tests you can change the number of users or other parameters as you want(I write a full docstring that describes each parameter in that):

class StagesShape(LoadTestShape):
    """
    A simply load test shape class that has different user and spawn_rate at
    different stages.
    Keyword arguments:
        stages -- A list of dicts, each representing a stage with the following keys:
            duration -- When this many seconds pass the test is advanced to the next stage
            users -- Total user count
            spawn_rate -- Number of users to start/stop per second
            stop -- A boolean that can stop that test at a specific stage
        stop_at_end -- Can be set to stop once all stages have run.
    """

    stages = [
        {"duration": 60, "users": 3, "spawn_rate": 0.05},
        {"duration": 60, "users": 6, "spawn_rate": 0.05},
        {"duration": 60, "users": 9, "spawn_rate": 0.05},
        {"duration": 60, "users": 12, "spawn_rate": 0.05},
        {"duration": 60, "users": 15, "spawn_rate": 0.05},
        {"duration": 60, "users": 18, "spawn_rate": 0.05},
        {"duration": 60, "users": 21, "spawn_rate": 0.05},
        {"duration": 60, "users": 24, "spawn_rate": 0.05},
        {"duration": 60, "users": 27, "spawn_rate": 0.05},
        {"duration": 60, "users": 30, "spawn_rate": 0.05},
    ]

    def tick(self):
        run_time = self.get_run_time()

        for stage in self.stages:
            if run_time < stage["duration"]:
                tick_data = (stage["users"], stage["spawn_rate"])
                return tick_data

        return None
  • Related