Running Locust without the web UI¶
You can run locust without the web UI - for example if you want to run it in some automated flow,
like a CI server - by using the --headless
flag together with -u
and -r
:
$ locust -f locust_files/my_locust_file.py --headless -u 1000 -r 100
-u
specifies the number of Users to spawn, and -r
specifies the spawn rate
(number of users to start per second).
Setting a time limit for the test¶
If you want to specify the run time for a test, you can do that with --run-time
or -t
:
$ locust -f --headless -u 1000 -r 100 --run-time 1h30m
Locust will shutdown once the time is up.
Allow tasks to finish their iteration on shutdown¶
By default, locust will stop your tasks immediately. If you want to allow your tasks to finish their iteration, you can use --stop-timeout <seconds>
.
$ locust -f --headless -u 1000 -r 100 --run-time 1h30m --stop-timeout 99
Running Locust distributed without the web UI¶
If you want to run Locust distributed without the web UI,
you should specify the --expect-workers
option when starting the master node, to specify
the number of worker nodes that are expected to connect. It will then wait until that many worker
nodes have connected before starting the test.
Controlling the exit code of the Locust process¶
When running Locust in a CI environment, you might want to control the exit code of the Locust
process. You can do that in your test scripts by setting the
process_exit_code
of the
Environment
instance.
Below is an example that’ll set the exit code to non zero if any of the following conditions are met:
More than 1% of the requests failed
The average response time is longer than 200 ms
The 95th percentile for response time is larger than 800 ms
import logging
from locust import events
@events.quitting.add_listener
def _(environment, **kw):
if environment.stats.total.fail_ratio > 0.01:
logging.error("Test failed due to failure ratio > 1%")
environment.process_exit_code = 1
elif environment.stats.total.avg_response_time > 200:
logging.error("Test failed due to average response time ratio > 200 ms")
environment.process_exit_code = 1
elif environment.stats.total.get_response_time_percentile(0.95) > 800:
logging.error("Test failed due to 95th percentile response time > 800 ms")
environment.process_exit_code = 1
else:
environment.process_exit_code = 0
(this code could go into the locustfile.py or in any other file that is imported in the locustfile)