ab (ApacheBench) 是 Apache 附帶的 HTTP 效能基準測試工具,用於測量單位時間內伺服器能處理的請求數(吞吐率)。
ab -n 1000 -c 10 https://localhost:81/t.php
-n 1000:總請求數-c 10:並發連線數(模擬 10 使用者同時存取)| 指標 | 中文名稱 | 計算/含義 | 關注重點 |
|---|---|---|---|
| Concurrency Level | 並發層級 | 設定的 -c 值 | 實際並發能力 |
| Time taken for tests | 總耗時 | 所有請求完成總秒數 | 越短越好 |
| Complete requests | 成功請求 | HTTP 2xx/3xx 回應數 | 應等於 -n |
| Failed requests | 失敗請求 | 非 2xx/3xx 或連線錯誤 | 應為 0 |
| Requests per second | 吞吐率 (RPS) | Complete / Time taken | 核心效能指標,越高越好 |
| Time per request | 平均請求延遲 | Time taken / Complete × Concurrency | 單用戶體感延遲 |
| Transfer rate | 傳輸速率 | 總位元組 / 秒 | 頻寬利用率參考 |
| 參數 | 用途 | 範例 |
|---|---|---|
-k | 啟用 Keep-Alive 長連線 | ab -k -n 1000 -c 50 ... |
-H | 自訂 Header(如 Token、Cookie) | -H "Authorization: Bearer xxx" |
-p | POST 資料檔案 | -p post_data.json -T "application/json" |
-T | Content-Type | 配合 -p 使用 |
-v | 詳細輸出等級 (1-4) | -v 2 顯示回應標頭 |
-g | 輸出 gnuplot 格式資料 | -g result.tsv 繪圖分析 |
Time per request (P99/P999) 判斷尾部延遲體驗。使用 -g output.tsv 匯出,配合 Python Pandas + Matplotlib 或 Grafana 繪製: