I'm interested in using Locust to load test an endpoint. From documentation (see third "Note" section.)
For example, if you want Locust to run 500 task iterations per second at peak load, you could use wait_time = constant_throughput(0.1) and a user count of 5000.
constant_throughput
appears to be an expected variable name in scope of classes that inherit from User.
from locust import User, task, between
class MyUser(User):
@task
def my_task(self):
print("executing my_task")
wait_time = between(0.5, 10)
However, it's ambiguous how user count should be parameterized to Locust.
What's the right way to go about this?
I can see how it can be confusing if you don't already know how to do it. Perhaps we should come up with a more clear and direct explanation. The documentation describes the Locust API and constant_throughput
is at the same level as all other classes and attributes so you import it the same way. In the code example you cited, replacing between
with constant_throughput
is how it should work:
from locust import User, task, constant_throughput
class MyUser(User):
@task
def my_task(self):
print("executing my_task")
wait_time = constant_throughput(0.1)
However, user count is not typically parameterized in code. You define that at run time when you run Locust either headless or via the web UI. That said, there are ways of doing it in code if you must. I'd recommend looking into Custom Load Shapes where you have total control over the user count at any given time. You could define an algorithm that determines user count or just pull values from a hardcoded list. If you need even more control than that, you could use Locust as a library and do whatever you want.