Golang的压测工具 hey


本文摘自网络,作者,侵删。

Golang的压测工具 hey

安装

go get -u github.com/rakyll/hey
go install github.com/rakyll/hey

注释

BenchmarkStringJoin1-4 300000 4351 ns/op 32 B/op 2 allocs/op
 
-4表示4个CPU线程执行;
300000表示总共执行了30万次;
4531ns/op,表示每次执行耗时4531纳秒;
32B/op表示每次执行分配了32字节内存;
2 allocs/op表示每次执行分配了2次对象
 
https://github.com/rakyll/hey
 
-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 secs
  Slowest:  0.0770 secs
  Fastest:  0.0067 secs
  // 平均响应时间
  Average:  0.0097 secs
  // 每秒响应数(QBS)
  Requests/sec: 3880.9152
  
  Total data:   454000 bytes
  Size/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 secs
  25% in 0.0077 secs
  50% in 0.0081 secs
  75% in 0.0088 secs
  90% in 0.0093 secs
  95% in 0.0106 secs
  // TP99:满足百分之九十九的网络请求所需要的最低耗时
  99% in 0.0634 secs

Details (average, fastest, slowest):
  DNS+dialup:   0.0010 secs, 0.0067 secs, 0.0770 secs
  DNS-lookup:   0.0001 secs, 0.0000 secs, 0.0076 secs
  req write:    0.0000 secs, 0.0000 secs, 0.0008 secs
  resp wait:    0.0083 secs, 0.0066 secs, 0.0769 secs
  resp 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

xy-icnc 测试例子

指定请求总数的get请求:运行2000次(-n),客户端并发为50(-c)
hey -n 2000 -c 50  http://192.168.21.224:8833/login

本文来自:简书

感谢作者:我是不会赢的

查看原文:Golang的压测工具 hey

相关阅读 >>

Golang gf怎么使用

Go语言gc实现、触发机制

Go html/template

[Go-linq]-Go的.net linq式查询方法

手撸Golang 创建型设计模式 建造者模式

Golang判断map是否存在key

Go 时间格式化 字符串格式化为时间格式

教你使用Golang实现redis服务器

一码理解函数是一等公民

Golang怎么判断map是否为空

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...