how to start new users every seconds in locust as a library

808 views Asked by At

In my scenario, I'm stopping users after completing a SequentialTaskSet. At the same time, I need to launch users in a regular interval, for example at each 100ms. There may be some users already running or not, it doesn't matter. However, if there's a user already running, locust won't create a new one, i.e. it won't create on top of a user.

def spawn_users(env):
    while True:
        env.runner.start(1, spawn_rate=50)
        gevent.sleep(0.1) # sleep for 100 ms

for x in range(1000):
    gevent.spawn(spawn_users, env)

N.B. Obviously, I can create 50/100/any number of users at a time but I need to create one by one.

Summary: Create locust users at a regular interval no matter how many users already running and how much time they are taking executing tasks.

Note: I'm also open to try other load testing frameworks if this use case is more suitable for them.

2

There are 2 answers

0
Solowalker On

A more supported and perhaps simpler way to do this would be to use a Load Test Shape.

https://docs.locust.io/en/stable/generating-custom-load-shape.html

The end result is similar to what you've already done, but may give you slightly more freedom and abstracted stability.

0
Rajib Hossen On

I solved it using a simple trick. Instead of creating 1 user at a time, I'm creating +1 with already existing users.

def spawn_users(env):
while True:
    env.runner.start(env.runner.user_count + 1, spawn_rate=50)
    gevent.sleep(0.1) # sleep for 100 ms

env.runner.user_count provides the current total user, so, after each sleep, it will create one more.