CentOS 8 hey 安装与命令参数详解

在 CentOS 8 上用 Go 工具链安装 HTTP 压测工具 hey 0.1.5(Go 1.24.2),并逐参数实测 -n/-c/-z/-q/-t/-H/-m/-d/-T/-o 及 --help 的输入与回显,涵盖多版本安装与排障。

网络测试hey压测压测Linux命令行2026/10/09

CentOS 8 hey 安装与命令参数详解

目标:在 CentOS 8 上用 Go 工具链安装并运行 HTTP 负载压测工具 hey(rakyll/hey 0.1.5),并逐参数实测压测命令的输入与输出,涵盖 -n、-c、-z、-q、-t、-H、-m、-d、-T、-o(csv/json/histogram)、--help 等参数,以及多版本安装。本文命令与输出均为 CentOS 8(hey 0.1.5,Go 1.24.2)对目标站 http://172.29.0.5:8080/ 实测回显。

一、环境信息

  • 操作系统:CentOS Linux release 8.4.2105(x86_64)
  • hey 版本:0.1.5(github.com/rakyll/hey,go install 构建)
  • Go 工具链:go1.24.2 linux/amd64(位于 /opt/data/hey/go)
  • 压测客户端主机:本机,网卡 eth0,IP 172.29.0.3/16
  • 目标站点:172.29.0.5:8080(HTTP 服务,供压测)
  • 目标服务类型:Python http.server(SimpleHTTP,Python 3.6.8)
  • 测试资源:/index.html(20 字节,实测每请求响应 26 字节)
  • 工作目录:/opt/data/hey/

二、工具概述

hey 是一个基于 Go 编写的轻量 HTTP 负载压测工具,GitHub 项目 rakyll/hey。它用一个命令模拟指定数量的并发调用向目标 URL 发起请求,输出吞吐量、平均/最快/最慢响应时间、请求耗时直方图、延迟百分位分布、各阶段耗时明细与状态码分布,常用于对 Web 服务做简单的压测与性能评估。hey 自带的输出统计比 ab 更直观(含直方图与 Latency distribution),并发能力亦更强。本文在 CentOS 8 上安装 hey 0.1.5 并逐参数实测。

⚠️ 注意:hey 不提供 --version 参数(hey --version 会报 flag provided but not defined: -version),版本信息可通过 Go 自带的 go version -m 子命令从二进制元数据读出(见 3.2)。CentOS 8 官方仓库无 hey 软件包,只能通过 Go 工具链安装。

三、安装

hey 的官方发行说明中并不附带各平台预编译的二进制包(rakyll/hey 只发布源码),因此官方推荐且最通用的是用 Go 工具链以 go install 安装。CentOS 8 官方仓库没有 hey 的 rpm 包(实测 dnf list hey 因仓库元数据源不可达而报错,且无该包),故不使用 dnf。

3.1 前置:安装 Go 工具链

先确认本机是否有 Go;没有则从官方下载 Go 1.24.2 的 tar 包解压安装(测得本机 Go 为 go1.24.2):

cd /opt/data/hey
ls -l go1.24.2.tar.gz
tar -C /opt/data/hey -xzf go1.24.2.tar.gz
export PATH=/opt/data/hey/go/bin:$PATH
go version
go version go1.24.2 linux/amd64

💡 tar -C /opt/data/hey -xzf go1.24.2.tar.gz 会把 Go 解压到 /opt/data/hey/go,命令回显为空属正常。export PATH 只是当前会话生效;若希望长期可用,可写入 ~/.bashrc。

3.2 方法一(推荐):go install 安装 hey 0.1.5

用 go install github.com/rakyll/hey@v0.1.5(@v0.1.5 锁定版本号)把 hey 安装到 GOBIN 指定目录,再建软链接放入 PATH:

