1 環境搭建
wget http://mirrors.cnnic.cn/apache//httpd/httpd-2.4.7.tar.gz tar zxvf httpd-2.4.7.tar.gz cd httpd-2.4.7 ./configure make –j4 cd support ls ./ab -h |
隻需要把ab編譯出來即可,路徑在HOME_APACHE/support,同樣有對ab.c檔案進行修改,也隻需編譯即可。若有完全安裝則在bin目錄下也可以見到。
2 介紹
2.1 指令格式
./ab [options] [http://]hostname[:port]/path |
如:./ab -n 1000 -c 50 -r http://www.csdn.net/
2.2 指令參數
-n requests Number of requests to perform
在測試會話中所執行的請求個數。預設時,僅執行一個請求。
-c concurrency Number of multiplerequests to make
一次産生的請求個數。預設是一次一個。
-t timelimit Seconds to max. wait for responses
測試所進行的最大秒數。其内部隐含值是-n 50000。它可以使對伺服器的測試限制在一個固定的總時間以内。預設時,沒有時間限制。
-b windowsize Size of TCP send/receive buffer, in bytes
發送和接收封包的大小設定。
-u putfile File containing data to PUT. Rememberalso to set –T
包含了需要PUT的資料檔案,同時需要設定-T選項。
-p postfile File containing data to POST. Rememberalso to set -T
包含了需要POST的資料的檔案。
-T content-type Content-typeheader for POSTing, eg.
'application/x-www-form-urlencoded'
Default is 'text/plain'
POST資料所使用的Content-type頭資訊。
-v verbosity How much troubleshooting info to print
設定顯示資訊的詳細程度 - 4或更大值會顯示頭資訊, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他資訊。 -V 顯示版本号并退出。
-w Print out results in HTML tables
以HTML表的格式輸出結果。預設時,它是白色背景的兩列寬度的一張表。
-i Use HEAD instead of GET
執行HEAD請求,而不是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)
-C cookie-name=value 對請求附加一個Cookie:行。 其典型形式是name=value的一個參數對。此參數可以重複。
-H attribute Add Arbitrary header line, eg.'Accept-Encoding: gzip'
Inserted after all normalheader lines. (repeatable)
-A attribute Add Basic WWW Authentication, theattributes
are a colon separated usernameand password.
-P attribute Add Basic Proxy Authentication, theattributes
are a colon separated usernameand password.
-P proxy-auth-username:password 對一個中轉代理提供BASIC認證信任。使用者名和密碼由一個:隔開,并以base64編碼形式發送。無論伺服器是否需要(即, 是否發送了401認證需求代碼),此字元串都會被發送。
-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 servedtable.
-S Do not show confidence estimatorsand warnings.
-g filename Output collected data to gnuplot formatfile.
-e filename Output CSV file with percentages served
-r Don't exit on socket receiveerrors.
忽略socket接收的錯誤,繼續壓測,不然遇到錯誤會直接退出。
-h Display usage information (thismessage)
-attributes 設定 屬性的字元串。缺陷程式中有各種靜态聲明的固定長度的緩沖區。另外,對指令行參數、伺服器的響應頭和其他外部輸入的解析也很簡單,這可能會有不良後果。它沒有完整地實作HTTP/1.x; 僅接受某些'預想'的響應格式。 strstr(3)的頻繁使用可能會帶來性能問題,即, 你可能是在測試ab而不是伺服器的性能。
3 結果分析
This is ApacheBench, Version 2.3<$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus TechnologyLtd, http://www.zeustech.net/
Licensed to The Apache Software Foundation,http://www.apache.org/
Benchmarking www.vmall.com (bepatient).....done
Server Software: ngx_openresty
表示被測試的Web伺服器軟體名稱。
Server Hostname: www.csdn.net
表示請求的URL中的主機部分名稱。
Server Port: 80
表示被測試的Web伺服器軟體的監聽端口。
Document Path: /
表示請求的URL中根絕對路徑。
Document Length: 73225 bytes
表示http響應資料的正文長度。
Concurrency Level: 10
表示并發使用者數,這是我們設定的參數。
Time taken for tests: 0.086 seconds
表示所有這些請求被處理完成花費的總時間。順便提一下,某些Apache版本如2.2.4附帶的ab,對于這一統計項存在一些計算上的bug,當總請求數較少時,其統計的總時間會無法小于0.1s。
Complete requests: 100
表示完成的請求數量,這是我們設定的相應參數。
Failed requests: 0
表示失敗的請求數量,這裡的失敗是指請求的連接配接伺服器、發送資料、接收資料等環節發生異常,以及無響應後逾時的情況。對于逾時時間的設定可以用ab的-t參數。而如果接受到的http響應資料的頭資訊中含有2xx以外的狀态碼,則會在測試結果顯示另一個名為“Non-2xxresponses”的統計項,用于統計這部分請求數,這些請求并不算是失敗的請求。若有失敗請求還會顯示(Connect: 0, Receive: 0, Length: 32951, Exceptions: 0):Exceptions表示除了前面3種失敗;Length表示與第一次請求的長度不一緻,這個在公台請求的頁面是正确的。
Write errors: 0
Total transferred: 7346200 bytes
表示所有請求的響應資料長度總和,包括每個http響應資料的頭資訊和正文資料的長度。注意這裡不包括http請求資料的長度,是以Total transferred代表了從Web伺服器流向使用者PC的應用層資料總長度。通過使用ab的-v參數即可檢視詳細的http頭資訊。
HTML transferred: 7322500 bytes
表示所有請求的響應資料中正文資料的總和,也就是減去了Total transferred中http響應資料中頭資訊的長度。
Requests per second: 1164.84 [#/sec] (mean)
這便是我們重點關注的吞吐率,它等于:Complete requests / Timetaken for tests。
Time per request: 8.585 [ms] (mean)
這便是前面提到的使用者平均請求等待時間,它等于:Time taken for tests /(Complete requests /Concurrency Level)。
Time per request: 0.858 [ms] (mean, across all concurrentrequests)
這便是前面提到的伺服器平均請求處理時間,它等于:Time taken for tests/ Complete requests。這正是吞吐率的倒數。同時,它也等于:Time per request / Concurrency Level。
Transfer rate: 83565.60 [Kbytes/sec] received
表示這些請求在機關時間内從伺服器擷取的資料長度,它等于:Total transferred/ Time taken for tests這個統計項可以很好的說明伺服器在處理能力達到限制時,其出口帶寬的需求量。利用前面介紹的有關帶寬的知識,不難計算出結果。
Connection Times (ms)
min mean[+/-sd] median max
Connect: 1 1 0.2 1 2
Processing: 6 7 0.8 7 9
Waiting: 1 1 0.3 1 2
Total: 7 8 0.9 8 11
網絡上消耗的時間的分解
Percentage of the requests served within acertain time (ms)
50% 8
66% 8
75% 9
80% 9
90% 10
95% 10
98% 10
99% 11
100% 11 (longest request)
這部分資料用于描述每個請求處理時間的分布情況,比如在以上測試結果中,80%請求的處理時間都不超過9ms,而99%的請求都不超過11ms。注意這裡的處理時間,是指前面的Time per request,即對于單個使用者而言,平均每個請求處理的時間。