Search code examples
pythonloadperformance-testinglocust

Locust load testing - change hatch rate from seconds to minutes?


I'd like to simulate spikey traffic, so that for example:

  • in the first 5 minutes there are only 50 users (instant hatch of 50 at time T0)
  • then from 5th to 10th minute we have 100 users (instant hatch +50 at T+5)
  • then 150 (instant hatch +50 at T+10)
  • etc.

Is it possible to create an equal number of users, but instead of doing that every second change that to every xx minutes?


Solution

  • There is no such built in feature (https://github.com/locustio/locust/issues/1353 might solve this if it is ever implemented)

    One way to do a workaround is to spawn all your users right away (using a spawn rate of something like 100/s), and have them sleep until it is time to run:

    import time
    start = time.time()
    
    class User1(HttpUser):
        @task
        def mytask(self):
            # do actual task
    
    class User2(HttpUser):
        @task
        def mytask(self):
            while time.time() - start < 300:
                time.sleep(1)
            # do actual task
    
    class User3(HttpUser):
        @task
        def mytask(self):
            while time.time() - start < 600:
                time.sleep(1)
            # do actual task
    
    ...
    

    You can probably do something clever and put it all in one class, but I'll leave that as an exercise :)