Hey 是一款由 Go 語言編寫的 HTTP 負載測試(壓力測試)工具,用來測試 API 或網頁在高並發情況下的效能表現,包括 QPS(每秒請求數)、延遲分佈等指標。
QPS 基本概念
- QPS(Queries Per Second):每秒請求數,衡量系統每秒能處理多少請求
- TPS(Transactions Per Second):每秒事務數,通常指完整業務流程的處理量
- 並發數(Concurrency):同時發出請求的連線數量
在壓力測試時,通常固定並發數,逐步提升 QPS,找出系統的效能瓶頸。
安裝 Hey
方式一:Go Install
1
|
go install github.com/rakyll/hey@latest
|
安裝後,hey 指令會在 $GOPATH/bin 目錄下,確保該目錄在 $PATH 中。
方式二:Homebrew(macOS)
方式三:直接下載二進位檔
前往 Hey GitHub Releases 下載對應平台的執行檔。
常用指令
基本測試
1
2
|
# 發送 200 個請求,並發數 50
hey -n 200 -c 50 https://example.com/api/health
|
常用參數說明
| 參數 |
說明 |
預設值 |
-n |
總請求數 |
200 |
-c |
並發數(同時連線數) |
50 |
-q |
每個 worker 的 QPS 限制(Rate Limiting) |
不限制 |
-z |
持續測試時間(例如 -z 30s,-z 1m) |
- |
-m |
HTTP 方法(GET、POST、PUT 等) |
GET |
-H |
新增 Header(可多次使用) |
- |
-d |
Request Body(POST 資料) |
- |
-T |
Content-Type |
text/html |
-t |
每個請求的 timeout(秒) |
20 |
發送 POST 請求
1
2
3
4
5
6
|
hey -n 500 -c 100 \
-m POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-token" \
-d '{"username": "test", "password": "pass"}' \
https://api.example.com/login
|
限制 QPS(Rate Limiting)
1
2
|
# 持續 30 秒,每秒最多 100 個請求,並發數 10
hey -z 30s -q 100 -c 10 https://api.example.com/data
|
持續時間模式
1
2
3
4
5
|
# 測試 1 分鐘
hey -z 1m -c 50 https://example.com/api
# 測試 30 秒
hey -z 30s -c 20 https://example.com/api
|
輸出結果解讀
執行 hey 後,輸出大致如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
Summary:
Total: 5.2134 secs
Slowest: 0.8321 secs
Fastest: 0.0123 secs
Average: 0.0512 secs
Requests/sec: 962.83 ← QPS
Response time histogram:
0.012 [1] |
0.094 [847] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
0.176 [82] |■■■■
...
Latency distribution:
10% in 0.0234 secs ← 10% 的請求在此時間內完成
25% in 0.0312 secs
50% in 0.0421 secs ← 中位數延遲(P50)
75% in 0.0589 secs
90% in 0.0876 secs ← P90(重要指標)
95% in 0.1023 secs ← P95
99% in 0.3210 secs ← P99(長尾延遲)
Status code distribution:
[200] 4997 responses ← 成功回應
[500] 3 responses ← 伺服器錯誤(需要注意)
|
重要指標說明
- Requests/sec(QPS):系統實際能處理的每秒請求數
- P50(中位數):50% 的請求在此時間內完成,代表一般使用者的體驗
- P90/P95/P99:長尾延遲指標,P99 高代表少數請求卡住,可能是 GC、DB 連線池問題
- Status code distribution:確認是否有大量錯誤回應
參考資料