开源性能测试工具Locust
1 参考
性能测试工具Locust
https://www.cnblogs.com/ailiailan/p/9474973.html
性能测试工具Locust
https://www.cnblogs.com/fnng/p/6081798.html
Locust 系列教程:
《Locust 介绍》http://www.testclass.net/locust/introduce/
《Locust 安装》http://www.testclass.net/locust/install/
《Locust 创建性能测试》http://www.testclass.net/locust/create-first-test/
《Locust no-web模式》http://www.testclass.net/locust/no-web-run/
《Locust 参数说明》http://www.testclass.net/locust/help/
《Locust 分布式运行》http://www.testclass.net/locust/distributed/
《Locust 类和方法》http://www.testclass.net/locust/dev-script/
《Locust 设置断言》http://www.testclass.net/locust/assert/
《Locust 参数化》http://www.testclass.net/locust/parameterization/
2 官方网站
官方使用文档:https://docs.locust.io/en/latest/
大并发量测试时,建议在linux系统下进行。
An open source load testing tool.
define user behaviour with python code, and swarm your system with millions of simultaneous users.
一个开源性能测试工具。
使用Python代码来定义用户行为。用它可以模拟百万计的并发用户访问你的系统。
3 Locust安装
3.1 安装Python
安装Python2 或Python3
3.2 安装Locuse失败
a通过pip命令安装 /> pip install locustio
在CMD下面执行命令即可,如下。
遇到问题:
**** Locust package has moved from 'locustio' to 'locust'. Please update your reference (or pin your version to 0.14.6 if you dont want to update to 1.0) ****
解决方法:
一是根据提示去安装Microsoft Visual C++ 14.0,但是这个方法消耗的时间和空间都非常的高,大家可以量力而行;
二是安装最新(发布前)的locust,
pip install -U setuptools
pip install -U --pre locustio
设置超时时间,
pip --default-timeout=100 install -U Pillow(对应的是软件包模块的名称)
pip --default-timeout=100 install -U setuptools
pip --default-timeout=100 install -U --pre locustio
解决办法:
pip3 install -i https://pypi.douban.com/simple/ locustio==0.14.6
F:\locust>pip3 install -i https://pypi.douban.com/simple/ locustio==0.14.6
Looking in indexes: https://pypi.douban.com/simple/
Collecting locustio==0.14.6
Downloading https://pypi.doubanio.com/packages/fc/b8/1f01a500292f9dc7234dfb95c81d76101091d54d030a0c696fbef2027dd6/locustio-0.14.6-py3-none-any.whl (263kB)
100% |████████████████████████████████| 266kB 3.6MB/s
Collecting msgpack>=0.6.2 (from locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/03/5d/22303a769b2ff7137402bd3b0e33e08ae3a9cbfddb6d3acaf92c1ad50a2d/msgpack-1.0.2-cp37-cp37m-win32.whl (61kB)
100% |████████████████████████████████| 61kB 1.1MB/s
Collecting flask>=0.10.1 (from locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94kB)
100% |████████████████████████████████| 102kB 3.8MB/s
Collecting gevent==20.4.0 (from locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/e5/dc/a6a92da9034848bad03e4eb071ebe19e02a81ff55d5221486848299b727c/gevent-20.4.0-cp37-cp37m-win32.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 3.4MB/s
Collecting psutil>=5.6.7 (from locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/18/c9/1db6aa0d28831f60408a6aab9d108c2edbd5a9ed11e5957a91d9d8023898/psutil-5.8.0-cp37-cp37m-win32.whl (240kB)
100% |████████████████████████████████| 245kB 4.0MB/s
Collecting ConfigArgParse>=1.0 (from locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/bb/79/3045743bb26ca2e44a1d317c37395462bfed82dbbd38e69a3280b63696ce/ConfigArgParse-1.2.3.tar.gz (42kB)
100% |████████████████████████████████| 51kB 4.9MB/s
Collecting requests>=2.9.1 (from locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/29/c1/24814557f1d22c56d50280771a17307e6bf87b70727d975fd6b2ce6b014a/requests-2.25.1-py2.py3-none-any.whl (61kB)
100% |████████████████████████████████| 61kB 5.1MB/s
Collecting pyzmq>=16.0.2 (from locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/64/29/62d2d28a3766f36441438faa3ae621fd79d819c0c5a2a07548bd97581f81/pyzmq-20.0.0-cp37-cp37m-win32.whl (898kB)
100% |████████████████████████████████| 901kB 3.7MB/s
Collecting geventhttpclient-wheels==1.3.1.dev2 (from locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/19/f3/5cc11d5605d4d9fee4174d6183ce00aff10da4e8c44c563aaf3587f4524f/geventhttpclient_wheels-1.3.1.dev2-cp37-cp37m-win32.whl (52kB)
100% |████████████████████████████████| 61kB 934kB/s
Collecting click>=5.1 (from flask>=0.10.1->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82kB)
100% |████████████████████████████████| 92kB 7.4MB/s
Collecting Werkzeug>=0.15 (from flask>=0.10.1->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB)
100% |████████████████████████████████| 307kB 6.8MB/s
Collecting itsdangerous>=0.24 (from flask>=0.10.1->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10.1 (from flask>=0.10.1->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl (125kB)
100% |████████████████████████████████| 133kB 7.1MB/s
Collecting cffi>=1.12.2; platform_python_implementation == "CPython" and sys_platform == "win32" (from gevent==20.4.0->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/2d/2c/62fa58a0f835a67acc71e61530b53f863bca14c51e73d4419fb93400f76d/cffi-1.14.4-cp37-cp37m-win32.whl (166kB)
100% |████████████████████████████████| 174kB 3.4MB/s
Collecting greenlet>=0.4.14; platform_python_implementation == "CPython" (from gevent==20.4.0->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/80/7d/fe2b8c93e67987ca40342035337ce32f33d1f9d8ba2008a4e252cb0507b3/greenlet-0.4.17-cp37-cp37m-win32.whl
Collecting chardet<5,>=3.0.2 (from requests>=2.9.1->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/19/c7/fa589626997dd07bd87d9269342ccb74b1720384a4d739a1872bd84fbe68/chardet-4.0.0-py2.py3-none-any.whl (178kB)
100% |████████████████████████████████| 184kB 9.3MB/s
Collecting urllib3<1.27,>=1.21.1 (from requests>=2.9.1->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/f5/71/45d36a8df68f3ebb098d6861b2c017f3d094538c0fb98fa61d4dc43e69b9/urllib3-1.26.2-py2.py3-none-any.whl (136kB)
100% |████████████████████████████████| 143kB 8.2MB/s
Collecting certifi>=2017.4.17 (from requests>=2.9.1->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/5e/a0/5f06e1e1d463903cf0c0eebeb751791119ed7a4b3737fdc9a77f1cdfb51f/certifi-2020.12.5-py2.py3-none-any.whl (147kB)
100% |████████████████████████████████| 153kB 4.4MB/s
Collecting idna<3,>=2.5 (from requests>=2.9.1->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/a2/38/928ddce2273eaa564f6f50de919327bf3a00f091b5baba8dfa9460f3a8a8/idna-2.10-py2.py3-none-any.whl (58kB)
100% |████████████████████████████████| 61kB 2.9MB/s
Collecting six (from geventhttpclient-wheels==1.3.1.dev2->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask>=0.10.1->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/5b/d4/1deb3c5dc3714fb160c7e2116fc6dff36a063d9156a9328cce54ef35cc52/MarkupSafe-1.1.1-cp37-cp37m-win32.whl
Collecting pycparser (from cffi>=1.12.2; platform_python_implementation == "CPython" and sys_platform == "win32"->gevent==20.4.0->locustio==0.14.6)
Downloading https://pypi.doubanio.com/packages/ae/e7/d9c3a176ca4b02024debf82342dab36efadfc5776f9c8db077e8f6e71821/pycparser-2.20-py2.py3-none-any.whl (112kB)
100% |████████████████████████████████| 112kB 5.8MB/s
Installing collected packages: msgpack, click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, flask, pycparser, cffi, greenlet, gevent, psutil, ConfigArgParse, chardet, urllib3, certifi, idna, requests, pyzmq, six, geventhttpclient-wheels, locustio
Running setup.py install for ConfigArgParse ... done
Successfully installed ConfigArgParse-1.2.3 Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 certifi-2020.12.5 cffi-1.14.4 chardet-4.0.0 click-7.1.2 flask-1.1.2 gevent-20.4.0 geventhttpclient-wheels-1.3.1.dev2 greenlet-0.4.17 idna-2.10 itsdangerous-1.1.0 locustio-0.14.6 msgpack-1.0.2 psutil-5.8.0 pycparser-2.20 pyzmq-20.0.0 requests-2.25.1 six-1.15.0 urllib3-1.26.2
F:\locust>
|
3.3 在\Python\Python37-32\Scripts目录下安装Locuse
C:\Users\lizm>cd C:\Users\lizm\AppData\Local\Programs\Python\Python37-32\Scripts
C:\Users\lizm\AppData\Local\Programs\Python\Python37-32\Scripts>
C:\Users\lizm\AppData\Local\Programs\Python\Python37-32\Scripts>pip install locust
Collecting locust
Downloading https://files.pythonhosted.org/packages/0b/23/19f6791e0405d6578526b0e53b1af6dfb8aa37c81f012ff90af82c810011/locust-1.4.1-py3-none-any.whl (660kB)
100% |████████████████████████████████| 665kB 20kB/s
Collecting geventhttpclient>=1.4.4 (from locust)
Downloading https://files.pythonhosted.org/packages/fa/d5/c1d493214e1b236246aeba60d583033fd50f82b6159ef7b9d68836ae4308/geventhttpclient-1.4.5-cp37-cp37m-win32.whl
Collecting Flask-BasicAuth>=0.2.0 (from locust)
Downloading https://files.pythonhosted.org/packages/16/18/9726cac3c7cb9e5a1ac4523b3e508128136b37aadb3462c857a19318900e/Flask-BasicAuth-0.2.0.tar.gz
Requirement already satisfied: requests>=2.9.1 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from locust) (2.25.1)
Requirement already satisfied: psutil>=5.6.7 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from locust) (5.8.0)
Requirement already satisfied: flask>=1.1.2 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from locust) (1.1.2)
Requirement already satisfied: pyzmq>=16.0.2 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from locust) (20.0.0)
Collecting gevent>=20.9.0 (from locust)
Downloading https://files.pythonhosted.org/packages/97/04/cfa31cdec4bfa898b33f588d29cbae2b284a4402f0bfb1bdb1c7451458eb/gevent-20.12.1-cp37-cp37m-win32.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 33kB/s
Requirement already satisfied: msgpack>=0.6.2 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from locust) (1.0.2)
Collecting pywin32; sys_platform == "win32" (from locust)
Downloading https://files.pythonhosted.org/packages/2e/4e/aa126480d86d448924453ec77421ae97b558c650468c0cb9bc5d693058bf/pywin32-300-cp37-cp37m-win32.whl (8.5MB)
100% |████████████████████████████████| 8.5MB 62kB/s
Requirement already satisfied: ConfigArgParse>=1.0 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from locust) (1.2.3)
Requirement already satisfied: Werkzeug>=1.0.1 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from locust) (1.0.1)
Requirement already satisfied: certifi in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from geventhttpclient>=1.4.4->locust) (2020.12.5)
Requirement already satisfied: six in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from geventhttpclient>=1.4.4->locust) (1.15.0)
Requirement already satisfied: chardet<5,>=3.0.2 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from requests>=2.9.1->locust) (4.0.0)
Requirement already satisfied: idna<3,>=2.5 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from requests>=2.9.1->locust) (2.10)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from requests>=2.9.1->locust) (1.26.2)
Requirement already satisfied: click>=5.1 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from flask>=1.1.2->locust) (7.1.2)
Requirement already satisfied: itsdangerous>=0.24 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from flask>=1.1.2->locust) (1.1.0)
Requirement already satisfied: Jinja2>=2.10.1 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from flask>=1.1.2->locust) (2.11.2)
Requirement already satisfied: greenlet<2.0,>=0.4.17; platform_python_implementation == "CPython" in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from gevent>=20.9.0->locust) (0.4.17)
Requirement already satisfied: setuptools in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from gevent>=20.9.0->locust) (51.1.2)
Collecting zope.event (from gevent>=20.9.0->locust)
Downloading https://files.pythonhosted.org/packages/9e/85/b45408c64f3b888976f1d5b37eed8d746b8d5729a66a49ec846fda27d371/zope.event-4.5.0-py2.py3-none-any.whl
Requirement already satisfied: cffi>=1.12.2; platform_python_implementation == "CPython" and sys_platform == "win32" in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from gevent>=20.9.0->locust) (1.14.4)
Collecting zope.interface (from gevent>=20.9.0->locust)
Downloading https://files.pythonhosted.org/packages/90/a8/ffcf229570b7c93a700a8e6f6bb39975278f86bdc2db960280cba1fc23fb/zope.interface-5.2.0-cp37-cp37m-win32.whl (194kB)
100% |████████████████████████████████| 194kB 26kB/s
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from Jinja2>=2.10.1->flask>=1.1.2->locust) (1.1.1)
Requirement already satisfied: pycparser in c:\users\lizm\appdata\local\programs\python\python37-32\lib\site-packages (from cffi>=1.12.2; platform_python_implementation == "CPython" and sys_platform == "win32"->gevent>=20.9.0->locust) (2.20)
locustio 0.14.6 has requirement gevent==20.4.0, but you'll have gevent 20.12.1 which is incompatible.
Installing collected packages: zope.event, zope.interface, gevent, geventhttpclient, Flask-BasicAuth, pywin32, locust
Found existing installation: gevent 20.4.0
Uninstalling gevent-20.4.0:
Successfully uninstalled gevent-20.4.0
Running setup.py install for Flask-BasicAuth ... done
Successfully installed Flask-BasicAuth-0.2.0 gevent-20.12.1 geventhttpclient-1.4.5 locust-1.4.1 pywin32-300 zope.event-4.5.0 zope.interface-5.2.0
C:\Users\lizm\AppData\Local\Programs\Python\Python37-32\Scripts>
b通过GitHub上克隆项目安装(Python3推荐):https://github.com/locustio/locust
方式二:GitHub下载安装
GitHub项目地址:https://github.com/locustio/locust/
将项目克隆下来,通过Python 执行 setup.py 文件
...\locust> python setup.py install
running install
running bdist_egg
running egg_info
creating locustio.egg-info
writing locustio.egg-info\PKG-INFO
writing dependency_links to locustio.egg-info\dependency_links.txt
writing entry points to locustio.egg-info\entry_points.txt
writing requirements to locustio.egg-info\
requires.txt
writing top-level names to locustio.egg-info\top_level.txt
writing manifest file
'locustio.egg-info\SOURCES.txt'
3.4 安装pyzmq
If you intend to run Locust distributed across multiple processes/machines, we recommend you to also install pyzmq.
如果你打算运行Locust 分布在多个进程/机器,我们建议你也安装pyzmq.
通过pip命令安装。 /> pip install pyzmq
3.5 安装成功
CMD敲入命令验证。 /> locust --help
C:\Users\lizm\AppData\Local\Programs\Python\Python37-32\Scripts>locust -V
locust 1.4.1
C:\Users\lizm\AppData\Local\Programs\Python\Python37-32\Scripts>locust -h
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. Primarily used
together with --headless. Can be changed during a test
by inputs w, W(spawn 1, 10 users) and s, S(stop 1, 10
users)
-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. Defaults to run forever.
-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
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)
C:\Users\lizm\AppData\Local\Programs\Python\Python37-32\Scripts>
4 Locust使用
4.1 Locust 创建性能测试
4.2 Locust no-web模式
4.3 Locust 参数说明
未完待续