Apache HTTP Server 內建的 ab (ApacheBench) 是最便捷的壓力測試工具,無需額外安裝即可完成靜態/動態頁面的併發基準測試。本文詳細說明 Windows 環境下的操作步驟、參數含義、核心指標解讀與常見誤區,助您量化伺服器承載能力。
index.html、test.php),確認防火牆放行 80/443 埠
ab [options] https://target_host[:port]/path
| 參數 | 含義 | 建議值/說明 |
|---|---|---|
-n | 總請求數 | 1000-100000,視測試時長而定 |
-c | 並發數(併發連線數) | 10-1000,分階段遞增(10, 50, 100, 200...) |
-t | 測試時間上限(秒) | 與 -n 二擇一,如 -t 60 |
-k | 啟用 Keep-Alive | 生產環境通常開啟,測試長連性能 |
-H | 自定義 Header | -H "Host: www.example.com" 測試虛擬主機 |
-p | POST 資料檔案 | 配合 -T 測試表單提交/API |
-T | Content-Type | -T "application/json" |
-v | 輸出詳細度 | -v 2 顯示回應頭,-v 4 顯示回應體 |
@echo off
set TARGET=https://192.168.1.100/index.html
set LOG_DIR=ab_logs
mkdir %LOG_DIR% 2>nul
echo === 靜態頁面基準測試 ===
for %%c in (10 50 100 200 500 1000) do (
echo 並發數: %%c
ab -n 10000 -c %%c -k -H "Host: www.example.com" %TARGET% > %LOG_DIR%\static_c%%c.txt 2>&1
timeout /t 5
)
echo === 動態頁面測試(PHP) ===
for %%c in (10 50 100 200) do (
echo 並發數: %%c
ab -n 5000 -c %%c -k -H "Host: www.example.com" https://192.168.1.100/test.php > %LOG_DIR%\php_c%%c.txt 2>&1
timeout /t 10
)
以實測輸出為例:
Server Software: Apache/2.4.58
Server Hostname: 192.168.1.100
Server Port: 80
Document Path: /index.html
Document Length: 292 bytes
Concurrency Level: 100
Time taken for tests: 2.345 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 5,750,000 bytes
HTML transferred: 2,920,000 bytes
Requests per second: 4264.39 [#/sec] (mean)
Time per request: 23.45 [ms] (mean)
Time per request: 0.23 [ms] (mean, across all concurrent requests)
Transfer rate: 2398.45 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 2
Processing: 5 23 8.4 21 120
Waiting: 5 22 8.3 20 115
Total: 5 23 8.4 21 120
Percentage of the requests served within a certain time (ms)
50% 21
66% 22
75% 23
80% 24
90% 28
95% 35
99% 68
100% 120 (longest request)
| 指標 | 業務含義 | 健康基準(靜態頁參考) |
|---|---|---|
| Requests per second (RPS) | 吞吐量核心指標,單位時間處理請求數 | > 3000 (靜態) / > 500 (動態 PHP) |
| Time per request (mean) | 平均響應時間(含併發等待) | < 50ms (靜態) / < 200ms (動態) |
| Time per request (across all) | 單請求平均耗時(扣除併發並行效應) | 接近 Processing mean,反映真實處理能力 |
| Failed requests | 非 2xx 回應或連線錯誤數 | 0(生產環境容忍 < 0.1%) |
| 99th Percentile | 99% 請求的響應時間上界 | < 200ms (靜態) / < 1000ms (動態) |
| Transfer rate | 網路頻寬利用率 | 接近網卡理論值 80% 為佳 |
-k,否則低估 3-5 倍效能。mod_status、系統 top、資料庫 show processlist,關聯分析瓶頸。掌握 ab 可快速完成單機基準驗證,但生產環境容量規劃需引入分散式、腳本化、可觀測的專業壓測體系,才能真實還原業務流量特徵並預判擴容節點。