In-depth discussion of performance testing, from entry to abandonment: Locust performance automation (1) First encounter with Locust

1. Basic introduction to Locust

1.1 Introduction

Now whether it is the Internet industry or the traditional industry, the requirements for performance are increasing day by day.
In order to locate problems faster and more accurately, find and solve problems,
more and more performance testing tools have appeared on the market, such as Jmeter. , Loadrunner, Locus, etc., and today, we mainly introduce Locust!
Many people don’t know what Locust is, including those who use python, because they don’t involve it, so they won’t know about it.
Then, what is Loucst and the function of Locust, what is it,
follow Xiaoyu, look down ~

1.2 Introduction

Locust is an open source performance testing tool developed in Python, based on events, supporting distributed and providing Web UI for test execution and result display.
1. Comparison of
resources occupied by Locust and Jmeter The reason why Locust outperforms the open source Jmeter in terms of resource occupancy is
mainly because:
>>The two modes of users are different:
①Jmeter uses threads as a virtual user
②Locust uses the gevent library for the coroutine Support, emulate you with greenlet.
Therefore, under the same configuration, the number of concurrent users that Locust can support is more than one level higher than Jmeter.
2.
Locust uses the language Locust uses Python code to define test scenarios, currently supports Python2.7, 3.3~3.7.
It comes with a Web UI for defining user models, initiating tests, real-time test data, error statistics, etc.

3. Official document For
more detailed content, please refer to:
1. Document content, click: official document
2. Source code, click: Github

2. Locsut installation method

Similarly, we can install directly with pip

2.1 Install locust

pip install  locust

2.2 Install pyzmq

If you plan to run Locust in multiple processes/processes, you need to install pyzmq and
also use pip to install

pip install pyzmq

Note:
If you install locust-1.2.3 version , you may not need to install pyzmq again,
it seems to be installed directly with pyzmq

2.3 Confirmation of successful installation

Open the cmd window and enter directly

locust --help

If the following picture appears, the installation was successful
Insert picture description here

3. Locust parameter description

After the installation of the above figure is successful, let's analyze the parameters

3.1 Parameter information

We first copy all the information in locust --help

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. Primarily used
                        together with --headless
  -r SPAWN_RATE, --spawn-rate SPAWN_RATE
                        The rate per second in which users are spawned.
                        Primarily 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. You must also specify the '--
                        csv' argument to enable this.
  --print-stats         Print stats in the console
  --only-summary        Only print the summary stats
  --reset-stats         Reset statistics once spawning 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.2 Parameter information analysis

Here, only the most part is written. As for the missing parts, Xiaoyu feels that it is not used frequently, so it will not waste electricity~ After all, 1 kilowatt-hour of electricity is more than 1 yuan! !
Also, Xiaoyu is going to have lunch~

-h, --help    查看帮助
-H HOST, --host=HOST    指定被测试的主机,采用以格式:http://10.21.32.33
--web-host=WEB_HOST    指定运行 Locust Web 页面的主机,默认为空 ''-P PORT, --port=PORT, --web-port=PORT    指定 --web-host 的端口,默认是8089
-f LOCUSTFILE, --locustfile=LOCUSTFILE    指定运行 Locust 性能测试文件,默认为: locustfile.py
--csv=CSVFILEBASE, --csv-base-name=CSVFILEBASE    以CSV格式存储当前请求测试数据。
--master    Locust 分布式模式使用,当前节点为 master 节点。
--slave    Locust 分布式模式使用,当前节点为 slave 节点。
--master-host=MASTER_HOST    分布式模式运行,设置 master 节点的主机或 IP 地址,只在与 --slave 节点一起运行时使用,默认为:127.0.0.1.
--master-port=MASTER_PORT    分布式模式运行, 设置 master 节点的端口号,只在与 --slave 节点一起运行时使用,默认为:5557。注意,slave 节点也将连接到这个端口+1 上的 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. Note that Locust will also use this port + 1, so by default the master node will bind to 5557 and 5558.
--expect-slaves=EXPECT_SLAVES    How many slaves master should expect to connect before starting the test (only when --no-web used).
--no-web    no-web 模式运行测试,需要 -c 和 -r 配合使用.
-c NUM_CLIENTS, --clients=NUM_CLIENTS    指定并发用户数,作用于 --no-web 模式。
-r HATCH_RATE, --hatch-rate=HATCH_RATE    指定每秒启动的用户数,作用于 --no-web 模式。
-t RUN_TIME, --run-time=RUN_TIME    设置运行时间, 例如: (300s, 20m, 3h, 1h30m). 作用于 --no-web 模式。
-L LOGLEVEL, --loglevel=LOGLEVEL    选择 log 级别(DEBUG/INFO/WARNING/ERROR/CRITICAL). 默认是 INFO.
--logfile=LOGFILE    日志文件路径。如果没有设置,日志将去 stdout/stderr
--print-stats    在控制台中打印数据
--only-summary    只打印摘要统计
--no-reset-stats    Do not reset statistics once hatching has been completed。
-l, --list    显示测试类, 配置 -f 参数使用
--show-task-ratio    打印 locust 测试类的任务执行比例,配合 -f 参数使用.
--show-task-ratio-json    以 json 格式打印 locust 测试类的任务执行比例,配合 -f 参数使用.
-V, --version    查看当前 Locust 工具的版本.

