下载hey
wget https://hey-release.s3.us-east-2.amazonaws.com/hey_linux_amd64
赋予执行权限
chmod +x hey_linux_amd64
移动文件到sbin目录
mv hey_linux_amd64 /usr/sbin/hey
说明
用法:hey [选项…] <url>
选项:
-n 请求次数。默认为 200。
-c 并发工作线程数。请确保总请求数不小于并发数量。默认为 50。
-q 每个工作线程的查询每秒限制(QPS)。默认不设限制。
-z 应用发送请求的持续时间。达到指定时间后,应用将停止并退出。如果指定了该选项,则忽略-n选项。示例:-z 10s -z 3m。
-o 输出类型。如果未提供,则打印摘要。"csv" 是唯一支持的替代选项。将响应指标以逗号分隔值格式转储。
-m HTTP 方法,GET、POST、PUT、DELETE、HEAD、OPTIONS 之一。
-H 自定义 HTTP 标头。您可以通过重复该标志来指定多个标头。例如,-H "Accept:text/html" -H "Content-Type:application/xml"。
-t 每个请求的超时时间(秒)。默认为 20,使用 0 表示无限期。
-A HTTP Accept 标头。
-d HTTP 请求体。
-D 从文件中读取 HTTP 请求体。例如,/home/user/file.txt 或 ./file.txt。
-T 内容类型,默认为 "text/html"。
-a 基本身份验证,格式为:用户名:密码。
-x HTTP 代理地址,格式为 host:port。
-h2 启用 HTTP/2。
-host HTTP Host 标头。
-disable-compression 禁用压缩。
-disable-keepalive 禁用 keep-alive,防止在不同的 HTTP 请求之间重用 TCP 连接。
-disable-redirects 禁用 HTTP 重定向跟随。
-cpus 使用的 CPU 核心数。
(当前设备的默认值为 8 个核心
执行命令查看是否可用
./hey -t 2 -z 10s -c 1 http://127.0.0.1:80
总结:
总共时间:60.0265秒
最慢请求:1.2805秒
最快请求:0.0037秒
平均请求时间:0.0550秒
请求速率:908.6493次/秒
响应时间直方图:
0.004 [1] |
0.131 [50352] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
0.259 [3580] |■■■
0.387 [491] |
0.514 [65] |
0.642 [4] |
0.770 [0] |
0.897 [0] |
1.025 [0] |
1.153 [8] |
1.280 [42] |
延迟分布:
10%的请求在0.0092秒内完成
25%的请求在0.0323秒内完成
50%的请求在0.0365秒内完成
75%的请求在0.0544秒内完成
90%的请求在0.1134秒内完成
95%的请求在0.1811秒内完成
99%的请求在0.2732秒内完成
细节(平均值,最快值,最慢值):
DNS+拨号: 0.0000秒, 0.0037秒, 1.2805秒
DNS查找: 0.0000秒, 0.0000秒, 0.0000秒
请求写入: 0.0000秒, 0.0000秒, 0.0042秒
响应等待: 0.0549秒, 0.0037秒, 1.2780秒
响应读取: 0.0001秒, 0.0000秒, 0.0857秒
状态码分布:
[200] 有54543个响应。
参数介绍
> // 指定运行的总请求数。默认值为200。
> -n Number of requests to run. Default is 200.
> // 客户端并发执行的请求数,默认为50。总请求数不能小于并发数。
> -c Number of workers to runconcurrently. 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)
实例:
指定时长的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://1.1.1.1
请求带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
官方源码:
https://github.com/rakyll/hey