Locust 官方网站:https://www.locust.io/
压测工具有很多,例如shell下的ab,这里locust已经封装成python模块,且跨平台支持win、mac、linux系统,直接安装即可快速实现web界面压测数据可视化,谓之强大~
1、安装
那么既是python模块,老规矩
pip install locust
2、实战代码
2.1 服务器监听
话不多说,直接上代码,运行demo.py
# demo.py
import os
from locust import HttpUser, TaskSet, task
class WebsiteTasks(TaskSet):
def on_start(self):
self.client.post("/login", {
"username": "test",
"password": "123456"
})
@task(2)
def index(self):
self.client.get("/")
@task(1)
def about(self):
self.client.get("/about/")
class WebsiteUser(HttpUser):
# task_set = WebsiteTasks
tasks = [WebsiteTasks]
task_create = WebsiteTasks
host = "https://debugtalk.com"
min_wait = 1000
max_wait = 5000
if __name__ == '__main__':
os.system(r'locust -f demo.py')
运行之后,可见端口已经监听
2.2 web界面进行数据压测
默认情况下,我们监听的是本地localhost:8089,当然,这玩意也能改,不过这里咱们先按默认测一把。
浏览器输入地址:localhost:8089,输入这个三个参数,点击start
3、性能测试参数
-
Type: 请求的类型,例如GET/POST。
-
Name:请求的路径。这里为百度首页,即:https://www.baidu.com/
-
request:当前请求的数量。
-
fails:当前请求失败的数量。
-
Median:中间值,单位毫秒,一半的服务器响应时间低于该值,而另一半高于该值。
-
Average:平均值,单位毫秒,所有请求的平均响应时间。
-
Min:请求的最小服务器响应时间,单位毫秒。
-
Max:请求的最大服务器响应时间,单位毫秒。
-
Content Size:单个请求的大小,单位字节。
-
reqs/sec:是每秒钟请求的个数
官方文档:
- https://docs.locust.io/en/stable/what-is-locust.html
参考文档:
- https://debugtalk.com/post/head-first-locust-user-guide/
- http://www.testclass.net/locust/create-first-test