cd /opt/data/hey
GOBIN=/opt/data/hey/bin go install github.com/rakyll/hey@v0.1.5
ln -sf /opt/data/hey/bin/hey /usr/local/bin/hey
ls -l /opt/data/hey/bin/hey /usr/local/bin/hey
-rwxr-xr-x 1 root root 11763206 Oct  9 06:54 /opt/data/hey/bin/hey
lrwxrwxrwx 1 root root 21 Oct  9 06:54 /usr/local/bin/hey -> /opt/data/hey/bin/hey

⚠️ go install 需要从 Go 模块代理下载源码。实测本地默认 GOPROXY=https://proxy.golang.org,direct 在境内网络下无法连通(报 dial tcp 142.251.45.145:443: connect: connection refused);改用可用的代理镜像即可成功下载,例如加 GOPROXY=https://goproxy.cn,direct,或首次拉取过依赖后以模块缓存离线安装。安装成功时为静默成功(无回显),失败会在 go: 行打印错误。

验证版本(hey 自身无 --version,用 go version -m 读二进制构建元数据):

go version -m /usr/local/bin/hey | sed -n '1,3p'
hey --help
/usr/local/bin/hey: go1.24.2
        path    github.com/rakyll/hey
        mod     github.com/rakyll/hey   v0.1.5  h1:oc3QhpT8ETXcr5xIE2xgWYNSNA/Z52XA20ku9hWCchY=
Usage: hey [options...] <url>

Options:
  -n  Number of requests to run. Default is 200.

mod 行后的 v0.1.5 即 hey 版本;go1.24.2 为构建所用 Go 版本。hey --help 开头即打印 Usage: hey [options...] <url>(完整帮助见「五、帮助输出」)。

3.3 方法二(备用/多版本安装):go install 指定其他版本

@<版本号> 可安装任意指定版本,用于多版本并存或降级。实测安装 hey 0.1.4 到另一目录 bin2(真实回显含 Go 的下载日志):

mkdir -p /opt/data/hey/bin2
GOBIN=/opt/data/hey/bin2 GOPROXY=https://goproxy.cn,direct go install github.com/rakyll/hey@v0.1.4
go: downloading github.com/rakyll/hey v0.1.4
go: downloading golang.org/x/net v0.0.0-20181017193950-04a2e542c03f
go: downloading golang.org/x/text v0.3.0

验证并存两版:

go version -m /opt/data/hey/bin2/hey | sed -n '1,3p'
/opt/data/hey/bin2/hey: go1.24.2
        path    github.com/rakyll/hey
        mod     github.com/rakyll/hey   v0.1.4  h1:hhc8GIqHN4+rPFZvkM9lkCQGi7da0sINM83xxpFkbPA=

💡 需要切换版本时,把 bin 或 bin2 下的 hey 软链到 /usr/local/bin/hey 即可;同一 GOBIN 目录下后装的版本会覆盖先装的同名二进制。

3.4 卸载与清理

sudo rm -f /usr/local/bin/hey
rm -rf /opt/data/hey

四、命令实测

实测约定:目标均为 http://172.29.0.5:8080/(HTTP 服务,返回 200 与 index.html),/index.html 为 20 字节小页面(每响应 26 字节)。以下输出均为逐条真实回显;Response time histogram 的直方图按终端宽度缩放,正文保留实测的桶值与柱条形态。

4.1 -n 请求数

用途:-n 指定要发起的总请求数(Default is 200)。这是最常用的入门命令。

参数说明:-n Number of requests to run. Default is 200.

hey -n 500 -c 50 http://172.29.0.5:8080/index.html
Summary:
  Total:        7.6711 secs
  Slowest:      7.6580 secs
  Fastest:      0.0007 secs
  Average:      0.2380 secs
  Requests/sec: 65.1798

  Total data:   13000 bytes
  Size/request: 26 bytes

Response time histogram:
  0.001 [1]   |
  0.766 [455] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  1.532 [20]  |■■
  2.298 [8]   |■
  3.064 [5]   |
  3.829 [0]   |
  4.595 [6]   |■
  5.361 [0]   |
  6.127 [0]   |
  6.892 [0]   |
  7.658 [5]   |

Latency distribution:
  10% in 0.0013 secs
  25% in 0.0014 secs
  50% in 0.0016 secs
  75% in 0.0020 secs
  90% in 0.0039 secs
  95% in 1.4816 secs
  99% in 7.6570 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0936 secs, 0.0001 secs, 1.0652 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0001 secs, 0.0000 secs, 0.0021 secs
  resp wait:  0.1442 secs, 0.0003 secs, 6.5937 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0007 secs

Status code distribution:
  [200]       500 responses

解读:Summary 给出总耗时 Total=7.6711s、最快/最慢/平均响应与吞吐 Requests/sec=65.1798;Total data=13000 = 26 字节 × 500。Response time histogram 是响应时间直方图(含桶起点与落入该桶的请求数,如 455 个请求在 0.766s 档),Latency distribution 给出分位数(如 P50=1.6ms、P95=1.4816s,长尾明显),Details 细分各阶段耗时,Status code distribution 显示 [200] 500 responses 表示 500 次全部成功返回 200。

4.2 -c 并发

用途:-c 指定同时工作的并发 worker 数(Default is 50)。并发数应不大于总请求数。

参数说明:-c Number of workers to run concurrently. Total number of requests cannot be smaller than the concurrency level. Default is 50.

hey -n 1000 -c 100 http://172.29.0.5:8080/index.html
Summary:
  Total:        22.2591 secs
  Slowest:      14.4219 secs
  Fastest:      0.0004 secs
  Average:      0.3002 secs
  Requests/sec: 44.9255

  Total data:   25038 bytes
  Size/request: 26 bytes

Response time histogram:
  0.000 [1]   |
  1.443 [908] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  2.885 [33]  |■
  4.327 [0]   |
  5.769 [8]   |
  7.211 [0]   |
  8.653 [6]   |
  10.095 [0]  |
  11.538 [0]  |
  12.980 [0]  |
  14.422 [7]  |

Latency distribution:
  10% in 0.0013 secs
  25% in 0.0015 secs
  50% in 0.0017 secs
  75% in 0.0020 secs
  90% in 0.0078 secs
  95% in 1.4625 secs
  99% in 7.6375 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.1071 secs, 0.0001 secs, 2.0348 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0001 secs, 0.0000 secs, 0.0029 secs
  resp wait:  0.1929 secs, 0.0002 secs, 13.3778 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0009 secs

Status code distribution:
  [200]       963 responses

Error distribution:
  [37]        Get "http://172.29.0.5:8080/index.html": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

解读:并发提到 100 后服务端排队严重:Total=22.26s、吞吐降到 44.9 请求/秒,resp wait 平均 0.1929s、最慢 13.38s,超默认 20s 的部分被判定超时产生 37 个错误(见 Error distribution),成功 [200] 963 次、总请求 1000 = 963 + 37。说明该 http.server 的软并发上限很低,压测高并发时目标和客户端(-t 超时、-c 并发)都要结合实际性能调整。

4.3 -z 压测时长

用途:-z 按固定时长(而非固定请求数)压测,到时长即停止;一旦指定 -z,-n 会被忽略。

参数说明:-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.

hey -z 5s -c 10 http://172.29.0.5:8080/index.html
Summary:
  Total:        5.4908 secs
  Slowest:      2.0482 secs
  Fastest:      0.0005 secs
  Average:      0.0026 secs
  Requests/sec: 3524.2270

  Total data:   503126 bytes
  Size/request: 26 bytes

Response time histogram:
  0.000 [1]   |
  0.205 [19331] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.410 [1]   |
  0.615 [0]   |
  0.820 [0]   |
  1.024 [9]   |
  1.229 [7]   |
  1.434 [0]   |
  1.639 [0]   |
  1.843 [0]   |
  2.048 [2]   |

