In the Python load test tool Locust, by coordinating multiple processes, parallelization can put a large load on the host under test. In this article, I'll show you how to do this with a single multi-core machine. The Web UI is not used. [^ 1]
In the official documentation, it is introduced on the page below.
[^ 1]: How to use Web UI is introduced in Preparing a distributed load test environment with Python load test tool Locust --Qiita. It has been.
In the following, it is assumed that the execution environment of locust
is ready. [^ 2]
Also, I will not explain how to write a locate file.
[^ 2]: Click here for how to install locust: https://docs.locust.io/en/stable/installation.html
Just create a shell script like the one below and run it.
run-locust-distributed.sh
#!/usr/bin/env bash
set -uc
#Create log and report output destination directory
mkdir -p logs output
host=https://example.com #Test target host
locustfile=yourlocustfile.py
slave_num=8 #Number of slaves (number of parallels)- 1)
master_port=5557
max_clients=200 #Maximum number of clients
duration=90m #Total execution time
step_clients=5 #Increased number of clients/interval
step_interval=1m #interval
# master
taskset -c 0 locust -f $locustfile --no-web \
-c $max_clients -t $duration -H $host \
--master --expect-slaves $slave_num --master-bind-port $master_port \
--step-load --step-clients $step_clients --step-time $step_interval \
--logfile logs/locust.master.log -L WARNING --csv output/locust.master \
&> /dev/null &
# slaves
for no in $(seq 1 ${slave_num}); do
taskset -c $no locust -f $locustfile --no-web \
-c $max_clients -H $host \
--slave --master-host 127.0.0.1 --master-port $master_port \
--logfile logs/locust.slave$no.log -L WARNING --csv output/locust.slave$no \
&> /dev/null &
done
--Cooperates with the master process and multiple slave processes.
--I am using the taskset (1) command to specify the core on which the process runs. This is the most important thing.
--By adding the --expect-slaves = N
option on the master side, the test will start after waiting for the specified number of slaves to connect. [^ 3]
--The --step-load
line is optional. Please use it when you want to increase the load step by step with Step Load Mode.
--With the --csv
option, various indicators and logs are recorded in CSV.
--The -L
option is the log level. With the default ʻINFO, there are many parts where information overlaps with CSV, so I think that
WARNING` is necessary and sufficient. If you watch the log, you will notice when there is a problem such as lack of CPU resources.
If the CPU resource of the locust execution host is insufficient, the following log will be output. If such a log appears, it is possible that the expected load cannot be applied, so it is better to consider parallelization with a master-slave configuration or an increase in the number of slaves.
For single process:
[2020-04-15 05:57:28,228] hostname/WARNING/root: Loadgen CPU usage above 90%! This may constrain your throughput and may even give inconsistent response time measurements! See https://docs.locust.io/en/stable/running-locust-distributed.html for how to distribute the load over multiple CPU cores or machines
When running in a distributed manner in a master-slave configuration:
#master side
[2020-04-15 07:41:01,928] hostname/WARNING/locust.runners: Slave hostname_886b86153c0c4a839a5dc35a190be166 exceeded cpu threshold (will only log this once per slave)
[2020-04-15 07:43:50,863] hostname/WARNING/locust.runners: CPU usage threshold was exceeded on slaves during the test!
#The slave side is the same as in the case of a single process, so it is omitted.