3.3 Locust main library

gevent
gevent is a Python network library based on coroutines. It uses the high-level synchronization API provided by Greenlet and encapsulates the libevent event loop.

A lightweight web application framework written in flask
Python.
If you want to understand flask and the actual code, read this article by Xiaoyu:
"Python3, website building and building Flask project"

requests
Python's HTTP library
can refer to this article by Xiaoyu "Seven main methods commonly used in requests library and control access parameters"

msgpack-python
MessagePack is a fast and compact binary serialization format, suitable for data formats similar to JSON. msgpack-python mainly provides methods for serializing and deserializing MessagePack data.

six
Python2 and 3 compatible libraries, used to encapsulate the differences between Python2 and Python3

pyzmq
pyzmq is a Python binding of zeromq (a communication queue), which is mainly used to realize the distributed mode operation of Locust.

4. Locust class description

4.1 client attributes

①In the Locust class, the static field client is the request method of the client. The client field here is not bound to the client request method. Therefore, when using Locust, you need to inherit the Locust class class HttpLocust (Locust) first, and then set self.client = HttpSession(base_url=self.host) binds the client request method;

②For the common HTTP(s) protocol, Locust has implemented the HttpLocust class, with self.client=HttpSession(base_url=self.host) , and HttpSession inherits from requests.Session.

③In the Locust script for testing HTTP(s), all methods of the Python requests library can be used through the client attribute, and the calling method is exactly the same as that of reqeusts .

Due to the use of requests.Session , the state memory function is automatically provided between client method calls.
⑤The common scenario is that after logging in to the system, the session that can maintain the login state , so that subsequent HTTP request operations can bring the login state .

4.2 Other attributes

4.2.1 task_set

Point to a TaskSet class, TaskSet class defines the user's task information, this static field is required.

4.2.2 max_wait/min_wait

The upper and lower limits (milliseconds) of the interval for each user to perform two tasks. The specific value is randomly selected from the upper and lower limits. If not specified, the default interval is 1 second.

4.2.3 host

The host of the tested system is only used when the -host parameter is not specified when starting locust in the terminal.

4.2.4 weight

When running multiple Locust classes at the same time, it is used to control the execution weight of different types of tasks.

5. Loucst execution process

Specific process is as follows:
performed first on_start (only once) WebsiteTasks is, as the initialization;

Randomly select from WebsiteTasks (if the weight relationship between tasks is defined, then randomly select according to the weight relationship) a task to execute;

The time interval and Locust class min_wait MAX_WAIT defined (if class is also defined taskset or min_wait MAX_WAIT, to preferentially in taskset), randomly in a range of time values, dormant wait;

Repeat steps 2~3 until the test task is terminated.

6. Comparison of performance testing tools

Regarding how to choose performance testing tools, Xiaoyu wrote in " Deep Talking about Performance Testing, From Getting Started to Giving Up: Performance Testing for the First Time ".
Each tool has its own value, that is, existence means meaning.

Next, Xiaoyu will give you an analysis, Jmeter, Loadrunner, Locust these three tools.

project Jmeter Loadrunner Locust
Authorization method Open source Commercial charges Open source
Development language Java C / Java Pyhton
Test script form GUI C / Java Python
Concurrency mechanism Thread Process/thread Coroutine
Single machine concurrency low low high
Distributed pressure stand by stand by stand by
Resource monitoring not support stand by not support
Report and analysis Simple chart perfect Simple chart
Support secondary development stand by not support stand by

By comparison, it can be seen that Locust is not dominant. However, Xiaoyu likes to use this
because:
1. First, it simulates user operations.
Locust uses Pure Python script description, and HTTP requests are completely based on the Requests library.

②Requests is a very concise and easy-to-use library, but it has very powerful functions. Many HTTP libraries in other programming languages ​​have borrowed its ideas and patterns. If it is selected as one of the best HTTP libraries (no language limitation), it should be There will not be much controversy.

③In addition to the HTTP(S) protocol, Locust can also test other systems of any protocol, and only needs to use Python to call the corresponding library for request description.

2. Concurrency mechanism
Locust 's concurrency mechanism adopts the mechanism of gevent .

②When multi-threading is used to simulate multiple users, the number of threads will increase as the number of concurrency increases , and the switching between threads requires resources. Blocking of IO and thread sleep will inevitably lead to a decrease in concurrency efficiency; For this reason, testing tools such as LoadRunner and Jmeter that use processes and threads are difficult to simulate high concurrency pressure on a single machine.

③The difference between coroutine and thread is : coroutine avoids system-level resource scheduling, thereby greatly improving performance.

④Under normal circumstances, a single test machine with a common configuration can produce thousands of concurrent pressures, which is impossible for LoadRunner and Jmeter.

7. Locust code combat

The actual code part, I put it in the second chapter to show.
You can directly click to send
"Deep Chat Performance Test, From Entry to Abandonment: Locust Performance Automation (2) Code Actual Combat"

Xiao Diaosi: Brother Yu, can you share a little first, what is the interface of Locust?
Xiaoyu: Uh, you can't wait~~
Xiao Diaosi: I don't think so, just give a picture and see, what is the place of Niu X?
Xiaoyu: Okay, waiting for tomorrow! I have to eat! ! !

Guess you like

Origin blog.csdn.net/wuyoudeyuer/article/details/108596264