Latency distribution:
  10% in 0.0013 secs
  25% in 0.0014 secs
  50% in 0.0015 secs
  75% in 0.0017 secs
  90% in 0.0019 secs
  95% in 0.0021 secs
  99% in 0.0029 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0012 secs, 0.0001 secs, 2.0469 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0000 secs, 0.0000 secs, 0.0007 secs
  resp wait:  0.0013 secs, 0.0002 secs, 0.2084 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0011 secs

Status code distribution:
  [200]       19351 responses

解读:-z 5s 用低并发(-c 10)在约 5.49s 内完成 19351 次请求、吞吐 3524.2 请求/秒、P99=2.9ms。对比 4.1/4.2 可见:并发 50100 时吞吐反而大幅下降,而低并发下吞吐最高,说明目标服务单线程、高并发时排队严重;时长压测建议至少 10s60s。

4.4 -q QPS 限速

用途:-q 限制每个 worker 的请求速率(QPS),用于「限速压测」。默认不限速。

参数说明:-q Rate limit, in queries per second (QPS) per worker. Default is no rate limit. 注意是按 worker 限速,总 QPS ≈ -q × -c。

hey -n 500 -q 20 -c 10 http://172.29.0.5:8080/index.html
Summary:
  Total:        5.4022 secs
  Slowest:      1.0565 secs
  Fastest:      0.0007 secs
  Average:      0.0260 secs
  Requests/sec: 92.5548

  Total data:   13000 bytes
  Size/request: 26 bytes

Response time histogram:
  0.001 [1]   |
  0.106 [485] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.212 [3]   |
  0.317 [0]   |
  0.423 [0]   |
  0.529 [0]   |
  0.634 [0]   |
  0.740 [0]   |
  0.845 [0]   |
  0.951 [0]   |
  1.057 [11]  |■

Latency distribution:
  10% in 0.0012 secs
  25% in 0.0015 secs
  50% in 0.0020 secs
  75% in 0.0025 secs
  90% in 0.0031 secs
  95% in 0.0035 secs
  99% in 1.0435 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0230 secs, 0.0001 secs, 1.0544 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0000 secs, 0.0000 secs, 0.0010 secs
  resp wait:  0.0028 secs, 0.0003 secs, 0.2092 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0004 secs

Status code distribution:
  [200]       500 responses

解读:-q 20 -c 10 理论上限 200 QPS,实测 Requests/sec=92.55,500 次请求约每 worker 20 QPS 地逐步发出,Total=5.4022s ≈ 500 / (20×10)。限速压测常用于模拟较弱流量、对比不同并发下的延迟。

4.5 -t 请求超时

用途:-t 设置每次请求的超时秒数(Default is 20,0 表示无限)。目标响应慢时调低可让压测提前结束并统计超时。

参数说明:-t Timeout for each request in seconds. Default is 20, use 0 for infinite.

hey -n 300 -t 1 -c 10 http://172.29.0.5:8080/index.html
Summary:
  Total:        1.0328 secs
  Slowest:      0.0035 secs
  Fastest:      0.0005 secs
  Average:      0.0015 secs
  Requests/sec: 290.4621

  Total data:   7696 bytes
  Size/request: 26 bytes

Response time histogram:
  0.000 [1]   |■
  0.001 [4]   |■■
  0.001 [77]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.001 [34]  |■■■■■■■■■■■■■■■■■
  0.002 [80]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.002 [50]  |■■■■■■■■■■■■■■■■■■■■■■■■■
  0.002 [26]  |■■■■■■■■■■■■■
  0.003 [13]  |■■■■■■■
  0.003 [5]   |■■■
  0.003 [4]   |■■
  0.003 [2]   |■

Latency distribution:
  10% in 0.0009 secs
  25% in 0.0010 secs
  50% in 0.0015 secs
  75% in 0.0018 secs
  90% in 0.0022 secs
  95% in 0.0025 secs
  99% in 0.0034 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0002 secs, 0.0001 secs, 0.0017 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0000 secs, 0.0000 secs, 0.0009 secs
  resp wait:  0.0012 secs, 0.0003 secs, 0.0028 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0008 secs

