| 基準測試載入器(ab) ab是什嗎? ab是基於命令列的工具,均可運行在windows、linux平台下。 為什麼選用這個工具? 便於使用,輸入少量命令選項,即可得出結果,同時易於安裝。 安裝abwin:如果本地有apache的開發環境,那麼ab預設在apache目錄下的bin目錄下,開啟bin目錄可以看到 linux:直接yum install httpd-tools,安裝即可 簡單上手直接敲入下面代碼(注意切換ab所在的目錄) -c 10代表並發數是10 -n 10總共進行100次訪問 後面接要訪問的網址,切記不可缺少http://
- D:\phpStudy\Apache\bin>ab -c 10 -n 1000 http://example.com/phpinfo.php
複製代碼待ab自己跑一下,得出下面結果
- #ab工具的描述,注意其中的版本號碼
- This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
- Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
- Licensed to The Apache Software Foundation, http://www.apache.org/
- #測試結果,共測試1000次,每100次顯示訪問進度,訪問的網站是example.com
- Benchmarking example.com (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: nginx/1.6.2 #伺服器web軟體
- Server Hostname: example.com #主機地址
- Server Port: 80 #訪問連接埠
- #文檔資訊
- Document Path: /phpinfo.php #訪問的文檔
- Document Length: 94804 bytes #返回結果大小位元組數(包含html,js,css,圖片及響應中的所有內容位元組數總和)
- #連結資訊
- Concurrency Level: 10 #並發數,-c 10 設定了10個並發
- Time taken for tests: 7.498 seconds #總耗時,單位秒
- Complete requests: 1000 #總共請求中已完成的請求總數的和
- Failed requests: 219 #失敗的請求數總和
- (Connect: 0, Receive: 0, Length: 219, Exceptions: 0)
- Non-2xx responses: 9 #未收到2xx系列成功的請求總數
- Total transferred: 94135779 bytes #整個請求總數中響應總數距的總大小位元組數,包含頭資訊
- HTML transferred: 93952076 bytes #整個請求總數中本文內容的總大小位元組
- Requests per second: 133.36 [#/sec] (mean) #每秒支援的並發數,這裡是每秒支援133.36個並發
- Time per request: 74.984 [ms] (mean) #完成一個請求的總耗時
- Time per request: 7.498 [ms] (mean, across all concurrent requests) #完成所有並發請求中一個請求的總耗時
- Transfer rate: 12259.83 [Kbytes/sec] received #每秒收到的位元組總數(KB)
- #測試的時間結果資料,留意Total一項,10個並發的情況下,完成一個請求,最小耗時(min)9毫秒,最大耗時(max)146毫秒
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 0 0.7 0 20
- Processing: 9 74 22.2 68 146
- Waiting: 6 73 22.1 67 143
- Total: 9 74 22.2 68 146
- #完成請求百分比,例如第一項,50% 68,代表一半的請求在68毫秒內完成,95% 129,代表1000的請求裡,百分之九十八的請求在129毫秒裡完成
- Percentage of the requests served within a certain time (ms)
- 50% 68
- 66% 78
- 75% 86
- 80% 92
- 90% 109
- 95% 122
- 98% 129
- 99% 133
- 100% 146 (longest request)
複製代碼ab選項輸入下面命令得到ab協助文檔,該協助文檔,分別說明了用法和命令選項的含義
- D:\phpStudy\Apache\bin>ab -h
複製代碼ab協助文檔的輸出
- Usage: ab [options] [http://]hostname[:port]/path
- Options are:
- -n requests Number of requests to perform
- -c concurrency Number of multiple requests to make at a time
- -t timelimit Seconds to max. to spend on benchmarking
- This implies -n 50000
- -s timeout Seconds to max. wait for each response
- Default is 30 seconds
- -b windowsize Size of TCP send/receive buffer, in bytes
- -B address Address to bind to when making outgoing connections
- -p postfile File containing data to POST. Remember also to set -T
- -u putfile File containing data to PUT. Remember also to set -T
- -T content-type Content-type header to use for POST/PUT data, eg.
- 'application/x-www-form-urlencoded'
- Default is 'text/plain'
- -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.
- -q Do not show progress when doing more than 150 requests
- -l Accept variable document length (use this for dynamic pages)
- -g filename Output collected data to gnuplot format file.
- -e filename Output CSV file with percentages served
- -r Don't exit on socket receive errors.
- -m method Method name
- -h Display usage information (this message)
複製代碼英文看不懂,附上中文解釋,網上抄的,我沒試過
- -n 即requests,用於指定壓力測試總共的執行次數。
- -c 即concurrency,用於指定壓力測試的並發數。
- -t 即timelimit,等待響應的最大時間(單位:秒)。
- -b 即windowsize,TCP發送/接收的緩衝大小(單位:位元組)。
- -p 即postfile,發送POST請求時需要上傳的檔案,此外還必須設定-T參數。
- -u 即putfile,發送PUT請求時需要上傳的檔案,此外還必須設定-T參數。
- -T 即content-type,用於設定Content-Type要求標頭資訊,例如:application/x-www-form-urlencoded,預設值為text/plain。
- -v 即verbosity,指定列印協助資訊的冗餘層級。
- -w 以HTML表格形式列印結果。
- -i 使用HEAD請求代替GET請求。
- -x 插入字串作為table標籤的屬性。
- -y 插入字串作為tr標籤的屬性。
- -z 插入字串作為td標籤的屬性。
- -C 添加cookie資訊,例如:"Apache=1234"(可以重複該參數選項以添加多個)。
- -H 添加任意的要求標頭,例如:"Accept-Encoding: gzip",要求標頭將會添加在現有的多個要求標頭之後(可以重複該參數選項以添加多個)。
- -A 添加一個基本的網路認證資訊,使用者名稱和密碼之間用英文冒號隔開。
- -P 添加一個基本的代理認證資訊,使用者名稱和密碼之間用英文冒號隔開。
- -X 指定使用的Proxy 伺服器和連接埠號碼,例如:"126.10.10.3:88"。
- -V 列印版本號碼並退出。
- -k 使用HTTP的KeepAlive特性。
- -k 使用HTTP的KeepAlive特性。
- -d 不顯示百分比。
- -S 不顯示預估和警告資訊。
- -g 輸出結果資訊到gnuplot格式的檔案中。
- -e 輸出結果資訊到CSV格式的檔案中。
- -r 指定接收到錯誤資訊時不退出程式。
- -h 顯示用法資訊,其實就是ab -help。
複製代碼一般用-n,-c,-t就好了 -n請求總數 -c並發數 -t指定秒數內的並發數 -t的用法說一下,下面的意思是在20秒內並發數10個訪問
- D:\phpStudy\Apache\bin> ab -c 10 -t 20 http://example.com/phpinfo.php
- This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
- Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
- Licensed to The Apache Software Foundation, http://www.apache.org/
- Benchmarking example.com (be patient)
- Finished 3011 requests
- Server Software: nginx/1.6.2
- Server Hostname: example.com
- Server Port: 80
- Document Path: /phpinfo.php
- Document Length: 94804 bytes
- Concurrency Level: 10
- Time taken for tests: 20.006 seconds
- Complete requests: 3011
- Failed requests: 495
- (Connect: 0, Receive: 0, Length: 495, Exceptions: 0)
- Non-2xx responses: 54
- Total transferred: 280896464 bytes
- HTML transferred: 280344222 bytes
- Requests per second: 150.50 [#/sec] (mean)
- Time per request: 66.444 [ms] (mean)
- Time per request: 6.644 [ms] (mean, across all concurrent requests)
- Transfer rate: 13711.44 [Kbytes/sec] received
- Connection Times (ms)
- min mean[+/-sd] median max
- Connect: 0 0 0.5 0 10
- Processing: 6 66 17.6 63 203
- Waiting: 5 64 17.3 61 202
- Total: 6 66 17.6 63 203
- Percentage of the requests served within a certain time (ms)
- 50% 63
- 66% 69
- 75% 72
- 80% 75
- 90% 82
- 95% 92
- 98% 113
- 99% 125
- 100% 203 (longest request)
複製代碼特別說明:如果訪問http://www.example.com/,沒有指定特定檔案,記得要加最後的那個/,一定要加!!!否則可能會出現訪問不到的情況!!! 可能會影響基準測試的情況地理位置和網路問題 回應檔大小 代碼的複雜度 瀏覽器解析 Web伺服器的配置 地理位置和網路問題如果Web伺服器存放在境外,那麼我們從本地訪問境外的網站,可想而知,經過的路由器結點,伺服器結點,再通過太平洋海底的光纜,而後再訪問到Web伺服器,Web伺服器處理後,再繼續海底光纜,各個伺服器、路由結點的跳轉返回,這樣的話肯定會影響到測試結果(我的建議是,直接在伺服器上進行ab測試) 回應檔大小假如發送一個3MB的頁面,伺服器網卡會把3MB的資料拆分成單個很小的資料包,傳輸過程中,只有一個資料包損壞或丟失,則會重新發送所有的資料包,所以發送的包越小越好,同時,越小的資料轉送,越快傳輸到使用者的機器上。 代碼的複雜度代碼的複雜度即是對商務邏輯複雜的處理,還有對檔案調用、資料庫訪問、遠端API介面的調用等等都會影響處理時間 瀏覽器解析每個瀏覽器都有自己對js、css、html的處理方法,想想IE8以下和chrome的區別即可知道 Web伺服器設定一般Web伺服器安裝後,簡單設定,即可實現Web服務(開箱即用),但是這樣的設定沒有發揮Web伺服器的最大效能,需要資深的工程師對伺服器進行相關配置,使之發揮最大效能,這裡簡單實用Keep-Alive說明。 Keep-Alive的作用就是,Web伺服器開啟特定數量的串連,讓這些串連處於開啟狀態,使之能快速處理傳入的請求,這樣就不會為每一個傳入的請求開啟一個串連然後處理請求,減少伺服器的處理請求的進程數,從而增加並發數。 在ab中使用-k進行測試,例如下面這句
- #-k,允許伺服器儲存5個並發串連處於開啟並允許的狀態,讓其他請求快速處理,減少建立新請求的處理時間
- ab -c 5 -t 100 -k http://www.example.com/
複製代碼 |