吞吐率和压力测试
吞吐率指的是單位時間內服務器處理的請求數,通常使用 reqs/s (服務器每秒處理的請求數量)來表示。
吞吐率只描述了服務器在實際運行期間單位時間內處理的請求數,而我們更加關心的是服務器并發處理能力的上限,即單位時間內服務器能夠處理的最大請求數(即最大吞吐率)。但在測試時,很難調動足夠多的人測試服務器的最大吞吐率。所以,需要使用某些方法模擬足夠數目的并發用戶數,這種方法稱為“壓力測試”。
壓力測試的工具很多,如LoadRunner、JMeter和ab等。由于LoadRunner和JMeter使用起來過于復雜,故這里我們介紹一種比較簡單的壓力測試工具ab。
ab(Apache Bench)是Apache附帶的一個壓力測試軟件。它操作起來比較簡單,功能基本能夠滿足我們的要求。
打開cmd,切換至Apache的bin目錄(如:我的是?E:\xampp\apache\bin )
cd?E:\xampp\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 benchmarkingThis implies -n 50000-s timeout Seconds to max. wait for each responseDefault 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 attributesare a colon separated username and password.-P attribute Add Basic Proxy Authentication, the attributesare 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)
ab的選項比較多,常用的有以下幾個: n ? 在測試會話中執行的請求的數目,默認執行一個請求。 c ? 要創建的并發用戶數,默認創建一個用戶數。 t ? 等待Web服務器響應的最大時間(單位:秒),默認沒有時間限制 k ? 使用Keep Alive(長連接)特性 c ? 對請求附加一個Cookie,形式為name=value
下面使用 ab 進行一次壓力測試: ab -n1000 -c10 demo.com/index.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 demo.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 requestsServer Software: Apache/2.4.10 Server Hostname: demo.com Server Port: 80Document Path: /index.php Document Length: 36 bytesConcurrency Level: 10 Time taken for tests: 0.452 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 254000 bytes HTML transferred: 36000 bytes Requests per second: 2210.43 [#/sec] (mean) Time per request: 4.524 [ms] (mean) Time per request: 0.452 [ms] (mean, across all concurrent requests) Transfer rate: 548.29 [Kbytes/sec] receivedConnection Times (ms)min mean[+/-sd] median max Connect: 0 0 1.8 0 16 Processing: 0 4 7.0 0 31 Waiting: 0 4 6.8 0 31 Total: 0 4 7.1 0 31Percentage of the requests served within a certain time (ms)50% 066% 075% 1680% 1690% 1695% 1698% 1699% 16100% 31 (longest request)
從測試結果可以看出,吞吐率為2210.43reqs/s。
測試結果中,我們通常會關注以下幾個內容: Server?Software:被測試的Web服務器軟件的名稱和版本。
Server Hostname:請求URL中的主機名稱。 Server Port:被測試的Web服務器的監聽端口。 Document Path:請求的URL的絕對路徑。 Document Length:HTTP響應數據的正文的長度。 Concurrency Level:并發的用戶數,設置的 c 參數。 Time taken for tests:所有請求處理完畢所花費的總時間。 Complete requests:總請求數,設置的 n 參數。 Failed requests:失敗的請求數。 Total transferred:所有請求的響應數據長度總和。 HTML transferred:所有請求的響應數據中正文數據的長度總和。 Requests per second:Web服務器的吞吐率,等于Complete requests/Time taken for tests。 Time per request:用戶平均每個請求的等待時間。 Transfer rate:這些請求在單位時間內從服務器取得的數據長度,等于Total transferred/Time taken for tests。
ab的選項比較多,常用的有以下幾個: n ? 在測試會話中執行的請求的數目,默認執行一個請求。 c ? 要創建的并發用戶數,默認創建一個用戶數。 t ? 等待Web服務器響應的最大時間(單位:秒),默認沒有時間限制 k ? 使用Keep Alive(長連接)特性 c ? 對請求附加一個Cookie,形式為name=value
下面使用 ab 進行一次壓力測試: ab -n1000 -c10 demo.com/index.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 demo.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 requestsServer Software: Apache/2.4.10 Server Hostname: demo.com Server Port: 80Document Path: /index.php Document Length: 36 bytesConcurrency Level: 10 Time taken for tests: 0.452 seconds Complete requests: 1000 Failed requests: 0 Total transferred: 254000 bytes HTML transferred: 36000 bytes Requests per second: 2210.43 [#/sec] (mean) Time per request: 4.524 [ms] (mean) Time per request: 0.452 [ms] (mean, across all concurrent requests) Transfer rate: 548.29 [Kbytes/sec] receivedConnection Times (ms)min mean[+/-sd] median max Connect: 0 0 1.8 0 16 Processing: 0 4 7.0 0 31 Waiting: 0 4 6.8 0 31 Total: 0 4 7.1 0 31Percentage of the requests served within a certain time (ms)50% 066% 075% 1680% 1690% 1695% 1698% 1699% 16100% 31 (longest request)
從測試結果可以看出,吞吐率為2210.43reqs/s。
測試結果中,我們通常會關注以下幾個內容: Server?Software:被測試的Web服務器軟件的名稱和版本。
Server Hostname:請求URL中的主機名稱。 Server Port:被測試的Web服務器的監聽端口。 Document Path:請求的URL的絕對路徑。 Document Length:HTTP響應數據的正文的長度。 Concurrency Level:并發的用戶數,設置的 c 參數。 Time taken for tests:所有請求處理完畢所花費的總時間。 Complete requests:總請求數,設置的 n 參數。 Failed requests:失敗的請求數。 Total transferred:所有請求的響應數據長度總和。 HTML transferred:所有請求的響應數據中正文數據的長度總和。 Requests per second:Web服務器的吞吐率,等于Complete requests/Time taken for tests。 Time per request:用戶平均每個請求的等待時間。 Transfer rate:這些請求在單位時間內從服務器取得的數據長度,等于Total transferred/Time taken for tests。
總結