Status code distribution:
  [200]       296 responses

Error distribution:
  [4] Get "http://172.29.0.5:8080/index.html": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

解读:目标响应多数在毫秒级(最慢 3.5ms),-t 1 只对超过 1s 未返回的请求生效:本次 300 次中有 4 次超时,成功 [200] 296 次,超时计入 Error distribution,错误信息为 context deadline exceeded (Client.Timeout exceeded while awaiting headers)。Total data=7696 = 26 × 296。若目标长时间无响应,调低 -t 可避免压测被慢请求拖长,并据此判断慢请求占比。

4.6 -H 自定义请求头

用途:-H 为每次请求追加自定义 HTTP 头,可重复指定多个。

参数说明:-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".

hey -n 200 -c 10 -H "X-Test-Header: hey-demo" http://172.29.0.5:8080/index.html
Summary:
  Total:        1.0705 secs
  Slowest:      1.0523 secs
  Fastest:      0.0007 secs
  Average:      0.0182 secs
  Requests/sec: 186.8245

  Total data:   5200 bytes
  Size/request: 26 bytes

Response time histogram:
  0.001 [1]   |
  0.106 [195] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.211 [1]   |
  0.316 [0]   |
  0.421 [0]   |
  0.526 [0]   |
  0.632 [0]   |
  0.737 [0]   |
  0.842 [0]   |
  0.947 [0]   |
  1.052 [3]   |■

Latency distribution:
  10% in 0.0008 secs
  25% in 0.0009 secs
  50% in 0.0015 secs
  75% in 0.0017 secs
  90% in 0.0024 secs
  95% in 0.0028 secs
  99% in 1.0518 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0160 secs, 0.0001 secs, 1.0508 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0000 secs, 0.0000 secs, 0.0002 secs
  resp wait:  0.0021 secs, 0.0003 secs, 0.2089 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0004 secs

Status code distribution:
  [200]       200 responses

解读:-H 对 hey 的汇总统计无影响,只在每次实际发出的请求里追加该头(本次追加 X-Test-Header: hey-demo)。多带头可叠加,如鉴权 Authorization、Host 等场景。

4.7 -m HTTP 方法

用途:-m 指定请求方法(GET、POST、PUT、DELETE、HEAD、OPTIONS)。

参数说明:-m HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS. 目标 Python http.server 仅支持 GET,下面用 HEAD 演示合法方法。

hey -n 30 -c 5 -m HEAD http://172.29.0.5:8080/index.html
Summary:
  Total:        0.0094 secs
  Slowest:      0.0026 secs
  Fastest:      0.0005 secs
  Average:      0.0014 secs
  Requests/sec: 3193.3422

  Total data:   780 bytes
  Size/request: 26 bytes

Response time histogram:
  0.001 [1]   |■■■■
  0.001 [1]   |■■■■
  0.001 [3]   |■■■■■■■■■■■■■
  0.001 [6]   |■■■■■■■■■■■■■■■■■■■■■■■■■
  0.001 [9]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.002 [1]   |■■■■
  0.002 [3]   |■■■■■■■■■■■■■
  0.002 [0]   |
  0.002 [2]   |■■■■■■■■■
  0.002 [2]   |■■■■■■■■■
  0.003 [2]   |■■■■■■■■■

Latency distribution:
  10% in 0.0009 secs
  25% in 0.0011 secs
  50% in 0.0013 secs
  75% in 0.0018 secs
  90% in 0.0023 secs
  95% in 0.0026 secs
  0% in 0.0000 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0002 secs, 0.0001 secs, 0.0006 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0000 secs, 0.0000 secs, 0.0002 secs
  resp wait:  0.0010 secs, 0.0003 secs, 0.0022 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0002 secs

