安装
go get -u github.com/rakyll/heygo install github.com/rakyll/hey
参数说明
Usage: hey [options...] <url>Options:-n 要运行的请求数。默认是200。-c 并发运行的请求数。请求的总数不能小于并发级别。默认是50。-q 速率限制,以每秒查询(QPS)为单位。默认没有限制。-z 发送请求的应用程序配置。当时间到了,应用程序停止并退出。如果指定持续时间,则忽略n。例子:- z 10s - z 3m。-o 输出类型。如果没有提供,则打印摘要。“csv”是唯一受支持的替代方案。转储文件的响应以逗号分隔值格式的度量。-m HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS.-H 自定义HTTP头。您可以通过重复标记指定所需的数量For example, -H "Accept: text/html" -H "Content-Type: application/xml"-t 每个请求的超时时间(以秒为单位)。默认值是20,使用0表示无穷大。-A HTTP Accept header.-d HTTP request body.-D HTTP request body from file. For example, /home/user/file.txt or ./file.txt.-T Content-type, defaults to "text/html".-a Basic authentication, username:password.-x HTTP Proxy address as host:port.-h2 Enable HTTP/2.-host HTTP Host header.-disable-compression 禁用压缩。-disable-keepalive 禁用keep-alive,防止重用TCP不同HTTP请求之间的连接。-disable-redirects 禁用HTTP重定向的后续操作-cpus 使用的cpu核数。(当前机器默认为48核)
hey -n 10 -c 2 -m POST -T “application/x-www-form-urlencoded” -d ‘username=1&message=hello’ http://your-rest-url/resource
结果解读
[sre@ecs-xxx ~]# hey -n 2000 -c 50 https://www.baidu.com/Summary:// 总运行时长Total: 0.5153 secsSlowest: 0.0770 secsFastest: 0.0067 secs// 平均响应时间Average: 0.0097 secs// 每秒响应数(QBS)Requests/sec: 3880.9152Total data: 454000 bytesSize/request: 227 bytes// 响应直方图Response time histogram:0.007 [1] |// 响应时长在0.007~0.014区间的请求总数为1940个0.014 [1940] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■0.021 [4] |0.028 [0] |0.035 [0] |0.042 [1] |0.049 [0] |0.056 [5] |0.063 [26]|■0.070 [22]|0.077 [1] |// http请求时延分布Latency distribution:10% in 0.0073 secs25% in 0.0077 secs50% in 0.0081 secs75% in 0.0088 secs90% in 0.0093 secs95% in 0.0106 secs// TP99:满足百分之九十九的网络请求所需要的最低耗时99% in 0.0634 secsDetails (average, fastest, slowest):DNS+dialup: 0.0010 secs, 0.0067 secs, 0.0770 secsDNS-lookup: 0.0001 secs, 0.0000 secs, 0.0076 secsreq write: 0.0000 secs, 0.0000 secs, 0.0008 secsresp wait: 0.0083 secs, 0.0066 secs, 0.0769 secsresp read: 0.0001 secs, 0.0000 secs, 0.0029 secs// http状态码分布Status code distribution:[200] 2000 responses
测试例子
- 指定时长的get请求:客户端(-c)并发为2, 持续发送请求2s (-c)
hey -z 5s -c 2 https://www.baidu.com/
- 指定请求总数的get请求:运行2000次(-n),客户端并发为50(-c)
hey -n 2000 -c 50 https://www.baidu.com/
- 指定host的get请求:使用的cpu核数为2 (-cpus), 压测时长为5s(-z), 并发数为2
hey -z 5s -c 2 -cpus 2 -host "baidu.com" https://220.181.38.148
- 请求带header的get接口:压测时长为5s (-z), 客户端发送请求的速度为128QPS, 请求头用-H添加
hey -z 5s -q 128 -H "client-ip:0.0.0.0" -H "X-Up-Calling-Line-Id:X.L.Xia"https://www.baidu.com/
- 请求post请求
hey -z 5s -c 50 -m POST -H "info:firstname=xiuli; familyname=xia" -d "year=2020&month=1&day=21"https://www.baidu.com/
- 代理模式,需额外配置proxy:因部分ip频繁发请求有风险,故可用-x设置白名单代理向服务器发请求
hey -z 5s -c 10 -x "http://127.0.0.1:8001" http://baidu.com/
- shell for循环实现压测
for i in `seq 10`; do curl -v http://baidu.com; done
- 指定请求总数的get请求:运行2000次(-n),客户端并发为50(-c) ```json
hey -n 2000 -c 50 http://192.168.21.224:8833/login ```
