Ubuntu 22.04 hey 安装与命令参数详解

在 Ubuntu 22.04 上通过 go install 安装 HTTP 压测工具 hey 0.1.5(Go 1.24.2),并逐参数实测 -n/-c/-z/-q/-t/-H/-m/-d/-T/-o 及 --help 的输入与回显,涵盖输入输出格式与 apt 备用安装。

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

Ubuntu 22.04 hey 安装与命令参数详解

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

一、环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(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.2/16
  • 目标站点:172.29.0.4:8080(HTTP 服务,供压测)
  • 目标服务类型:Python http.server(SimpleHTTP,Python 3.10.12)
  • 测试资源:/index.html(20 字节,实测每请求响应 26 字节)
  • 工作目录:/opt/data/hey/

二、工具概述

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

⚠️ 注意:hey 不提供 --version 参数(hey --version 会报 flag provided but not defined: -version),版本信息可通过 Go 自带的 go version -m 子命令从二进制元数据读出(见 3.2)。

三、安装

hey 的官方发行说明中并不附带各平台预编译的二进制包(rakyll/hey 只发布源码),因此官方推荐且最通用的是用 Go 工具链以 go install 安装。Ubuntu 22.04 官方仓库也内置 hey 软件包(0.1.4-1)作为备用方案。

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。Ubuntu 亦可用 sudo apt install -y golang-go 安装(会取 Ubuntu 仓库里的 Go 版本,比 1.24.2 旧)。

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>(完整帮助见「五、帮助输出」)。

💡 多版本管理:go install github.com/rakyll/hey@<版本号> 可安装任意指定版本,同一机器上多用不同 GOBIN 目录即可并存多版 hey。例如将 0.1.4 装进独立目录,与默认路径下的最新版并存:GOBIN=/opt/data/hey-bin2 go install github.com/rakyll/hey@v0.1.4,再用 /opt/data/hey-bin2/hey -version 验证该目录下的版本即可切换使用。

3.3 方法二(备用):Ubuntu 仓库 apt 安装

Ubuntu 22.04 官方仓库(universe)自带 hey 软件包(版本 0.1.4-1,略旧于源码版 0.1.5),无需 Go 工具链:

sudo apt update
sudo apt install -y hey
dpkg -l hey | tail -2
||/ Name           Version        Architecture Description
+++-==============-==============-============-=====================================
ii  hey            0.1.4-1        amd64        Sends some load to a web application

验证可运行:

/usr/bin/hey -z 2s http://172.29.0.4:8080/index.html | sed -n '1,4p'
Summary:
  Total:        7.6682 secs
  Slowest:      7.6606 secs
  Fastest:      0.0018 secs

💡 apt 版为 0.1.4(装在 /usr/bin/hey),go install 版为 0.1.5(/usr/local/bin/hey 指向它)。由于 /usr/local/bin 优先级高于 /usr/bin,默认执行 hey 调用到的是 0.1.5。备用方案适合不想装 Go、可联网的场景,但版本旧,且 -o 等行为以各自实装为准。

3.4 卸载与清理

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

四、命令实测

实测约定:目标均为 http://172.29.0.4: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.4:8080/index.html
Summary:
  Total:        3.2593 secs
  Slowest:      2.2596 secs
  Fastest:      0.0010 secs
  Average:      0.1480 secs
  Requests/sec: 153.4060

  Total data:   13000 bytes
  Size/request: 26 bytes

Response time histogram:
  0.001 [1]   |
  0.227 [456] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.453 [0]   |
  0.679 [0]   |
  0.904 [0]   |
  1.130 [9]   |■
  1.356 [6]   |■
  1.582 [8]   |■
  1.808 [0]   |
  2.034 [6]   |■
  2.260 [14]  |■

Latency distribution:
  10% in 0.0040 secs
  25% in 0.0073 secs
  50% in 0.0091 secs
  75% in 0.0109 secs
  90% in 0.0157 secs
  95% in 1.4311 secs
  99% in 2.2451 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.1197 secs, 0.0001 secs, 2.0320 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0001 secs, 0.0000 secs, 0.0008 secs
  resp wait:  0.0273 secs, 0.0004 secs, 0.8299 secs
  resp read:  0.0009 secs, 0.0001 secs, 0.0047 secs

Status code distribution:
  [200]       500 responses

解读:Summary 给出总耗时 Total=3.2593s、最快/最慢/平均响应与吞吐 Requests/sec=153.4060;Total data=13000 = 26 字节 × 500。Response time histogram 是响应时间直方图(含桶起点与落入该桶的请求数,如 456 个请求在 0.227s 档),多数请求集中在秒级以内。Latency distribution 给出分位数(如 P50=9.1ms、P90=15.7ms),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.4:8080/index.html
Summary:
  Total:        8.7043 secs
  Slowest:      8.6701 secs
  Fastest:      0.0010 secs
  Average:      0.2720 secs
  Requests/sec: 114.8858

  Total data:   26000 bytes
  Size/request: 26 bytes

Response time histogram:
  0.001 [1]   |
  0.868 [897] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  1.735 [34]  |■■
  2.602 [27]  |■
  3.469 [21]  |■
  4.336 [7]   |
  5.202 [3]   |
  6.069 [7]   |
  6.936 [0]   |
  7.803 [0]   |
  8.670 [3]   |

Latency distribution:
  10% in 0.0040 secs
  25% in 0.0077 secs
  50% in 0.0096 secs
  75% in 0.0115 secs
  90% in 1.0069 secs
  95% in 2.2922 secs
  99% in 5.3983 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.1629 secs, 0.0001 secs, 3.0963 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0000 secs, 0.0000 secs, 0.0008 secs
  resp wait:  0.1081 secs, 0.0005 secs, 6.5956 secs
  resp read:  0.0010 secs, 0.0000 secs, 0.0051 secs

Status code distribution:
  [200]       1000 responses

解读:并发提到 100 后,吞吐从 153.4 降到 114.9 请求/秒、P99 升到 5.3983s,说明已达目标 http.server 的软并发上限(高并发在服务端排队)。Requests/sec 可直接反映并发加大后的吞吐变化,分析时需同时看 Latency distribution 尾部(P95/P99)是否被拉长。

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.4:8080/index.html
Summary:
  Total:        5.7941 secs
  Slowest:      1.2690 secs
  Fastest:      0.0022 secs
  Average:      0.0152 secs
  Requests/sec: 592.3259

  Total data:   89232 bytes
  Size/request: 26 bytes

Response time histogram:
  0.002 [1]   |
  0.129 [3411] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.256 [0]   |
  0.382 [0]   |
  0.509 [0]   |
  0.636 [0]   |
  0.762 [0]   |
  0.889 [0]   |
  1.016 [0]   |
  1.142 [19]  |
  1.269 [1]   |

Latency distribution:
  10% in 0.0066 secs
  25% in 0.0076 secs
  50% in 0.0089 secs
  75% in 0.0104 secs
  90% in 0.0119 secs
  95% in 0.0132 secs
  99% in 0.0171 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0063 secs, 0.0001 secs, 1.0638 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0000 secs, 0.0000 secs, 0.0008 secs
  resp wait:  0.0078 secs, 0.0009 secs, 0.2146 secs
  resp read:  0.0011 secs, 0.0000 secs, 0.0069 secs

Status code distribution:
  [200]       3432 responses

解读:-z 5s 在约 5.79s 内完成 3432 次请求、吞吐 592.3 请求/秒。用时长压测时关注 Total(实际耗时)与 Requests/sec 即可;合理时长建议至少 10s~60s,太短单次结果波动大。

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.4:8080/index.html
Summary:
  Total:        5.3522 secs
  Slowest:      1.0463 secs
  Fastest:      0.0010 secs
  Average:      0.0306 secs
  Requests/sec: 93.4191

  Total data:   13000 bytes
  Size/request: 26 bytes

Response time histogram:
  0.001 [1]   |
  0.106 [486] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.210 [1]   |
  0.315 [0]   |
  0.419 [0]   |
  0.524 [0]   |
  0.628 [0]   |
  0.733 [0]   |
  0.837 [0]   |
  0.942 [0]   |
  1.046 [12]  |■

Latency distribution:
  10% in 0.0029 secs
  25% in 0.0038 secs
  50% in 0.0057 secs
  75% in 0.0077 secs
  90% in 0.0087 secs
  95% in 0.0103 secs
  99% in 1.0325 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0248 secs, 0.0001 secs, 1.0424 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.0050 secs, 0.0006 secs, 0.2095 secs
  resp read:  0.0007 secs, 0.0000 secs, 0.0050 secs

Status code distribution:
  [200]       500 responses

解读:-q 20 -c 10 理论上限 200 QPS,实测 Requests/sec=93.42(受 -q 限速与 -n 总量约束),500 次请求约每 worker 20 QPS 地逐步发出,Total=5.3522s ≈ 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.4:8080/index.html
Summary:
  Total:        1.1152 secs
  Slowest:      0.0169 secs
  Fastest:      0.0011 secs
  Average:      0.0079 secs
  Requests/sec: 269.0141

  Total data:   7722 bytes
  Size/request: 26 bytes

Response time histogram:
  0.001 [1]   |■
  0.003 [17]  |■■■■■■■■■■■■
  0.004 [42]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.006 [34]  |■■■■■■■■■■■■■■■■■■■■■■■■
  0.007 [25]  |■■■■■■■■■■■■■■■■■■
  0.009 [56]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.011 [54]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.012 [46]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.014 [14]  |■■■■■■■■■■
  0.015 [6]   |■■■■
  0.017 [2]   |■

Latency distribution:
  10% in 0.0033 secs
  25% in 0.0048 secs
  50% in 0.0084 secs
  75% in 0.0104 secs
  90% in 0.0118 secs
  95% in 0.0129 secs
  99% in 0.0158 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0002 secs, 0.0000 secs, 0.0008 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0000 secs, 0.0000 secs, 0.0003 secs
  resp wait:  0.0067 secs, 0.0007 secs, 0.0156 secs
  resp read:  0.0009 secs, 0.0000 secs, 0.0047 secs

Status code distribution:
  [200]       297 responses

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

解读:目标响应多数在毫秒级(最慢 16.9ms),-t 1 只对超过 1s 未返回的请求生效:本次 300 次中有 3 次超时,Status code distribution 只统计成功返回的 297 次(200),超时的 3 次计入 Error distribution,错误信息为 context deadline exceeded (Client.Timeout exceeded while awaiting headers)。Total data=7722 = 26 × 297。若目标长时间无响应,调低 -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.4:8080/index.html
Summary:
  Total:        1.1386 secs
  Slowest:      1.0589 secs
  Fastest:      0.0008 secs
  Average:      0.0232 secs
  Requests/sec: 175.6550

  Total data:   5200 bytes
  Size/request: 26 bytes

Response time histogram:
  0.001 [1]   |
  0.107 [195] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.212 [1]   |
  0.318 [0]   |
  0.424 [0]   |
  0.530 [0]   |
  0.636 [0]   |
  0.741 [0]   |
  0.847 [0]   |
  0.953 [0]   |
  1.059 [3]   |■

Latency distribution:
  10% in 0.0012 secs
  25% in 0.0021 secs
  50% in 0.0071 secs
  75% in 0.0095 secs
  90% in 0.0122 secs
  95% in 0.0147 secs
  99% in 1.0570 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0160 secs, 0.0001 secs, 1.0574 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0001 secs, 0.0000 secs, 0.0012 secs
  resp wait:  0.0064 secs, 0.0004 secs, 0.2050 secs
  resp read:  0.0008 secs, 0.0001 secs, 0.0045 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.4:8080/index.html
Summary:
  Total:        0.0342 secs
  Slowest:      0.0081 secs
  Fastest:      0.0019 secs
  Average:      0.0052 secs
  Requests/sec: 876.6934

  Total data:   780 bytes
  Size/request: 26 bytes

Response time histogram:
  0.002 [1]   |■■■■■■■
  0.003 [1]   |■■■■■■■
  0.003 [4]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.004 [0]   |
  0.004 [1]   |■■■■■■■
  0.005 [5]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.006 [6]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.006 [3]   |■■■■■■■■■■■■■■■■■■■■
  0.007 [4]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.007 [1]   |■■■■■■■
  0.008 [4]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■

Latency distribution:
  10% in 0.0031 secs
  25% in 0.0045 secs
  50% in 0.0054 secs
  75% in 0.0065 secs
  90% in 0.0078 secs
  95% in 0.0081 secs
  0% in 0.0000 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0003 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.0002 secs
  resp wait:  0.0048 secs, 0.0013 secs, 0.0077 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0001 secs
  resp read:  0.0001 secs, 0.0000 secs, 0.0001 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.4:8080/
Summary:
  Total:        0.0327 secs
  Slowest:      0.0081 secs
  Fastest:      0.0022 secs
  Average:      0.0052 secs
  Requests/sec: 917.5742

  Total data:   14910 bytes
  Size/request: 497 bytes

Response time histogram:
  0.002 [1]   |■■■■■
  0.003 [2]   |■■■■■■■■■■
  0.003 [1]   |■■■■■
  0.004 [3]   |■■■■■■■■■■■■■■■
  0.005 [1]   |■■■■■
  0.005 [4]   |■■■■■■■■■■■■■■■■■■
  0.006 [8]   |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.006 [5]   |■■■■■■■■■■■■■■■■■■■■■■■■■
  0.007 [1]   |■■■■■
  0.008 [1]   |■■■■■
  0.008 [3]   |■■■■■■■■■■■■■■■

Latency distribution:
  10% in 0.0033 secs
  25% in 0.0046 secs
  50% in 0.0054 secs
  75% in 0.0058 secs
  90% in 0.0076 secs
  95% in 0.0081 secs
  0% in 0.0000 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0003 secs, 0.0001 secs, 0.0016 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0001 secs, 0.0000 secs, 0.0002 secs
  resp wait:  0.0044 secs, 0.0011 secs, 0.0073 secs
  resp read:  0.0004 secs, 0.0001 secs, 0.0009 secs

Status code distribution:
  [501]       30 responses

解读:目标 Python SimpleHTTP 不支持 POST,30 次全部返回 501,Size/request=497 为 501 错误页字节数;[501] 30 responses 说明请求都成功发送并被服务端响应,只是非 2xx。压测写入接口时务必检查 Status code distribution 是否为期望状态码(如压测 POST 接口应看到 [200]),而不是只看 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.4:8080/
Summary:
  Total:        0.1068 secs
  Slowest:      0.0090 secs
  Fastest:      0.0010 secs
  Average:      0.0052 secs
  Requests/sec: 936.2840

  Total data:   49700 bytes
  Size/request: 497 bytes

Response time histogram:
  0.001 [1]   |■■
  0.002 [0]   |
  0.003 [2]   |■■■
  0.003 [7]   |■■■■■■■■■■■■
  0.004 [13]  |■■■■■■■■■■■■■■■■■■■■
  0.005 [24]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.006 [23]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.007 [10]  |■■■■■■■■■■■■■■■■■
  0.007 [13]  |■■■■■■■■■■■■■■■■■■■■■■■■
  0.008 [4]   |■■■■■■■
  0.009 [3]   |■■■■■

Latency distribution:
  10% in 0.0035 secs
  25% in 0.0043 secs
  50% in 0.0052 secs
  75% in 0.0060 secs
  90% in 0.0071 secs
  95% in 0.0077 secs
  99% in 0.0090 secs

Details (average, fastest, slowest):
  DNS+dialup: 0.0002 secs, 0.0001 secs, 0.0005 secs
  DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:  0.0000 secs, 0.0000 secs, 0.0003 secs
  resp wait:  0.0045 secs, 0.0006 secs, 0.0087 secs
  resp read:  0.0004 secs, 0.0000 secs, 0.0014 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.4:8080/index.html
response-time,DNS+dialup,DNS,Request-write,Response-delay,Response-read,status-code,offset
0.0043,0.0005,0.0000,0.0001,0.0019,0.0017,200,0.0004
0.0040,0.0002,0.0000,0.0001,0.0025,0.0011,200,0.0009
0.0061,0.0003,0.0000,0.0000,0.0052,0.0005,200,0.0007
0.0090,0.0003,0.0000,0.0001,0.0064,0.0021,200,0.0007
0.0091,0.0004,0.0000,0.0000,0.0065,0.0019,200,0.0009

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

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

解读:-o csv 每行一条请求,列含义依次为 response-time(总响应时间)、DNS+dialup、DNS、Request-write、Response-delay、Response-read、status-code、offset,便于落盘/绘图。而 json、histogram 并非本版本支持的输出类型,实测只见两命令分别打印字面 json、histogram 后直接退出,不产生压测——不要对 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(帮助全文,Ubuntu 22.04 / 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),或直接看 hey --help 确认可用参数。-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: [3] Get "http://172.29.0.4:8080/index.html": context deadline exceeded (Client.Timeout exceeded while awaiting headers),还导致一些请求没进 Status code distribution? A:是 -t 超时触发:有 3 个请求在 -t 设置的秒数内未收到响应头,被判定超时并计入 Error distribution(见 4.5 的实测量)。若目标正常但 -t 偏小会出现偶发超时;压测响应慢的目标时适当增大 -t。超时请求不计入 Total data 与成功状态分布,故 [200] 297 + errors 3 = 300 次。

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

Q:-z 5s 压测,Summary 里 Total 竟然有 5.79s,不是到点就停吗? A:-z 到点即停止新增请求,但 Total 包含已发起请求的收尾等待(等最后一个慢请求返回后才统计完),故略大于设定时长(本文 5.79s vs 5s)。这是 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)。若已下载过依赖,模块缓存亦可直接离线安装。也可改 $HOME/.config/go/env 持久化 GOPROXY。

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

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

七、参考链接