Status code distribution:
  [200]       30 responses

解读:-m HEAD 全部 30 次返回 200;HEAD 只取响应头不取正文,resp read(读取正文耗时)极小。把方法换成 POST/PUT 并配合 -d 可压测写入接口。

4.8 -d 请求体(body)

用途:-d 指定请求体内容,与 -m POST/PUT 搭配发送数据。

参数说明:-d HTTP request body. 缺省 -m 为 GET 时 body 不生效;POST 时若未配 -T,默认 Content-Type 为 text/html。

hey -n 30 -c 5 -m POST -d "load=test" http://172.29.0.5:8080/
Summary:
  Total:        0.0082 secs
  Slowest:      0.0021 secs
  Fastest:      0.0007 secs
  Average:      0.0012 secs
  Requests/sec: 3636.7079

  Total data:   14910 bytes
  Size/request: 497 bytes

Response time histogram:
  0.001 [1]   |■■■■■
  0.001 [1]   |■■■■■
  0.001 [8]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.001 [6]   |■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.001 [3]   |■■■■■■■■■■■■■■■
  0.001 [4]   |■■■■■■■■■■■■■■■■■■
  0.002 [0]   |
  0.002 [4]   |■■■■■■■■■■■■■■■■■■■■
  0.002 [0]   |
  0.002 [1]   |■■■■■
  0.002 [2]   |■■■■■■■■■■

Latency distribution:
  10% in 0.0009 secs
  25% in 0.0010 secs
  50% in 0.0011 secs
  75% in 0.0015 secs
  90% in 0.0018 secs
  95% in 0.0021 secs
  0% in 0.0000 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0002 secs, 0.0001 secs, 0.0008 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0001 secs, 0.0000 secs, 0.0004 secs
  resp wait:  0.0009 secs, 0.0004 secs, 0.0015 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0002 secs

Status code distribution:
  [501]       30 responses

解读:目标 Python SimpleHTTP 不支持 POST,30 次全部返回 501,Size/request=497 为 501 错误页字节数;[501] 30 responses 说明请求都成功发送并被服务端响应,只是非 2xx。压测写入接口时务必检查 Status code distribution 是否为期望状态码,而不是只看 Requests/sec。

4.9 -T Content-Type

用途:-T 设置请求体(body)的 Content-Type,需与 -d/-D 搭配。

参数说明:-T Content-type, defaults to "text/html". -D 表示从文件读取请求体(-D HTTP request body from file)。

hey -n 30 -c 5 -m POST -d "load=test" -T "application/x-www-form-urlencoded" http://172.29.0.5:8080/
Summary:
  Total:        0.0078 secs
  Slowest:      0.0027 secs
  Fastest:      0.0005 secs
  Average:      0.0011 secs
  Requests/sec: 3835.4647

  Total data:   14910 bytes
  Size/request: 497 bytes

Response time histogram:
  0.001 [1]   |■■■■
  0.001 [2]   |■■■■■■■■
  0.001 [1]   |■■■■
  0.001 [4]   |■■■■■■■■■■■■■■■■
  0.001 [13]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.002 [3]   |■■■■■■■■■■■■
  0.002 [1]   |■■■■
  0.002 [2]   |■■■■■■■■
  0.002 [2]   |■■■■■■■■
  0.002 [1]   |■■■■
  0.003 [0]   |

Latency distribution:
  10% in 0.0009 secs
  25% in 0.0010 secs
  50% in 0.0011 secs
  75% in 0.0015 secs
  90% in 0.0018 secs
  95% in 0.0022 secs
  0% in 0.0000 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0002 secs, 0.0001 secs, 0.0008 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0001 secs, 0.0000 secs, 0.0004 secs
  resp wait:  0.0009 secs, 0.0004 secs, 0.0015 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0002 secs

Status code distribution:
  [501]       30 responses

