谈到性能测试工具,我们首先想到的是LoadRunner或JMeter。LoadRunner是非常有名的商业性能测试工具,功能非常强大。但现在一般不推荐使用该工具来进行性能测试,主要是使用也较为复杂,而且该工具体积比较大,需要付费且价格不便宜。
JMeter同样是非常有名的开源性能测试工具,功能也很完善,我们之前介绍了它作为接口测试工具的使用。
Locust同样是性能测试工具,虽然官方这样来描述它:“An open source load testing tool.”,但它和前面两个工具有一些不同。
Locust完全基于Python编程语言,采用纯 Python描述测试脚本,并且HTTP请求完全基于Requests
库。除了HTTP/HTTPS
协议外,Locust还可以测试其他协议的系统,只需采用Python调用对应的库进行请求描述即可。
LoadRunner和JMeter这类采用进程
和线程
的测试工具,都很难在单机上模拟出较高的并发压力。Locust的并发机制摒弃了进程和线程,采用协程
(gevent)的机制。协程避免了系统级资源调度,因此可以大幅提高单机的并发能力。
官网地址:https://www.locust.io
使用pip命令安装Locust:
pip install locustio
安装完成之后检测是否安装成功:
C:\Users\Shuqing>locust -help
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
....
针对如下两个接口进行性能测试:
http://127.0.0.1:8000/users/
http://127.0.0.1:8000/groups/
以上两个接口也就是我们之前项目django_restful
的接口
负载场景
restful_api_locust.py
from locust import HttpLocust,TaskSet,task
class UserBehavior(TaskSet):
@task(2)
def test_users(self):
self.client.get("/users/",auth=('51zxw','zxw20182018'))
@task(1)
def test_groups(self):
self.client.get("/groups/",auth=('51zxw','zxw20182018'))
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 3000
max_wait = 6000
UserBehavior类继承TaskSet类,用于描述用户行为。
@task
装饰该方法为一个事务,后面的数字表示请求比例,上面的比例为2:1
默认都是1:1
test_users()
方法表示一个用户行为,这里是请求user
接口。test_groups()
方法表示请求group
接口。client.get()
用于指定请求的路径。WebsiteUser类用于设置性能测试。
task_set
:指向一个定义的用户行为类。min_wait
:执行事务之间用户等待时间的下界(单位:毫秒)。max_wait
:执行事务之间用户等待时间的上界(单位:毫秒)。使用如下命令开始启动测试
C:\Users\Shuqing>locust -f D:\api_test\locust\restful_api_locust.py --host=http://127.0.0.1:8000
[2018-07-24 15:39:22,917] LAPTOP-8B5JADC8/INFO/locust.main: Starting web monitor at *:8089
[2018-07-24 15:39:22,917] LAPTOP-8B5JADC8/INFO/locust.main: Starting Locust 0.8.1
在浏览器打开localhost:8089
可以看到如下页面:
运行之后可以看到主界面如下:
性能测试参数如下。
点击 Charts
菜单可以查看性能图表
图表含义如下:
如果想对如下接口进行并发测试,则可以将id
进行参数化设置
http://127.0.0.1:8000/groups/1/
http://127.0.0.1:8000/groups/2/
http://127.0.0.1:8000/users/1/
http://127.0.0.1:8000/users/2/
locust_users_groups.py
from locust import HttpLocust,TaskSet,task
class UserBehavior(TaskSet):
def on_start(self):
#设置user和group参数下标初始值
self.users_index=0
self.groups_index=0
@task
def test_users(self):
#读取参数
users_id=self.locust.id[self.users_index]
url="/users/"+str(users_id)+'/'
self.client.get(url,auth=('51zxw','zxw20182018'))
#取余运算循环遍历参数
self.users_index=(self.users_index+1)%len(self.locust.id)
@task
def test_groups(self):
#参数化
groups_id=self.locust.id[self.groups_index]
url="/groups/"+str(groups_id)+"/"
self.client.get(url,auth=('51zxw','zxw20182018'))
self.groups_index=(self.groups_index+1)%len(self.locust.id)
class WebsiteUser(HttpLocust):
task_set = UserBehavior
#参数配置
id=[1,2]
min_wait = 3000
max_wait = 6000
#host配置
host = 'http://127.0.0.1:8000'
执行如下命令即可运行测试
C:\Users\Shuqing>locust -f D:\api_test\locust\locust_users_groups.py
前面是通过登录web来运行测试的,其实也可以非web状态来运行,如cmd命令来运行。如果需要非Web形式运行,则需使用--no-web
参数,并会用到如下几个参数。
-c, --clients
:指定并发用户数;-r, --hatch-rate
:指定并发加压速率,默认值位1。-t, --run-time
:设置运行时间。如(300s,20m, 3h, 1h30m等);运行命令如下:
locust -f D:\api_test\locust\locust_users_groups.py --no-web -c 10 -r 2 -t 15s
运行结果如下:
C:\Users\Shuqing>locust -f D:\api_test\locust\locust_users_groups.py --no-web -c 10 -r 2 -t 15s
[2018-08-21 10:12:59,017] LAPTOP-8B5JADC8/INFO/locust.main: Run time limit set to 15 seconds
[2018-08-21 10:12:59,017] LAPTOP-8B5JADC8/INFO/locust.main: Starting Locust 0.8
[2018-08-21 10:12:59,018] LAPTOP-8B5JADC8/INFO/locust.runners: Hatching and swarming 10 clients at the rate 2 clients/s...
Name # reqs # fails Avg Min Max | Median req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
Total 0 0(0.00%) 0.00
Name # reqs # fails Avg Min Max | Median req/s
--------------------------------------------------------------------------------------------------------------------------------------------
GET /groups/1/ 2 0(0.00%) 134 122 146 | 120 0.00
GET /users/1/ 2 0(0.00%) 118 100 136 | 100 0.00
--------------------------------------------------------------------------------------------------------------------------------------------
Total 4 0(0.00%) 0.00
Name # reqs # fails Avg Min Max | Median req/s
--------------------------------------------------------------------------------------------------------------------------------------------
GET /groups/1/ 4 0(0.00%) 135 122 146 | 130 1.00
GET /users/1/ 4 0(0.00%) 115 100 136 | 100 1.00
--------------------------------------------------------------------------------------------------------------------------------------------
Total 8 0(0.00%) 2.00
[2018-08-21 10:13:04,034] LAPTOP-8B5JADC8/INFO/locust.runners: All locusts hatched: WebsiteUser: 10
[2018-08-21 10:13:04,034] LAPTOP-8B5JADC8/INFO/locust.runners: Resetting stats
Name # reqs # fails Avg Min Max | Median req/s
--------------------------------------------------------------------------------------------------------------------------------------------
GET /groups/1/ 1 0(0.00%) 147 147 147 | 150 0.00
GET /users/1/ 2 0(0.00%) 110 92 128 | 92 0.00
GET /users/2/ 1 0(0.00%) 102 102 102 | 100 0.00
--------------------------------------------------------------------------------------------------------------------------------------------
Total 4 0(0.00%) 0.00
Name # reqs # fails Avg Min Max | Median req/s
--------------------------------------------------------------------------------------------------------------------------------------------
GET /groups/1/ 3 0(0.00%) 129 101 147 | 140 0.00
GET /users/1/ 3 0(0.00%) 108 92 128 | 100 0.00
GET /users/2/ 1 0(0.00%) 102 102 102 | 100 0.00
--------------------------------------------------------------------------------------------------------------------------------------------
Total 7 0(0.00%) 0.00
Name # reqs # fails Avg Min Max | Median req/s
--------------------------------------------------------------------------------------------------------------------------------------------
GET /groups/1/ 4 0(0.00%) 125 101 147 | 110 1.00
GET /groups/2/ 2 0(0.00%) 136 116 156 | 120 0.00
GET /users/1/ 3 0(0.00%) 108 92 128 | 100 1.00
GET /users/2/ 2 0(0.00%) 102 102 102 | 100 0.33
--------------------------------------------------------------------------------------------------------------------------------------------
Total 11 0(0.00%) 2.33
Name # reqs # fails Avg Min Max | Median req/s
--------------------------------------------------------------------------------------------------------------------------------------------
GET /groups/1/ 5 0(0.00%) 123 101 147 | 120 0.75
GET /groups/2/ 3 0(0.00%) 124 100 156 | 120 0.50
GET /users/1/ 3 0(0.00%) 108 92 128 | 100 0.75
GET /users/2/ 4 0(0.00%) 114 102 153 | 100 0.25
--------------------------------------------------------------------------------------------------------------------------------------------
Total 15 0(0.00%) 2.25
Name # reqs # fails Avg Min Max | Median req/s
--------------------------------------------------------------------------------------------------------------------------------------------
GET /groups/1/ 6 0(0.00%) 128 101 157 | 120 0.67
GET /groups/2/ 5 0(0.00%) 127 100 156 | 120 0.33
GET /users/1/ 4 0(0.00%) 108 92 128 | 100 0.50
GET /users/2/ 5 0(0.00%) 121 102 153 | 100 0.50
--------------------------------------------------------------------------------------------------------------------------------------------
Total 20 0(0.00%) 2.00
[2018-08-21 10:13:13,691] LAPTOP-8B5JADC8/INFO/locust.main: Time limit reached. Stopping Locust.
[2018-08-21 10:13:13,693] LAPTOP-8B5JADC8/INFO/locust.main: Shutting down (exit code 0), bye.
Name # reqs # fails Avg Min Max | Median req/s
--------------------------------------------------------------------------------------------------------------------------------------------
GET /groups/1/ 6 0(0.00%) 128 101 157 | 120 0.67
GET /groups/2/ 5 0(0.00%) 127 100 156 | 120 0.33
GET /users/1/ 4 0(0.00%) 108 92 128 | 100 0.50
GET /users/2/ 5 0(0.00%) 121 102 153 | 100 0.50
--------------------------------------------------------------------------------------------------------------------------------------------
Total 20 0(0.00%) 2.00
Percentage of the requests completed within given times
Name # reqs 50% 66% 75% 80% 90% 95% 98% 99% 100%
--------------------------------------------------------------------------------------------------------------------------------------------
GET /groups/1/ 6 140 140 150 150 160 160 160 160 157
GET /groups/2/ 5 120 150 150 160 160 160 160 160 156
GET /users/1/ 4 110 110 130 130 130 130 130 130 128
GET /users/2/ 5 100 150 150 150 150 150 150 150 153
--------------------------------------------------------------------------------------------------------------------------------------------
Total 20 120 140 150 150 160 160 160 160 157
上面我们都是单台机器来执行性能测试,但是当单台机器不够模拟足够多的用户时,Locust支持运行在多台机器中进行压力测试。分布式运行一般是一台master
多台slave
如下图所示:
首先定义一台 master
C:\Users\Shuqing>locust -f D:\api_test\locust\locust_users_groups.py --master
然后定义 slave
C:\Users\Shuqing>locust -f D:\api_test\locust\locust_users_groups.py --slave
如果slave
与master
不在同一台机器上,还需要通过--master-host
参数再指定master
的IP地址。
C:\Users\Shuqing>locust -f D:\api_test\locust\locust_users_groups.py --slave --master-host <master_ip>
运行之后可以看到web界面显示的SLAVES
数量