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

Viewed 689

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?

2 Answers

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 :)

Locust 2.8.6 Update

Now you can benefit from using custom shapes. Read more at Locust Documentation.

You should use the tick function and define the return tuple of your user limit and spawn rate.

Here is a code example:

from locust import LoadTestShape


class SharpStepShape(LoadTestShape):
    increase_delay = 300  # 5 minutes for increase
    increase_size = 50  # number of extra users per increase

    def tick(self):
        run_time = self.get_run_time()
        step_number = int(run_time / self.increase_delay) + 1
        user_limit = int(step_number * self.increase_size)
        return user_limit, self.increase_size

Then just import this shape into your locustfile and it will use this shape for your load testing.

from locust import User
from sharp_step_shape import SharpStepShape


class PerformanceUser(User):
    pass


Related