转载*请注明原始出处:http://blog.csdn.net/a464057216/article/details/48391867
后续此博客不再更新,欢迎大家搜索关注微信公众号“测开之美”,测试开发工程师技术修炼小站,持续学习持续进步。
最近的测试任务中需要测试系统的并发处理能力,尝试了多进程、多线程、协程等多种方法,虽然最终也能把问题搞定,但是使用起来总觉得不方便。在网上查了一下了解到Locust这个负载测试工具,学习使用以后感觉不错,下面来介绍一下。
Locust是一个基于Python(Python 2.6+,不支持Python 3)的支持分布式的压力测试工具,能够测试系统的并发处理能力,可以用来测试Web服务或者其他业务。在测试过程中,会产生大量的用户(Locusts蝗群)对系统进行攻击,每个用户(Locust蝗虫)的行为是通过Python代码控制的,并且整个测试过程有一个Web的UI界面可以实时观察测试数据。
Locust的内部采用了协程gevent,虽然受到每个机器(Locust node)硬件的限制(比如文件描述符的数量)可能模拟的用户数是受限的,但是通过采用分布式的方式,理论上Locust工具能够模拟无数的用户.
安装Locust的命令:
$ pip install locustio
然后通过$locust -h如果看到帮助信息,证明安装成功:
mars@mars-Ideapad-V460:~$ locust -h
Usage: locust [options] [LocustClass [LocustClass2 ... ]]
Options:
-h, --help show this help message and exit
-H HOST, --host=HOST Host to load test in the following format:
http://10.21.32.33
--web-host=WEB_HOST Host to bind the web interface to. Defaults to '' (all
interfaces)
-P PORT, --port=PORT, --web-port=PORT
Port on which to run web host
-f LOCUSTFILE, --locustfile=LOCUSTFILE
Python module file to import, e.g. '../other.py'.
Default: locustfile
--master Set locust to run in distributed mode with this
process as master
--slave Set locust to run in distributed mode with this
process as slave
--master-host=MASTER_HOST
Host or IP address of locust master for distributed
load testing. Only used when running with --slave.
Defaults to 127.0.0.1.
--master-port=MASTER_PORT
The port to connect to that is used by the locust
master for distributed load testing. Only used when
running with --slave. Defaults to 5557. Note that
slaves will also connect to the master node on this
port + 1.
--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.
--no-web Disable the web interface, and instead start running
the test immediately. Requires -c and -r to be
specified.
-c NUM_CLIENTS, --clients=NUM_CLIENTS
Number of concurrent clients. Only used together with
--no-web
-r HATCH_RATE, --hatch-rate=HATCH_RATE
The rate per second in which clients are spawned. Only
used together with --no-web
-n NUM_REQUESTS, --num-request=NUM_REQUESTS
Number of requests to perform. Only used together with
--no-web
-L LOGLEVEL, --loglevel=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
--print-stats Print stats in the console
--only-summary Only print the summary stats
-l, --list Show list of possible locust classes and exit
--show-task-ratio print table of the locust classes' task execution
ratio
--show-task-ratio-json
print json data of the locust classes' task execution
ratio
-V, --version show program's version number and exit
如果需要分布式的Locust,需要安装pyzmq:
$ pip install pyzmq
安装的过程中可能会弹出很多warning等等,不用管他们,只要最后显示Successfully installed ...
就行.安装完成后,在Python解释器中能够成功import表示安装成功:
mars@mars-Ideapad-V460:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:38)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import zmq
>>>
注意:每一个HTTP连接都会占用一个文件描述符,所以如果模拟的用户数量超过了系统允许的最多文件描述符数量,Locust会报错。这种情况需要调整系统允许的最多文件描述符数量。
如果觉得我的文章对您有帮助,欢迎关注我(CSDN:Mars Loo的博客)或者为这篇文章点赞,谢谢!