一、开发环境
Windows 10
python版本:3.7.6
二、安装locust
pip install locust
pip install pyzmq
PS D:\> pip install locust
PS D:\> locust -V
locust 1.4.3
三、编写性能测试脚本
创建 test.py 文件
from locust import HttpUser, task, TaskSet, between
class UserBehavior(TaskSet):
@task(1)
def task1(self):
self.client.get("/search/error.html")
@task(2)
def task2(self):
self.client.get("/")
class MyUser(HttpUser):
wait_time = between(0.5, 1)
host = "https://www.baidu.com"
tasks = [UserBehavior]
四、运行脚本
locust -f test.py :-f 指定性能测试脚本文件
通过浏览器访问locust服务:http://localhost:8089(Locust启动网络监控器,默认为端口号为: 8089)

image.png
- 参数说明:
(1)Number of users to simulate :模拟用户数
(2)Hatch rate(users spawned/second): 每秒启动的用户数
(3)Host:被测接口的ip地址或域名(带http://)
拓展说明: 当运行脚本时,加上参数step-load,就可以增加步长
此时再访问http://localhost:8089,就会出现步长参数:locust -f test.py --step-load
(1)Number of users to increase by step:逐步增加用户数
(2)Step duration:步长持续运行时间
点击 “Start swarming” 按钮,开始运行性能测试。
五、界面参数
Top栏

locust
- HOST
- STATUS:状态(两个用户正在运行)
- RPS:单位时间内处理的请求数
- FAILURES:失败率
- STOP:停止运行
- Reset Stats:重置
Statistics

image.png
| 参数 | 说明 |
|---|---|
| Type | 请求的类型,即接口请求方法,例如GET/POST |
| Name | 请求的路径 |
| request | 当前已完成的请求数量 |
| fails | 当前请求失败的数量 |
| Median | 响应时间的中间值,单位毫秒,一半的服务器响应时间低于该值,而另一半高于该值 |
| 90%ile (ms) | 90%的响应时间,单位毫秒 |
| Average | 平均响应时间,单位毫秒 |
| Min | 请求的最小服务器响应时间,单位毫秒 |
| Max | 请求的最大服务器响应时间,单位毫秒 |
| Average size (bytes) | 请求的平均大小,单位字节 |
| Current RPS | 当前的RPS |
| Current Failures/s | 当前每秒钟请求失败率 |
最后一行:aggregated为汇总数据
Charts
(1) 每秒响应请求数(RPS)+ 失败率

image.png
(2)请求响应时间

image.png
(3)虚拟用户数

image.png
(4)Failures:失败的请求

image.png
(5)Exceptions:异常的请求

image.png
(6)Download Data:下载测试数据

image.png
1. Download request statistics CSV
2. Download failures CSV
3. Download exceptions CSV
4. Download Report