Apache附帶的ab工具(本機使用的PHP環境是WAMP集成環境,ab工具位於D:\wamp\bin\apache\Apache2.2.21\bin)非常容易使用,ab可以直接在Web服務器本地發起測試請求,這至關重要,因為有些時候我們需要測試的僅僅是服務器的處理性能,並不想摻雜着網絡傳輸時間的影響。ab進行一切測試的本質都是基於HTTP的,所以可以說ab對於Web服務器軟件的黑盒性能測試,獲得的一切數據和計算結果,都是可以通過HTTP來解釋的。
測試本機是否正確安裝ab工具,在power shell想將當前目錄定位到bin,輸入 .\ab –V 命令,如果安裝正確,則會將其版本信息打印出來。
PS D:\wamp\bin\apache\Apache2.2.21\bin> .\ab -V This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech Licensed to The Apache Software Foundation, http://www.apache.org/
好了,一切就緒,下面提供一個壓力測試的實例:
輸入命令 PS D:\wamp\bin\apache\Apache2.2.21\bin> .\ab -n1000 -c10 http://localhost/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (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 Completed 1000 requests Finished 1000 requests Server Software: Apache/2.2.21 Server Hostname: localhost Server Port: 80 Document Path: /index.php Document Length: 211 bytes Concurrency Level: 10 Time taken for tests: 0.496 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Non-2xx responses: 1000 Total transferred: 400000 bytes HTML transferred: 211000 bytes Requests per second: 2015.93 [#/sec] (mean) Time per request: 4.960 [ms] (mean) Time per request: 0.496 [ms] (mean, across all concurrent requests) Transfer rate: 787.47 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.4 0 1 Processing: 2 5 1.1 4 12 Waiting: 2 4 1.1 4 12 Total: 2 5 1.1 5 12 Percentage of the requests served within a certain time (ms) 50% 5 66% 5 75% 5 80% 6 90% 6 95% 7 98% 8 99% 9 100% 12 (longest request)
下面開始解析這條命令語句:啟動ab,並出入三個參數(PS D:\wamp\bin\apache\Apache2.2.21\bin> .\ab -n1000 -c10 http://localhost/index.php )
-n1000 表示請求總數為1000
-c10 表示並發用戶數為10
http://localhost/index.php 表示這寫請求的目標URL
測試結果也一目了然,測試出的吞吐率為:Requests per second: 2015.93 [#/sec] (mean) 初次之外還有其他一些信息。
Server Software 表示被測試的Web服務器軟件名稱
Server Hostname 表示請求的URL主機名
Server Port 表示被測試的Web服務器軟件的監聽端口
Document Path 表示請求的URL中的根絕對路徑,通過該文件的后綴名,我們一般可以了解該請求的類型
Document Length 表示HTTP響應數據的正文長度
Concurrency Level 表示並發用戶數,這是我們設置的參數之一
Time taken for tests 表示所有這些請求被處理完成所花費的總時間
Complete requests 表示總請求數量,這是我們設置的參數之一
Failed requests 表示失敗的請求數量,這里的失敗是指請求在連接服務器、發送數據等環節發生異常,以及無響應后超時的情況。如果接收到的HTTP響應數據的頭信息中含有2XX以外的狀態碼,則會在測試結果中顯示另一個名為 “Non-2xx responses”的統計項,用於統計這部分請求數,這些請求並不算在失敗的請求中。
Total transferred 表示所有請求的響應數據長度總和,包括每個HTTP響應數據的頭信息和正文數據的長度。注意這里不包括HTTP請求數據的長度,僅僅為web服務器流向用戶PC的應用層數據總長度。
HTML transferred 表示所有請求的響應數據中正文數據的總和,也就是減去了Total transferred中HTTP響應數據中的頭信息的長度。
Requests per second 吞吐率,計算公式:Complete requests / Time taken for tests
Time per request 用戶平均請求等待時間,計算公式:Time token for tests/(Complete requests/Concurrency Level)
Time per requet(across all concurrent request) 服務器平均請求等待時間,計算公式:Time taken for tests/Complete requests,正好是吞吐率的倒數。也可以這么統計:Time per request/Concurrency Level
Transfer rate 表示這些請求在單位時間內從服務器獲取的數據長度,計算公式:Total trnasferred/ Time taken for tests,這個統計很好的說明服務器的處理能力達到極限時,其出口寬帶的需求量。
Percentage of requests served within a certain time(ms) 這部分數據用於描述每個請求處理時間的分布情況,比如以上測試,80%的請求處理時間都不超過6ms,這個處理時間是指前面的Time per request,即對於單個用戶而言,平均每個請求的處理時間。
未完待續。。。