默認(rèn)分類(lèi) 2009-11-06 18:44:24 閱讀1 評(píng)論0 字號(hào):大中小
使用 ApacheBench 進(jìn)行網(wǎng)站的壓力測(cè)試
ApacheBench 工具程序是 Apache 網(wǎng)站服務(wù)器軟件的一個(gè)附帶的工具軟件,專(zhuān)門(mén)用來(lái)執(zhí)行網(wǎng)站服務(wù)器的運(yùn)行效能,特別是針對(duì) Apache 網(wǎng)站服務(wù)器 的效能分析。這支程序原本是用來(lái)檢測(cè) Apache 網(wǎng)站服務(wù)器(Web Server) 所能夠提供的效能,特別是可以看出 Apache 網(wǎng)站服務(wù)器能提供每秒能送出多少網(wǎng)頁(yè),當(dāng)然的,也可以用在任何其它的網(wǎng)站服務(wù)器,例如說(shuō):IIS 或 lighttpd。
你可以到 Apache HTTP Server 網(wǎng)站下載最新版,如果你要在 Windows 的環(huán)境執(zhí)行 ApacheBench 可以直接下載 Win32 Binary 的版本就好,由于在線(xiàn)所提供的版本是 MSI 的封裝檔,安裝好之后也等同于在你的計(jì)算機(jī)內(nèi)安裝了一套 Apache HTTP Server,如果你不需要多執(zhí)行一套 Apache HTTP Server 的話(huà),你可以在安裝好之后進(jìn)入 C:\Program Files\Apache Group\Apache2\bin 目錄,找到 ab.exe 執(zhí)行檔,復(fù)制出來(lái)后再移除 Apache 安裝即可,因?yàn)?ab.exe 是可以獨(dú)立執(zhí)行的,不需要任何關(guān)連的 dll 檔。
底下是 ab.exe 的使用參數(shù)摘要說(shuō)明,若要看詳細(xì)說(shuō)明可以看這里(或中文翻譯):
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 da
-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 da
-e filename Output CSV file with percentages served
-h Display usage information (this message)
而我經(jīng)常使用的參數(shù)摘要如下:
1. 同時(shí) 10 個(gè)聯(lián)機(jī),連續(xù)點(diǎn)擊 10000 次 ( 每個(gè) Request 執(zhí)行完畢后都會(huì)自動(dòng)斷線(xiàn),然后再重新聯(lián)機(jī) )
ab -n 10000 -c 10 http://www.example.com/index.aspx
2. 同時(shí) 10 個(gè)聯(lián)機(jī),連續(xù)點(diǎn)擊 10000 次,并且使用 Keep-Alive 方式聯(lián)機(jī)(當(dāng) Web Server 有支持 Keep-Alive 功能時(shí) ApacheBench 會(huì)在同一個(gè)聯(lián)機(jī)下連續(xù)點(diǎn)擊該網(wǎng)頁(yè))
ab -n 10000 -c 10 -k http://www.example.com/index.aspx
3. 將測(cè)試的效能原始資料匯出成 CSV 文件
ab -e output.csv -n 10000 -c 10 http://www.example.com/index.aspx
匯出的 output.csv 內(nèi)容如下:
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
......
上表所代表的每一列代表送出的百分比,第二個(gè)字段是當(dāng)下的 "Time per request" (每個(gè)要求所花費(fèi)的時(shí)間),單位是豪秒(millisecond)。
如何有效的檢視結(jié)果
壓力測(cè)試的核心在于如何分析結(jié)果,底下我用一個(gè)測(cè)試的結(jié)果說(shuō)明每個(gè)字段所代表的意義。如果你只要看重點(diǎn)的話(huà),可以看 Failed requests、Requests per second、與 Time per request 這三個(gè)參數(shù)也就差不多夠了。其中的 Failed requests 的數(shù)量太高的話(huà),很有可能代表你的 Web Application 的穩(wěn)定度不夠,而導(dǎo)致使用大量要求時(shí)無(wú)法響應(yīng)需求 。而 Request per second 代表你每表可送出的響應(yīng)數(shù)有多少,代表你 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
如上表顯示的結(jié)果來(lái)說(shuō)明,我一個(gè)字段一個(gè)字段的講解如下:
* Server Software: Web主機(jī)的操作系統(tǒng)與版本(若Web主機(jī)設(shè)定關(guān)閉此信息則無(wú))
* Server Hostname: Web主機(jī)的IP地址(Hostname)
* Server Port: Web主機(jī)的連接端口(Port)
* Document Path: 測(cè)試網(wǎng)址的路徑部分
* Document Length: 測(cè)試網(wǎng)頁(yè)響應(yīng)的網(wǎng)頁(yè)大小
* Concurrency Level: 同時(shí)進(jìn)行壓力測(cè)試的人數(shù)
* Time taken for tests: 本次壓力測(cè)試所花費(fèi)的總秒數(shù)
* Complete requests: 完成的要求數(shù)(Requests)
* Failed requests: 失敗的要求數(shù)(Requests)
* Write errors: 寫(xiě)入失敗的數(shù)量
* Total transferred: 本次壓力測(cè)試的總數(shù)據(jù)傳輸量(包括 HTTP Header 的數(shù)據(jù)也計(jì)算在內(nèi))
* HTML transferred: 本次壓力測(cè)試的總數(shù)據(jù)傳輸量(僅計(jì)算回傳的 HTML 的數(shù)據(jù))
* Requests per second: 平均每秒可響應(yīng)多少要求
* Time per request: 平均每個(gè)要求所花費(fèi)的時(shí)間(單位: 豪秒)
* Time per request: 平均每個(gè)要求所花費(fèi)的時(shí)間,跨所有同時(shí)聯(lián)機(jī)數(shù)的平均值(單位: 豪秒)
* Transfer rate: 從 ab 到 Web Server 之間的網(wǎng)絡(luò)傳輸速度
最后的 Connection Times (ms) 指的是壓力測(cè)試時(shí)的聯(lián)機(jī)處理時(shí)間:
橫軸字段的部分:
* min: 最小值
* mean: 平均值(正、負(fù)標(biāo)準(zhǔn)差)
* median: 平均值(中間值)
* max: 最大值
縱軸字段的部分:
* Connect: 從 ab 發(fā)出 TCP 要求到 Web 主機(jī)所花費(fèi)的建立時(shí)間。
* Processing: 從 TCP 聯(lián)機(jī)建立后,直到 HTTP 響應(yīng)(Response)的數(shù)據(jù)全部都收到所花的時(shí)間。
* Waiting: 從發(fā)送 HTTP 要求完后,到 HTTP 響應(yīng)(Response)第一個(gè) Byte 所等待的時(shí)間。
* Total: 等于 Connect + Processing 的時(shí)間(因?yàn)?Waiting 包含在 Processing 時(shí)間內(nèi)了)
壓力測(cè)試的基本觀念
* 排除頻寬的限制
o 做壓力測(cè)試通常不會(huì)考慮「頻寬的限制」,所以一般來(lái)說(shuō)不會(huì)將測(cè)試的主機(jī)擺在遠(yuǎn)程機(jī)房、然后測(cè)試程序擺在公司內(nèi)部的主機(jī),而是會(huì)將壓力測(cè)試的 Client 跟 Web 主機(jī)擺在同一個(gè)網(wǎng)段下進(jìn)行壓力測(cè)試。
o 因?yàn)椤割l寬」只要花錢(qián)就會(huì)有了,但是主機(jī)的承載量卻是有限的,從遠(yuǎn)程進(jìn)行壓力測(cè)試主要的限制是在「頻寬」而非「效能」,所以從遠(yuǎn)程單點(diǎn)進(jìn)行壓力測(cè)試毫無(wú)任何意義可言,這樣是測(cè)不出主機(jī)的效能極限的。
o 如果你有能力與資源進(jìn)行大規(guī)模(多點(diǎn))壓力測(cè)試的話(huà),透過(guò)遠(yuǎn)程進(jìn)行壓力測(cè)試才有意義。
* 壓力要循序漸進(jìn)
o 你不要一下字就執(zhí)行同時(shí)聯(lián)機(jī)數(shù) 100 人,而是要循序漸進(jìn)的慢慢加同時(shí)聯(lián)機(jī)數(shù)上去,才不會(huì)讓 Web Application 一下字承受過(guò)大的負(fù)載而導(dǎo)致效能的數(shù)據(jù)不正確(例如說(shuō) Failed requests 過(guò)高),但這只是建議,你也可以一下子操死你的主機(jī),反正你在測(cè)主機(jī)的極限嘛!
聯(lián)系客服