解读:加了 -T application/x-www-form-urlencoded 后请求体以该类型发送;目标不支持 POST 仍返回 501(与 4.8 同)。-T 只影响请求头的 Content-Type,不改变目标对 POST 的支持与否;压测可接收 body 的接口时用它指定正确的类型。

4.10 -o 输出格式(csv / json / histogram)

用途:-o 指定输出格式。仅 csv 为受支持的输出类型;json、histogram 在本 hey 版本不被支持。

参数说明:-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.

-o csv(逐请求输出逗号分隔的响应指标,每行一次请求,首行为表头):

hey -n 5 -c 2 -o csv http://172.29.0.5:8080/index.html
response-time,DNS+dialup,DNS,Request-write,Response-delay,Response-read,status-code,offset
0.0015,0.0004,0.0000,0.0001,0.0007,0.0002,200,0.0004
0.0017,0.0003,0.0000,0.0003,0.0008,0.0001,200,0.0005
0.0019,0.0002,0.0000,0.0002,0.0013,0.0001,200,0.0007
0.0022,0.0002,0.0000,0.0001,0.0017,0.0001,200,0.0006
0.0023,0.0013,0.0000,0.0000,0.0009,0.0001,200,0.0008

-o json(非受支持值,hey 不报错但也不发起压测,仅把该值原样打印,列为实测取证):

hey -n 10 -o json http://172.29.0.5:8080/index.html
json

解读:-o csv 每行一条请求,列含义依次为 response-time(总响应时间)、DNS+dialup、DNS、Request-write、Response-delay、Response-read、status-code、offset,便于落盘/绘图。而 json、histogram 并非本版本支持的输出类型,实测只见命令打印字面 json 后直接退出,不产生压测——不要对 non-csv 的 -o 值抱期望,需要 JSON/直方图统计请用默认 summary 输出的解析或 -o csv 二次处理。

4.11 --help 帮助

用途:--help 打印全部参数说明。

hey --help
Usage: hey [options...] <url>

Options:
  -n  Number of requests to run. Default is 200.
  -c  Number of workers to run concurrently. Total number of requests cannot
      be smaller than the concurrency level. Default is 50.

完整帮助全文见「五、帮助输出」。

五、帮助输出

5.1 hey --help(帮助全文,CentOS 8 / hey 0.1.5)

Usage: hey [options...] <url>

Options:
  -n  Number of requests to run. Default is 200.
  -c  Number of workers to run concurrently. Total number of requests cannot
      be smaller than the concurrency level. Default is 50.
  -q  Rate limit, in queries per second (QPS) per worker. Default is no rate limit.
  -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.
  -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.

  -m  HTTP method, one of GET, POST, PUT, DELETE, HEAD, OPTIONS.
  -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" .
  -t  Timeout for each request in seconds. Default is 20, use 0 for infinite.
  -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".
  -U  User-Agent, defaults to version "hey/0.0.1".
  -a  Basic authentication, username:password.
  -x  HTTP Proxy address as host:port.
  -h2 Enable HTTP/2.

  -host HTTP Host header.

  -disable-compression  Disable compression.
  -disable-keepalive    Disable keep-alive, prevents re-use of TCP
                        connections between different HTTP requests.
  -disable-redirects    Disable following of HTTP redirects
  -cpus                 Number of used cpu cores.
                        (default for current machine is 6 cores)

💡 简要说明:-A 设 Accept 头、-U 设 User-Agent、-a Basic 认证、-x 代理、-h2 启用 HTTP/2、-host 覆盖 Host 头、-disable-compression/-keepalive/-redirects 关闭相应特性、-cpus 限制使用 CPU 核数。-U 帮助里的 hey/0.0.1 是默认 User-Agent 的固定写法,并非真实版本(真实版本见 go version -m,本文为 0.1.5)。

5.2 版本查询

hey 不提供 --version;用 Go 工具读二进制构建元数据即可精确拿到版本:

