1. What is locust?
引用官网文案:Define user behaviour with Python code, and swarm your system with millions of simultaneous users.
That is, use Python code to define user behavior and let millions of concurrent users flood into your system.
2. Why use locust?
Compared with loadrunner and jmeter, locust's advantages are mainly:
1. Based on the coroutine, system-level resource scheduling is avoided, and the single-machine concurrency capability is greatly improved
2. Open source and free, simple function, low learning cost
3. Based on python, and the HTTP request is completely based on the Requests library, other protocols or custom requests can also be supported, with strong scalability, and all systems can be tested theoretically
Of course, the shortcomings of locust are also obvious, that is, the minimalist report and analysis that have been criticized by people, and the lack of support for resource monitoring
Therefore, the advantages and disadvantages of locust are obvious, and the choice depends on individual needs.
The specific comparison is as follows:
LR | Jmeter | Locust | |
Development language | C / JAVA | JAVA | PYTHON |
Script form | C / JAVA | GUI | PYTHON |
Authorization method | Commercial charges | Open source and free | Open source and free |
Concurrency mechanism | Process/thread | Thread | Coroutine |
Single machine concurrency | low | low | high |
LF | high | high | low |
distributed | stand by | stand by | stand by |
Report and analysis | comprehensive | simple | simple |
Resource monitoring | stand by | not support | not support |
Secondary development | not support | stand by | stand by |
3. Why are most of the locust tutorials found on the Internet unavailable?
In fact, the reason why it can't be used is very simple. Because the times have changed, most of the online tutorials are in the '0.n' version, and locust has entered the '1.n' version in 2020.
However, most of the online tutorials are still in version 0, and the versions are inconsistent and naturally cannot be used.
So what are the version changes from 0 to 1?
1. Installation
The installation command is unified as: pip install locust
If you use pip install locustio, you must specify the previous version starting with 0
2. Parameters
You can pull out the help file directly
C:\Users\Administrator>locust --help
Usage: locust [OPTIONS] [UserClass ...]
Common options:
-h, --help show this help message and exit
-f LOCUSTFILE, --locustfile LOCUSTFILE
Python module file to import, e.g. '../other.py'.
Default: locustfile
--config CONFIG Config file path
-H HOST, --host HOST Host to load test in the following format:
http://10.21.32.33
-u NUM_USERS, --users NUM_USERS
Number of concurrent Locust users. Only used together
with --headless
-r HATCH_RATE, --hatch-rate HATCH_RATE
The rate per second in which users are spawned. Only
used together with --headless
-t RUN_TIME, --run-time RUN_TIME
Stop after the specified amount of time, e.g. (300s,
20m, 3h, 1h30m, etc.). Only used together with
--headless
-l, --list Show list of possible User classes and exit
Web UI options:
--web-host WEB_HOST Host to bind the web interface to. Defaults to '*'
(all interfaces)
--web-port WEB_PORT, -P WEB_PORT
Port on which to run web host
--headless Disable the web interface, and instead start the load
test immediately. Requires -u and -t to be specified.
--web-auth WEB_AUTH Turn on Basic Auth for the web interface. Should be
supplied in the following format: username:password
--tls-cert TLS_CERT Optional path to TLS certificate to use to serve over
HTTPS
--tls-key TLS_KEY Optional path to TLS private key to use to serve over
HTTPS
Master options:
Options for running a Locust Master node when running Locust distributed. A Master node need Worker nodes that connect to it before it can run load tests.
--master Set locust to run in distributed mode with this
process as master
--master-bind-host MASTER_BIND_HOST
Interfaces (hostname, ip) that locust master should
bind to. Only used when running with --master.
Defaults to * (all available interfaces).
--master-bind-port MASTER_BIND_PORT
Port that locust master should bind to. Only used when
running with --master. Defaults to 5557.
--expect-workers EXPECT_WORKERS
How many workers master should expect to connect
before starting the test (only when --headless used).
Worker options:
Options for running a Locust Worker node when running Locust distributed.
Only the LOCUSTFILE (-f option) need to be specified when starting a Worker, since other options such as -u, -r, -t are specified on the Master node.
--worker Set locust to run in distributed mode with this
process as worker
--master-host MASTER_NODE_HOST
Host or IP address of locust master for distributed
load testing. Only used when running with --worker.
Defaults to 127.0.0.1.
--master-port MASTER_NODE_PORT
The port to connect to that is used by the locust
master for distributed load testing. Only used when
running with --worker. Defaults to 5557.
Tag options:
Locust tasks can be tagged using the @tag decorator. These options let specify which tasks to include or exclude during a test.
-T [TAG [TAG ...]], --tags [TAG [TAG ...]]
List of tags to include in the test, so only tasks
with any matching tags will be executed
-E [TAG [TAG ...]], --exclude-tags [TAG [TAG ...]]
List of tags to exclude from the test, so only tasks
with no matching tags will be executed
Request statistics options:
--csv CSV_PREFIX Store current request stats to files in CSV format.
Setting this option will generate three files:
[CSV_PREFIX]_stats.csv, [CSV_PREFIX]_stats_history.csv
and [CSV_PREFIX]_failures.csv
--csv-full-history Store each stats entry in CSV format to
_stats_history.csv file
--print-stats Print stats in the console
--only-summary Only print the summary stats
--reset-stats Reset statistics once hatching has been completed.
Should be set on both master and workers when running
in distributed mode
Logging options:
--skip-log-setup Disable Locust's logging setup. Instead, the
configuration is provided by the Locust test or Python
defaults.
--loglevel LOGLEVEL, -L LOGLEVEL
Choose between DEBUG/INFO/WARNING/ERROR/CRITICAL.
Default is INFO.
--logfile LOGFILE Path to log file. If not set, log will go to
stdout/stderr
Step load options:
--step-load Enable Step Load mode to monitor how performance
metrics varies when user load increases. Requires
--step-users and --step-time to be specified.
--step-users STEP_USERS
User count to increase by step in Step Load mode. Only
used together with --step-load
--step-time STEP_TIME
Step duration in Step Load mode, e.g. (300s, 20m, 3h,
1h30m, etc.). Only used together with --step-load
Other options:
--show-task-ratio Print table of the User classes' task execution ratio
--show-task-ratio-json
Print json data of the User classes' task execution
ratio
--version, -V Show program's version number and exit
--exit-code-on-error EXIT_CODE_ON_ERROR
Sets the process exit code to use when a test result
contain any failure or error
-s STOP_TIMEOUT, --stop-timeout STOP_TIMEOUT
Number of seconds to wait for a simulated user to
complete any executing task before exiting. Default is
to terminate immediately. This parameter only needs to
be specified for the master process when running
Locust distributed.
User classes:
UserClass Optionally specify which User classes that should be
used (available User classes can be listed with -l or
--list)
3. Code
(1) Rename the Locust class to User, rename the HttpLocust class to HttpUser, and rename the TaskSet class attribute locust to user
(2) Remove the task_set attribute in the Locust class and use the tasks attribute in the User class instead, and tasks can only be lists or dictionaries
(3) Add tag tags. When executing, you can use -T --tags to specify tag execution, -E --exclude-tags to exclude specified tag execution
(4) You can use the @task decorator directly under the inherited User class
(5) Environmental variables
· LOCUST_MASTER renamed to LOCUST_MODE_MASTER
· LOCUST_SLAVE renamed to LOCUST_MODE_WORKER
· LOCUST_MASTER_PORT is renamed to LOCUST_MASTER_NODE_PORT
· LOCSUT_MASTER_HOST renamed to LOCUST_MASTER_NODE_HOST
· CSVFILEBASE renamed to LOCUST_CSV
(6) In the distributed mode, the parameter of the assist process is changed from --slave \ expect-slaves to --worker \ --expect-workers
(7) 移除 Locust.setup、 Locust.teardown、 TaskSet.setup、 TaskSet.teardown,改用 on_test_start、on_test_stop
(8) The smooth class TaskSequence and the sequence decorator @seq_task are all packaged to SequentialTaskSet.
(9) No graphics mode startup parameter, changed from --no-web to --headless
(10) Operating data records, csv files, header changes, statistics are added to make the meaning of the header clearer
(11) The parameter --csv-base-name of the result written to the csv file is removed
(12) The console log does not use the standard output stdout (or standard error output stderr). To output the log, you can directly use print() for output, or you can use the logging module to define the log