在 Mac 上捣鼓了半天的压测工具,最后发现还是 wrk 最方便。Mac 、 Linux 压测推荐 wrk,Windows 压测推荐 SuperBenchmarker(简称sb),sb相关的可以看我这篇文章 可能是Windows下最好用的压测工具——SuperBenchmarker
一、安装
1.Mac 安装
brew install wrk
2.Ubuntu 安装
sudo apt install build-essential libssl-dev git unzip
从 github 拉取源代码
git clone git@github.com:wg/wrk.git
使用 make 编译源代码
cd wrk
make
把生成的wrk移到 PATH 目录下面
sudo cp wrk /usr/local/bin
二、使用
1.Get 请求压测
wrk -t12 -c400 -d30s http://127.0.0.1:8801/
参数说明:
--timeout
超时的时间-d
持续时间-c
,--connections
模拟连接数 (必须大于等于模拟线程数)-d
, --duration 测试持续时间-t
, --threads 模拟线程数-s
,--script
加载 lua 脚本-H
,--header
添加请求头--latency
打印延迟分布--timeout
超时时间-v
,--version
打印 wrk 版本细节
2.Post 请求压测
创建 post.lua
wrk.method = "POST"
wrk.body = '{请求 JSON}'
wrk.headers["Content-Type"] = "application/json"
使用 post.lua
发起请求
wrk -t12 -c400 -d10s --script=post.lua --latency http://localhost:9091/api/test
3.压测结果说明
Running 10s test @ http://localhost:9091/api/test
12 threads and 4000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 184.18ms 77.32ms 495.90ms 78.41%
Req/Sec 434.03 181.35 1.06k 63.75%
52078 requests in 10.09s, 33.18MB read//10秒52078个请求
Socket errors: connect 2987, read 0, write 0, timeout 0//连接错误 2987 个,读取错误 0,写入错误 0,超时错误 0
Latency Distribution
50% 175.26ms
75% 220.62ms
90% 313.56ms
99% 431.14ms
Requests/sec: 5162.35 //QPS
Transfer/sec: 3.29MB
(1)纵轴:
Latency:延迟
Req/Sec:每秒请求数
(2)横轴:
Avg:平均值
Stdev:标准差
Max:最大值
+/- Stdev:正负一个标准差所占比例
(3)延迟分布:
Latency Distribution (延迟分布)
50% 218.31ms
75% 520.60ms
90% 955.08ms
99% 1.93s
4922 requests in 30.06s, 73.86MB read (30.06s内处理了4922个请求,耗费流量73.86MB)
Socket errors: connect 0, read 0, write 0, timeout 311 (发生错误数)
Requests/sec: 163.76 (QPS 163.76,即平均每秒处理请求数为163.76)
Transfer/sec: 2.46MB (平均每秒流量2.46MB)
三、其他功能
如果只看这些信息我们还不能满足,还想看实际请求和响应 json。
还记得刚刚 2.1 POST 请求写的 lua 脚本吗?wrk 提供了4个方法可供调用
function init(args)
function delay()
function request()
function response(status, headers, body)
我们只需要在下方使用 lua 的语法打印
3.1 打印返回信息
wrk.method = "POST"
wrk.body = '{请求 JSON}'
wrk.headers["Content-Type"] = "application/json"
function response(status, headers, body)
print(body)
end
3.2 实现动态请求信息
wrk.method = "POST"
wrk.body = '{请求 JSON}'
wrk.headers["Content-Type"] = "application/json"
评论区