Apache Benchmark

HTTP 伺服器壓力測試工具

基本用法

常用參數

  • -n → 請求數量
  • -c → 併發數量
  • -r → 收到錯誤訊息不退出程序
  • -T → Content-Type
  • -p → POST 資料檔案
  • -H → 自訂 Header
  • -e → 輸出 CSV 檔案

範例

發送 500 請求,最大併發 10

ab -r -n 100 -c 5 http://127.0.0.1:8080/json

輸出到 CSV

ab -n 10 -c 5 -e output.csv http://127.0.0.1:8081/webclient

帶 JSON Body 和 Header 的請求

ab -p data.json -T 'application/json; charset=utf-8' -H "Authorization: Bearer $JWT_TOKEN" -c 10 -n 100 -r http://example.com/path/to/endpoint

測試效能匯出成 CSV

ab -r -e output.csv -n 100 -c 10 http://127.0.0.1:8080/json

輸出說明

伺服器資訊

  • Server Software → Web 主機的作業系統與版本
  • Server Hostname → Web 主機的 IP 位址
  • Server Port → Web 主機的連接埠

文件資訊

  • Document Path → 網址的路徑部分
  • Document Length → 網頁回應的網頁大小

測試結果

  • Concurrency Level → 同時進行壓力測試的人數
  • Time taken for tests → 處理完成所有請求的時間
  • Complete requests → 完成的請求數
  • Failed requests → 失敗的請求數

效能指標

  • Requests per second → 每秒處理請求的數量
    • 計算: Request Numbers / Taken Time
  • Time per request
    • 第一個 TPR: 併發數 × 處理時間 × 1000 / Request Numbers (平均每個要求的時間)
    • 第二個 TPR: 處理時間 × 1000 / Request Numbers (所有同時連線數的平均值)

傳輸資訊

  • Total transferred → 包括 HTTP Header 的測試總數據傳輸量 (bytes)
  • HTML transferred → 僅回傳的 HTML 資料的測試總數據傳輸量 (bytes)
  • Transfer rate → ab 到 Web Server 的網路傳輸速度 (Kbytes/sec)

連線時間

  • Connection Times (ms) → 測試時的連線處理時間

輸出範例

This is ApacheBench, Version 2.3 <$Revision: 1903618 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient).....done

Server Software:
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /json
Document Length:        111 bytes

Concurrency Level:      10
Time taken for tests:   0.164 seconds
Complete requests:      100
Failed requests:        84
   (Connect: 0, Receive: 0, Length: 84, Exceptions: 0)
Total transferred:      21534 bytes
HTML transferred:       11034 bytes
Requests per second:    611.61 [#/sec] (mean)
Time per request:       16.350 [ms] (mean)
Time per request:       1.635 [ms] (mean, across all concurrent requests)
Transfer rate:          128.62 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       1
Processing:     2    8   9.1      7      91
Waiting:        1    8   9.0      6      90
Total:          2    8   9.1      7      92

Percentage of the requests served within a certain time (ms)
  50%      7
  66%      9
  75%      9
  80%     10
  90%     12
  95%     14
  98%     15
  99%     92
 100%     92 (longest request)