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 -h
Usage: ab [options] [http://]hostname[:port]/path
Options 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 ms 0,6.200000e+001 1,6.200000e+001 2,6.200000e+001 3,6.200000e+001 4,6.200000e+001 5,6.200000e+001 6,6.200000e+001 7,6.200000e+001 8,6.200000e+001 9,6.200000e+001 10,6.200000e+001 11,6.200000e+001 12,6.200000e+001 13,6.200000e+001 14,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.aspx This is ApacheBench, Version 2.0.41-dev < $Revision: 1.121.2.12 $> apache-2.0 Copyright (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 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Finished 1000 requests Server Software: Microsoft-IIS/6.0 Server Hostname: www.m-taoyuan.tw Server Port: 80 Document Path: /index.aspx Document Length: 25986 bytes Concurrency Level: 3 Time taken for tests: 25.734375 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 26372000 bytes HTML transferred: 25986000 bytes Requests 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] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 1 4.4 0 15 Processing: 62 75 9.1 78 109 Waiting: 46 64 8.0 62 109 Total: 62 76 9.3 78 109
如上表顯示的結果來說明,我一個欄位一個欄位的講解如下:
最後的 Connection Times (ms) 指的是壓力測試時的連線處理時間:
橫軸欄位的部分:
縱軸欄位的部分:
壓力測試的基本觀念
參考網址: