使用 ApacheBench 進行網站的壓力測試
ApacheBench 工具程式是 Apache 網站伺服器軟體的一個附帶的工具軟體,專門用來執行網站伺服器的運行效能,特別是針對 Apache 網站伺服器 的效能分析。這支程式原本是用來檢測 Apache 網站伺服器(Web Server) 所能夠提供的效能,特別是可以看出 Apache 網站伺服器能提供每秒能送出多少網頁,當然的,也可以用在任何其他的網站伺服器,例如說:IIS 或 lighttpd。
你可以到 Apache HTTP Server 網站下載最新版,如果你要在 Windows 的環境執行 ApacheBench 可以直接下載 Win32 Binary 的版本就好,由於線上所提供的版本是 MSI 的封裝檔,安裝好之後也等同於在你的電腦內安裝了一套 Apache HTTP Server,如果你不需要多執行一套 Apache HTTP Server 的話,你可以在安裝好之後進入 C:\Program Files\Apache Group\Apache2\bin 目錄,找到 ab.exe 執行檔,複製出來後再移除 Apache 安裝即可,因為 ab.exe 是可以獨立執行的,不需要任何關連的 dll 檔。
底下是 ab.exe 的使用參數摘要說明,若要看詳細說明可以看這裡(或中文翻譯):
C:\>ab -hUsage: ab [options] [http://]hostname[:port]/pathOptions are: -n requests Number of requests to perform -c concurrency Number of multiple requests to make -t timelimit Seconds to max. wait for responses -p postfile File containing data to POST -T content-type Content-type header for POSTing -v verbosity How much troubleshooting info to print -w Print out results in HTML tables -i Use HEAD instead of GET -x attributes String to insert as table attributes -y attributes String to insert as tr attributes -z attributes String to insert as td or th attributes -C attribute Add cookie, eg. 'Apache=1234. (repeatable) -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip' Inserted after all normal header lines. (repeatable) -A attribute Add Basic WWW Authentication, the attributes are a colon separated username and password. -P attribute Add Basic Proxy Authentication, the attributes are a colon separated username and password. -X proxy:port Proxyserver and port number to use -V Print version number and exit -k Use HTTP KeepAlive feature -d Do not show percentiles served table. -S Do not show confidence estimators and warnings. -g filename Output collected data to gnuplot format file. -e filename Output CSV file with percentages served -h Display usage information (this message)
而我經常使用的參數摘要如下:
1. 同時 10 個連線,連續點擊 10000 次 ( 每個 Request 執行完畢後都會自動斷線,然後再重新連線 )
ab -n 10000 -c 10 http://www.example.com/index.aspx
2. 同時 10 個連線,連續點擊 10000 次,並且使用 Keep-Alive 方式連線(當 Web Server 有支援 Keep-Alive 功能時 ApacheBench 會在同一個連線下連續點擊該網頁)
ab -n 10000 -c 10 -k http://www.example.com/index.aspx
3. 將測試的效能原始資料匯出成 CSV 檔
ab -e output.csv -n 10000 -c 10 http://www.example.com/index.aspx
匯出的 output.csv 內容如下:
Percentage served,Time in ms0,6.200000e+0011,6.200000e+0012,6.200000e+0013,6.200000e+0014,6.200000e+0015,6.200000e+0016,6.200000e+0017,6.200000e+0018,6.200000e+0019,6.200000e+00110,6.200000e+00111,6.200000e+00112,6.200000e+00113,6.200000e+00114,6.200000e+001......
上表所代表的每一列代表送出的百分比,第二個欄位是當下的 "Time per request" (每個要求所花費的時間),單位是豪秒(millisecond)。
如何有效的檢視結果
壓力測試的核心在於如何分析結果,底下我用一個測試的結果說明每個欄位所代表的意義。如果你只要看重點的話,可以看 Failed requests、Requests per second、與 Time per request 這三個參數也就差不多夠了。其中的 Failed requests 的數量太高的話,很有可能代表你的 Web Application 的穩定度不夠,而導致使用大量要求時無法回應需求 。而 Request per second 代表你每表可送出的回應數有多少,代表你 Web Application 的承載量有多少(在不考慮頻寬限制的情況下)。
C:\>ab -d -e a.csv -v 1 -n 1000 -c 3 http://www.example.com/index.aspxThis is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/Benchmarking www.m-taoyuan.tw (be patient)Completed 100 requestsCompleted 200 requestsCompleted 300 requestsCompleted 400 requestsCompleted 500 requestsCompleted 600 requestsCompleted 700 requestsCompleted 800 requestsCompleted 900 requestsFinished 1000 requestsServer Software: Microsoft-IIS/6.0Server Hostname: www.m-taoyuan.twServer Port: 80Document Path: /index.aspxDocument Length: 25986 bytesConcurrency Level: 3Time taken for tests: 25.734375 secondsComplete requests: 1000Failed requests: 0Write errors: 0Total transferred: 26372000 bytesHTML transferred: 25986000 bytesRequests per second: 38.86 [#/sec] (mean)Time per request: 77.203 [ms] (mean)Time per request: 25.734 [ms] (mean, across all concurrent requests)Transfer rate: 1000.72 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 1 4.4 0 15Processing: 62 75 9.1 78 109Waiting: 46 64 8.0 62 109Total: 62 76 9.3 78 109
如上表顯示的結果來說明,我一個欄位一個欄位的講解如下:
- Server Software: Web主機的作業系統與版本(若Web主機設定關閉此資訊則無)
- Server Hostname: Web主機的IP位址(Hostname)
- Server Port: Web主機的連接埠(Port)
- Document Path: 測試網址的路徑部分
- Document Length: 測試網頁回應的網頁大小
- Concurrency Level: 同時進行壓力測試的人數
- Time taken for tests: 本次壓力測試所花費的總秒數
- Complete requests: 完成的要求數(Requests)
- Failed requests: 失敗的要求數(Requests)
- Write errors: 寫入失敗的數量
- Total transferred: 本次壓力測試的總數據傳輸量(包括 HTTP Header 的資料也計算在內)
- HTML transferred: 本次壓力測試的總數據傳輸量(僅計算回傳的 HTML 的資料)
- Requests per second: 平均每秒可回應多少要求
- Time per request: 平均每個要求所花費的時間(單位: 豪秒)
- Time per request: 平均每個要求所花費的時間,跨所有同時連線數的平均值(單位: 豪秒)
- Transfer rate: 從 ab 到 Web Server 之間的網路傳輸速度
最後的 Connection Times (ms) 指的是壓力測試時的連線處理時間:
橫軸欄位的部分:
- min: 最小值
- mean: 平均值(正、負標準差)
- median: 平均值(中間值)
- max: 最大值
縱軸欄位的部分:
- Connect: 從 ab 發出 TCP 要求到 Web 主機所花費的建立時間。
- Processing: 從 TCP 連線建立後,直到 HTTP 回應(Response)的資料全部都收到所花的時間。
- Waiting: 從發送 HTTP 要求完後,到 HTTP 回應(Response)第一個 Byte 所等待的時間。
- Total: 等於 Connect + Processing 的時間(因為 Waiting 包含在 Processing 時間內了)
壓力測試的基本觀念
- 排除頻寬的限制
- 做壓力測試通常不會考量「頻寬的限制」,所以一般來說不會將測試的主機擺在遠端機房、然後測試程式擺在公司內部的主機,而是會將壓力測試的 Client 跟 Web 主機擺在同一個網段下進行壓力測試。
- 因為「頻寬」只要花錢就會有了,但是主機的承載量卻是有限的,從遠端進行壓力測試主要的限制是在「頻寬」而非「效能」,所以從遠端單點進行壓力測試毫無任何意義可言,這樣是測不出主機的效能極限的。
- 如果你有能力與資源進行大規模(多點)壓力測試的話,透過遠端進行壓力測試才有意義。
- 壓力要循序漸進
- 你不要一下字就執行同時連線數 100 人,而是要循序漸進的慢慢加同時連線數上去,才不會讓 Web Application 一下字承受過大的負載而導致效能的數據不正確(例如說 Failed requests 過高),但這只是建議,你也可以一下子操死你的主機,反正你在測主機的極限嘛!
浙公网安备 33010602011771号