go version -m /usr/local/bin/hey | sed -n '1,3p'
/usr/local/bin/hey: go1.24.2
        path    github.com/rakyll/hey
        mod     github.com/rakyll/hey   v0.1.5  h1:oc3QhpT8ETXcr5xIE2xgWYNSNA/Z52XA20ku9hWCchY=

六、常见问题

6.1 hey --version 报 flag provided but not defined: -version

Q:hey --version 想查版本,却报 flag provided but not defined: -version 并打印 usage? A:hey 没有实现 --version。查版本请用 go version -m /usr/local/bin/hey | sed -n '1,3p'(见 5.2)。-U 帮助里写的 hey/0.0.1 只是默认 User-Agent 的占位写法,并非版本文案。

6.2 Status code distribution 里出现 [501] / [404] 算失败吗?

Q:压测 POST 接口时 Status code distribution 只看到 [501] 30 responses,但 hey 没报错,怎么回事? A:说明请求都已发出并被服务端响应,但服务端返回的是 501/404 等非 2xx(本环境 Python SimpleHTTP 不支持 POST/PUT,见 4.8/4.9)。hey 把「收到响应」都正常统计,不会再额外报错,判断业务是否成功必须看 Status code distribution 里的状态码是否为期望值(如 [200])。

6.3 Error distribution: context deadline exceeded 是什么?

Q:输出末尾出现 Error distribution: [4] Get "http://172.29.0.5:8080/index.html": context deadline exceeded (Client.Timeout exceeded while awaiting headers),还导致一些请求没进 Status code distribution? A:是 -t(默认 20s)超时触发:有请求在超时秒数内未收到响应头,被判定超时并计入 Error distribution(见 4.2/4.5 的实测量)。若目标正常但并发过高导致排队超长,会大量出现该错误,需降低 -c 或增大 -t。超时请求不计入成功状态分布,故 [200] 296 + errors 4 = 300 次。

6.4 为什么 -z 设了 5s 但 Total 显示比 5s 大?

Q:-z 5s 压测,Summary 里 Total 竟然有 5.49s,不是到点就停吗? A:-z 到点即停止新增请求,但 Total 包含已发起请求的收尾等待(等最后一个慢请求返回后才统计完),故略大于设定时长。这是 hey 的正常统计口径,看每请求平均/分位数即可,无需纠结毫秒级收尾。

6.5 go install 报 connect: connection refused 下载失败

Q:go install github.com/rakyll/hey@v0.1.5 报 Get "https://proxy.golang.org/github.com/rakyll/hey/@v/list": dial tcp 142.251.45.145:443: connect: connection refused? A:默认 GOPROXY=https://proxy.golang.org,direct 在境内/受限网络无法连通。加国内代理再装即可:GOPROXY=https://goproxy.cn,direct go install github.com/rakyll/hey@v0.1.5(见 3.2)。若已下载过依赖,模块缓存亦可直接离线安装。第一次执行 3.3 的 go install github.com/rakyll/hey@v0.1.4 会有 go: downloading github.com/rakyll/hey v0.1.4 等下载日志,后续同版本会走缓存复用。

6.6 -o json/-o histogram 没有输出压测结果

Q:-o json 跑了之后只打印 json,没有汇总也没发起请求? A:本 hey 版本(0.1.5)的 -o 只支持 csv。传 json/histogram 这类未支持值时,hey 不识别也不报错,直接打印该字面值退出(实测取证见 4.10)。需要结构化输出请用 -o csv,并对 CSV 做二次处理。

6.7 为什么高并发吞吐反而比低并发低?

Q:-c 100 的吞吐(44.9/s)远低于 -c 10 的时长压测(3524.2/s),这不合理? A:目标为单线程 Python http.server,高并发下请求在服务端排队严重,吞吐锐减且超时增多(见 4.2)。hey 本身并发能力强,瓶颈在目标服务。压测对比应固定并发档位、在目标服务稳定时取多次均值;同时看 Requests/sec、Latency distribution 尾部与 Error distribution。

七、参考链接