原文:http://blog.linuxeye.com/124.html
Apache附帶的ab,它非常容易使用,ab可以直接在Web服務器本地發起測試請求。這至關重要,因為我們希望測試的服務器的處理時間,而不包含數據的網絡傳輸時間以及用戶PC本地的計算時間。
需要清楚的是,ab進行一切測試的本質都是基於HTTP,所以可以說它是對於Web服務器軟件的黑盒性能測試,它獲得的一切數據和計算結果,都可以通過HTTP來解釋。
另有一些壓力測試軟件,包括LoadRnner、Jmeter等,則是不同程度上包含了服務器處理之外的時間,比如LoadRunner運行在用戶PC上,可以錄制瀏覽器行為,這種測試的結果玩玩側重於站點用戶的角度,有另外一些層面的參考意義。
接下來,將使用ab來進行一次壓力測試,在本書中我們使用Apache 2.3中附帶的ab,其版本信息如下所示:
[root@localhost ~]#/usr/local/apache/bin/ab -VThis is ApacheBench,Version2.3<$Revision:655654 $>Copyright1996AdamTwiss,ZeusTechnologyLtd, http://www.zeustech.net/Licensed to TheApacheSoftwareFoundation, http://www.apache.org/
請看下面的命令行信息:
[root@localhost ~]# /usr/local/apache/bin/ab -n1000 -c10 http://localhost/index.htmlThis is ApacheBench,Version2.3<$Revision:655654 $>Copyright1996AdamTwiss,ZeusTechnologyLtd, http://www.zeustech.net/Licensed to TheApacheSoftwareFoundation, http://www.apache.org/Benchmarking localhost (be patient)Completed100 requests Completed200 requests Completed300 requests Completed400 requests Completed500 requests Completed600 requests Completed700 requests Completed800 requests Completed900 requests Completed1000 requests Finished1000 requests ServerSoftware:Apache/2.2.19ServerHostname: localhost ServerPort:80DocumentPath:/index.html DocumentLength:45 bytes ConcurrencyLevel:10Time taken for tests:0.454 seconds Complete requests:1000Failed requests:0Write errors:0Total transferred:322644 bytes HTML transferred:45090 bytes Requests per second:2204.64[#/sec] (mean)Time per request:4.536[ms](mean)Time per request:0.454[ms](mean, across all concurrent requests)Transfer rate:694.64[Kbytes/sec] received ConnectionTimes(ms) min mean[+/-sd] median max Connect:010.515Processing:135.6262Waiting:035.6262Total:145.6363Percentage of the requests served within a certain time (ms)50%366%475%480%490%695%1298%2699%33100%63(longest request)
請注意我們在啟動ab時,傳入3個命令行參數,它們正是代表了前面提到的前提條件:
-n1000 表示總請求數位1000
-c 表示並發用戶數為10
http://localhost/index.html 表示這些請求的目標URL。
測試結果一目了然,我們看到吞吐率顯示為2204.64reqs/s。同時,在測試結果中還有一些其他內容也值得我們關注,主要包括:
Server Software
表示被測試的Web服務器軟件名稱,這里是Apache/2.2.19,它來自於http響應數據的頭信息,所以如果是我們自己編寫的Web服務器軟或者修改開源Web服務器軟件的源代碼,便可以隨意改寫這里的名稱。
vi /usr/local/apache/conf/httpd.conf #隱藏具體版本信息
ServerSignature Off
ServerTokens Prod
Server Hostname
表示請求的URL中的主機部分名稱,它來自於http請求數據的頭信息,這里我們請求的URL是http://localhost/index.html,所以主機名為localhost,說明我們的請求是從Web服務器端發起的。
Server Port
表示被測試的Web服務器軟件的監聽端口,為了方便測試,我們后面會對多個不同的Web服務器軟件使用不同的監聽端口。
Document Path
表示請求的URL中根絕對路徑,它同樣來自於http請求數據的頭信息,通過它的后綴名,我們一般可以理解該請求的類型。
Document Length
表示http響應數據的正文長度。
Concurrency Level
表示並發用戶數,這是我們設置的參數。
Time taken for tests
表示所有這些請求被處理完成花費的總時間。順便提一下,某些Apache版本如2.2.4附帶的ab,對於這一統計項存在一些計算上的bug,當總請求數較少時,其統計的總時間會無法小於0.1s。
Complete requests
表示總請求數,這是我們設置的相應參數。
Failed requests
表示失敗的請求數,這里的失敗是指請求的連接服務器、發送數據、接收數據等環節發生異常,以及無響應后超時的情況。對於超時時間的設置可以用ab的-t參數。
而如果接受到的http響應數據的頭信息中含有2xx以外的狀態碼,則會在測試結果顯示另一個名為“Non-2xx responses”的統計項,用於統計這部分請求數,這些請求並不算是失敗的請求。
Total transferred
表示所有請求的響應數據長度總和,包括每個http響應數據的頭信息和正文數據的長度。注意這里不包括http請求數據的長度,所以Total
transferred代表了從Web服務器流向用戶PC的應用層數據總長度。通過使用ab的-v參數即可查看詳細的http頭信息。
HTML transferred
表示所有請求的響應數據中正文數據的總和,也就是減去了Total transferred中http響應數據中頭信息的長度。
Requests per second
這便是我們重點關注的吞吐率,它等於:
Complete requests / Time taken for tests
Time per request
這便是前面提到的用戶平均請求等待時間,它等於:
Time taken for tests / (Complete requests /Concurrency Level)
Time per request?(across all concurrent requests)
這便是前面提到的服務器平均請求處理時間,它等於:
Time taken for tests / Complete requests
這正是吞吐率的倒數。同時,它也等於:
Time per request / Concurrency Level
Transfer rate
表示這些請求在單位時間內從服務器獲取的數據長度,它等於:
Total transferred / Time taken for tests
這個統計項可以很好的說明服務器在處理能力達到限制時,其出口帶寬的需求量。
利用前面介紹的有關帶寬的知識,不難計算出結果。
Percentage of the requests served within a certain time(ms)
這部分數據用於描述每個請求處理時間的分布情況,比如在以上測試結果中,80%請求的處理時間都不超過1ms,而99%的請求都不超過2ms。注意這里的處理時間,是指前面的Time per request,即對於單個用戶而言,平均每個請求處理的時間。
繼續壓力測試
下面,我們再來進行一次壓力測試,此時並發用戶數為100,其他條件不變,測試結果如下所示:
[root@localhost ~]# /usr/local/apache/bin/ab -n1000 -c100 http://localhost/index.htmlThis is ApacheBench,Version2.3<$Revision:655654 $>Copyright1996AdamTwiss,ZeusTechnologyLtd, http://www.zeustech.net/Licensed to TheApacheSoftwareFoundation, http://www.apache.org/Benchmarking localhost (be patient)Completed100 requests Completed200 requests Completed300 requests Completed400 requests Completed500 requests Completed600 requests Completed700 requests Completed800 requests Completed900 requests Completed1000 requests Finished1000 requests ServerSoftware:Apache/2.2.19ServerHostname: localhost ServerPort:80DocumentPath:/index.html DocumentLength:45 bytes ConcurrencyLevel:100Time taken for tests:0.369 seconds Complete requests:1000Failed requests:0Write errors:0Total transferred:283648 bytes HTML transferred:46080 bytes Requests per second:2711.45[#/sec] (mean)Time per request:36.881[ms](mean)Time per request:0.369[ms](mean, across all concurrent requests)Transfer rate:751.07[Kbytes/sec] received ConnectionTimes(ms) min mean[+/-sd] median max Connect:0123.41219Processing:112318.917122Waiting:61918.313114Total:203517.130128Percentage of the requests served within a certain time (ms)50%3066%3275%3480%3590%5395%7598%9999%114100%128(longest request)
和前一次的測試結果相比,可以看出,當並發用戶數據從原來的10變為100后,吞吐率從原來的2204.64增長到了2711.45,服務器平均請求處理
時間從原來的0.454ms降到了0.369ms,而用戶評價請求等待時間從原來的4.536ms增加到了36.881ms.
可見,隨着並發用戶數的變化,吞吐率、用戶平均請求等待時間、服務器配件請求處理時間都發生了相應的變化。