引言
前面已经介绍Locust基本使用与案例分享,脚本已经写好后,我们仅仅只是完成了一小部分的工作,对于监控到的数据展示以及后面的数据准确性分析、问题定位等等,我们并没有讲到。该篇简单分享一下Locust界面上面基本知识与使用。
UI界面
这里就不重复讲解脚本中的代码了,如果不懂,建议从头看起。
脚本如下:
from locust import TaskSet,HttpLocust,between,task class TestLogin(TaskSet): def setup(self): print("开始...") def teardown(self): print("结束...") def _login(self): url = '/api/private/v1/login' body = { "username": "admin", "password": "123456" } r = self.client.post(url=url, data=body) print("测试") print("响应数据是:%s"%r.text) assert "登录成功" in r.text return r.json()['data']['token'] def logout(self): print("退出系统") @task(1) def goods_list(self): url = '/api/private/v1/goods' params = { "pagenum":1, "pagesize":10 } headers = { 'Authorization': self._login() } r = self.client.get(url=url,params=params,headers=headers) print(r.text) print("获取商品列表") @task(1) def user_list(self): url = '/api/private/v1/users' params = { 'pagenum':1, 'pagesize':1 } headers = { 'Authorization':self._login() } r = self.client.get(url=url,params=params,headers=headers) print(r.text) print("获取用户列表") def on_start(self): """ on_start is called when a Locust start before any task is scheduled """ print("启动") self._login() def on_stop(self): """ on_stop is called when the TaskSet is stopping """ print("停止") self.logout() import random class WebsiteUser(HttpLocust): task_set = TestLogin def setup(self): print('locust setup') def teardown(self): print('locust teardown') wait_time = between(5,6) wait_function = lambda self: random.expovariate(1) * 1000 if __name__ == '__main__': import os os.system('locust -f locustfile_pro.py --host=http://127.0.0.1:8899 --port=8084')
运行结果:
[2020-06-24 00:58:40,686] WIN10-804191526/INFO/locust.main: Starting web monitor at http://*:8084 [2020-06-24 00:58:40,686] WIN10-804191526/INFO/locust.main: Starting Locust 0.14.6 [2020-06-24 00:58:43,586] WIN10-804191526/INFO/locust.runners: Hatching and swarming 1 users at the rate 1 users/s (0 users already running)... [2020-06-24 00:58:43,586] WIN10-804191526/ERROR/stderr: d:python3libsite-packageslocustutildeprecation.py:12: DeprecationWarning: Usage of wait_function is deprecated since version 0.13. Declare a WebsiteUser.wait_time method instead (should return seconds and not milliseconds) "(should return seconds and not milliseconds)" % type(locust_or_taskset).__name__, DeprecationWarning) [2020-06-24 00:58:43,586] WIN10-804191526/INFO/stdout: locust setup [2020-06-24 00:58:43,586] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:43,587] WIN10-804191526/INFO/locust.runners: All locusts hatched: WebsiteUser: 1 (0 already running) [2020-06-24 00:58:43,587] WIN10-804191526/INFO/stdout: 开始... [2020-06-24 00:58:43,587] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:43,587] WIN10-804191526/INFO/stdout: 启动 [2020-06-24 00:58:43,587] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:43,668] WIN10-804191526/INFO/stdout: 测试 [2020-06-24 00:58:43,668] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:43,668] WIN10-804191526/INFO/stdout: 响应数据是:{"data":{"id":500,"rid":0,"username":"admin","mobile":"12345678","email":"adsfad@qq.com","token":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1OTI5MzE1MjMsImV4cCI6MTU5MzAxNzkyM30.Jzs3jsLsmCkHozXhZueadEdiilJ1aHBYPHn2t8lARgQ"},"meta":{"msg":"登录成功","status":200}} [2020-06-24 00:58:43,668] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:43,743] WIN10-804191526/INFO/stdout: 测试 [2020-06-24 00:58:43,743] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:43,743] WIN10-804191526/INFO/stdout: 响应数据是:{"data":{"id":500,"rid":0,"username":"admin","mobile":"12345678","email":"adsfad@qq.com","token":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1OTI5MzE1MjMsImV4cCI6MTU5MzAxNzkyM30.Jzs3jsLsmCkHozXhZueadEdiilJ1aHBYPHn2t8lARgQ"},"meta":{"msg":"登录成功","status":200}} [2020-06-24 00:58:43,743] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:43,755] WIN10-804191526/INFO/stdout: {"data":{"total":926,"pagenum":"1","goods":[{"goods_id":926,"cat_id":null,"goods_name":"【海外购自营】黎珐(ReFa) MTG日本 CARAT铂金微电流瘦脸瘦身提拉紧致V脸美容仪 【保税仓发货】","goods_price":1399,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514345477,"upd_time":1514345477,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null},{"goods_id":925,"cat_id":null,"goods_name":"卡奇莱德汽车车载空气净化器负离子除甲醛PM2.5除烟异味车用氧吧双涡轮出风(红色)","goods_price":168,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514259452,"upd_time":1514259452,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null},{"goods_id":924,"cat_id":null,"goods_name":"沿途(yantu)车载充电器车充一拖二usb转接口手机智能头多功能汽车点烟器","goods_price":0,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514259448,"upd_time":1514259448,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null},{"goods_id":923,"cat_id":null,"goods_name":"车载冰箱7.5L 冷暖两用汽车冰箱半导体12V迷你电冰箱升级款","goods_price":149,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514259445,"upd_time":1514259445,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null},{"goods_id":922,"cat_id":null,"goods_name":"神行者电子狗 神行者L70电子狗测速 测速雷达 流动测速 多种警示路段提醒","goods_price":98,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514259442,"upd_time":1514259442,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null},{"goods_id":921,"cat_id":null,"goods_name":"360行车记录仪尊享升级版 J501C 安霸A12 高清夜视 WIFI连接 智能管理 机卡套装 黑色(存储卡品牌随机)","goods_price":359,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514259438,"upd_time":1514259438,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null},{"goods_id":920,"cat_id":null,"goods_name":"淘尔杰汽车专用伸缩蜡拖 除尘车掸子擦车拖把 洗车蜡刷清洁工具用品","goods_price":39,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514259435,"upd_time":1514259435,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null},{"goods_id":919,"cat_id":null,"goods_name":"保赐利(botny) 防雾剂汽车用长效雾清车窗挡风玻璃浴室去雾剂除雾剂玻璃清洁剂 B-2113","goods_price":0,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514259432,"upd_time":1514259432,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null},{"goods_id":918,"cat_id":null,"goods_name":"龟牌硬壳蜡新汽车防护划痕去污上光抛光养护漆固体洗车打蜡腊正品","goods_price":49,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514259429,"upd_time":1514259429,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null},{"goods_id":917,"cat_id":null,"goods_name":"点缤汽车车漆划痕修复补漆笔漆面修复油漆修补套装珠光珍珠白色点漆笔--单支补漆笔","goods_price":14,"goods_number":100,"goods_weight":100,"goods_state":0,"add_time":1514259426,"upd_time":1514259426,"hot_mumber":0,"is_promote":false,"cat_one_id":null,"cat_two_id":null,"cat_three_id":null}]},"meta":{"msg":"获取成功","status":200}} [2020-06-24 00:58:43,755] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:43,755] WIN10-804191526/INFO/stdout: 获取商品列表 [2020-06-24 00:58:43,755] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:49,428] WIN10-804191526/INFO/stdout: 测试 [2020-06-24 00:58:49,428] WIN10-804191526/INFO/stdout: [2020-06-24 00:58:49,428] WIN10-804191526/INFO/stdout: 响应数据是:{"data":{"id":500,"rid":0,"username":"admin","mobile":"12345678","email":"adsfad@qq.com","token":"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE1OTI5MzE1MjksImV4cCI6MTU5MzAxNzkyOX0.5CPSH2efZE34jSQOrZxL7mp0nQ1jhWQAjMFGev1FNlU"},"meta":{"msg":"登录成功","status":200}} [2020-06-24 00:58:49,428] WIN10-804191526/INFO/stdout:
启动界面
名词解析:
Number of users to simulate:设置模拟的用户总数
Hatch rate (users spawned/second):每秒启动的虚拟用户数
Start swarming:执行locust脚本
测试监控界面
Statistics类似Jmeter中的聚合报告
名词解析:
Type:请求类型,即接口的请求方法;
Name:请求名称;
requests:当前已完成的请求数量;
fails:当前失败的数量;
Median:响应时间的中间值,即50%的响应时间在这个数值范围内,单位为毫秒;
90%ile:根据正态分布,90%的响应时间在正态分布平均值下方,即小于这个值;
Average:平均响应时间,单位为毫秒;
Min:最小响应时间,单位为毫秒;
Max:最大响应时间,单位为毫秒;
average Size:平均每个请求的数据量,单位为字节;
current RPS(requests per second):每秒钟处理请求的数量,即RPS。(以前叫reqs/sec:每秒钟处理请求的数量,即QPS;),所以这两个可以看成一个。
Charts模块说明
测试结果变化趋势的曲线展示图,分别为每秒完成的请求数(RPS)、响应时间、不同时间的虚拟用户数。
RPS:
响应时间:
不同时间的用户数:
注意:这些图表没有持久性,如果刷新此页面,将创建新的图表。
Failtures模块说明
失败请求的展示界面。即服务器原因导致无法处理请求。
Exceptions模块说明
常请求的展示界面
Download Data
测试数据下载模块, 提供三种类型的CSV格式的下载,分别是:Statistics CSV、responsetime CSV、failures CSV、exceptions CSV。
Statistics CSV
statistics csv的数据信息和前面Statistics的是一致的。
responsetime CSV
responsetime CSV和Statistics介绍的90%ile类似,更精确地显示响应时间在各区间分布情况。
New Test模块说明
点击该按钮可对模拟的总虚拟用户数和每秒启动的虚拟用户数进行编辑。
Number of users to simulate:模拟的总虚拟用户数
Hatch rate (users spawned/second):每秒启动的虚拟用户数
HOST:测试的地址
总结
以上即为locust的介绍和简单使用以及说明,更多详细的内容请参考官方文档。。。有兴趣的朋友可以加入测开交流群进行沟通与学习,一起成长!