go安装与hey压测

背景说明

Jmeter(java语言开发的压测工具)的用户体验比较好,操作简单,能生成报表数据。在用JMeter排查k8s问题时,发现java虚拟机GC的时候,会产生错误的压测结果,导致错误的判断。因此,才采用hey压测工具,进行简单的压测,以确定容器云在组件升级过程中,服务是否中断。

适用范围

操作系统:centos7.3以上

使用说明

一.安装并配置golang

  1. 安装go和git
yum install -y go git
  1. 配置golang的环境变量
  • GOPATH对应的是go 下载的相关的依赖包所在地,类似于jar包的指定路径
mkdir -p /opt/addons/gopath
  • GOROOT和GOPATH指定的路径不要相同, 否则会报错“$GOPATH must not be set to $GOROOT”
cat >> ~/.bashrc <<EOF
#added by X.L.Xia
export GOROOT=/usr/bin/go
export GOPATH=/opt/addons/gopath
export PATH=\$PATH:\$GOROOT/bin
EOF
  • 启用配置
source ~/.bashrc
  • 检查golang的环境变量与版本
go version
go env

二.下载hey压测工具

go get -u github.com/rakyll/hey

三.hey压测简明示例

  • 指定时长的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

报告解读

  • hey参数说明
[sre@ecs-xxx ~]# hey --help
Usage: hey [options...] <url>

Options:
  // 指定运行的总请求数。默认值为200。
  -n  Number of requests to run. Default is 200.
  // 客户端并发执行的请求数,默认为50。总请求数不能小于并发数。
  -c  Number of workers to run concurrently. Total number of requests cannot
      be smaller than the concurrency level. Default is 50.
  // 客户端发送请求的速度限制,以每秒响应数QPS为单位,默认没有限制。
  -q  Rate limit, in queries per second (QPS) per worker. Default is no rate limit.
  // 发送请求的持续时长,超时后程序停止并退出。若指定了持续时间,则忽略总请求数(-n),例如-z 10s,-z 3m
  -z  Duration of application to send requests. When duration is reached,
      application stops and exits. If duration is specified, n is ignored.
      Examples: -z 10s -z 3m.
  // 输出类型。若没有提供,则打印摘要。CSV是唯一支持的格式,结果以逗号分隔各个指标项。
  -o  Output type. If none provided, a summary is printed.
      "csv" is the only supported alternative. Dumps the response
      metrics in comma-separated values format.
  // HTTP方法,例如GET,POST,PUT,DELETE,HEAD,OPTIONS方法
  -m  HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS.
  // HTTP请求头,可以用-H连续添加多个请求头。
  -H  Custom HTTP header. You can specify as many as needed by repeating the flag.
      For example, -H "Accept: text/html" -H "Content-Type: application/xml" .
  // 每个请求的超时时间(以秒为单位)。默认值为20s,数值0代表永不超时。
  -t  Timeout for each request in seconds. Default is 20, use 0 for infinite.
  // HTTP响应头
  -A  HTTP Accept header.
  // HTTP请求正文
  -d  HTTP request body.
  // HTTP请求正文文件
  -D  HTTP request body from file. For example, /home/user/file.txt or ./file.txt.
  // 内容类型,默认为“ text / html”。
  -T  Content-type, defaults to "text/html".
  // 基本身份验证,用户名:密码。
  -a  Basic authentication, username:password.
  // HTTP代理地址作, 使用host:port格式。
  -x  HTTP Proxy address as host:port.
  // 启用HTTP / 2
  -h2 Enable HTTP/2.
  // HTTP主机头
  -host	HTTP Host header.
  // 禁用压缩。
  -disable-compression  Disable compression.
  // 禁用保持活动状态,防止重新使用不同的HTTP请求之间的TCP连接。
  -disable-keepalive    Disable keep-alive, prevents re-use of TCP
                        connections between different HTTP requests.
  // 禁用HTTP重定向                      
  -disable-redirects    Disable following of HTTP redirects
  // 使用的cpu内核数。当前计算机的默认值为8核。
  -cpus                 Number of used cpu cores.
                        (default for current machine is 8 cores)
  • hey结果解读
[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

参考资料

1. 压测工具:hey
2. 一次WEB性能压测
3.几乎所有的WEB压力测试工具 WEB压力测试工具大全
4. 推荐一款高性能 HTTP 负载测试工具 Vegeta
5. 对 wrk Latency Distribution 不准确的分析
6. HTTP 状态码 常见及分析

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值