使用ab命令進行web server性能測試

  ApacheBench 是 Apache 服務器自帶的一個web壓力測試工具,簡稱ab。ab命令會建立多個併發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問。它的測試目標是基於URL的,所以,既能夠用來測試Apache的負載壓力,也能夠測試nginx、lighthttp、tomcat、IIS等其它Web服務器的壓力。ab命令對發出負載的計算機要求很低,既不會佔用很高CPU,也不會佔用不少內存,但卻會給目標服務器形成巨大的負載,可能會形成目標服務器資源耗盡,嚴重時可能會致使死機。html

  使用 ab -h 命令查看其支持的參數,以下所示:nginx

Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number of multiple requests to make
    -t timelimit    Seconds to max. wait for responses
    -b windowsize   Size of TCP send/receive buffer, in bytes
    -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 for POSTing, 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.
    -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.
    -h              Display usage information (this message)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol (SSL2, SSL3, TLS1, or ALL)


  下面看一個例子:web

[root@nginx-dev ~]# ab -n 20000 -c 200 -p "post.txt" -T "application/json" "xxxxxxxxxxxxxxx.ad-survey.com/store/match"
Benchmarking xxxxxxxxxxxxxxx.ad-survey.com (be patient)
Completed 2000 requests
Completed 4000 requests
Completed 6000 requests
Completed 8000 requests
Completed 10000 requests
Completed 12000 requests
Completed 14000 requests
Completed 16000 requests
Completed 18000 requests
Completed 20000 requests
Finished 20000 requests


Server Software:        nginx
Server Hostname:        xxxxxxxxxxxxxxx.ad-survey.com
Server Port:            80

Document Path:          /store/match
Document Length:        5961 bytes

Concurrency Level:      200
Time taken for tests:   97.578 seconds
Complete requests:      20000
Failed requests:        16525
   (Connect: 0, Receive: 0, Length: 16525, Exceptions: 0)
Total transferred:      84152648 bytes
Total body sent:        59680000
HTML transferred:       80532648 bytes
Requests per second:    204.97 [#/sec] (mean)
Time per request:       975.775 [ms] (mean)
Time per request:       4.879 [ms] (mean, across all concurrent requests)
Transfer rate:          842.21 [Kbytes/sec] received
                        597.28 kb/s sent
                        1439.49 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.6      0      30
Processing:    17  968 895.8    793    7349
Waiting:       17  968 895.7    793    7349
Total:         18  968 895.7    793    7349

Percentage of the requests served within a certain time (ms)
  50%    793
  66%   1110
  75%   1378
  80%   1591
  90%   2160
  95%   2611
  98%   3162
  99%   3640
 100%   7349 (longest request)

  以上命令中用到的幾個參數說明下:apache

    -n 20000:請求總數爲20000json

    -c 200:併發數爲200windows

    -p "post.txt":POST請求攜帶的數據在當前目錄的"post.txt"文件中tomcat

    -T "application/json":POST數據所使用的Content-type頭信息服務器

 

  測試結果解釋說明cookie

#服務器軟件及其版本號 Server Software:        nginx
#服務器主機名
Server Hostname:        xxx-xxxxxxxxxxx.ad-survey.com
#服務器端口號
Server Port:            80

#請求URI,也就是請求的頁面文檔地址
Document Path:          /store/match
#頁面文檔大小,其實是第一個成功返回的文檔的字節大小。若是在測試期間文檔長度發生變化,則認爲響應是錯誤的。
Document Length:        5961 bytes

#併發數 Concurrency Level:      200
#整個測試持續的時間 Time taken for tests:   97.578 seconds
#完成的請求數量 Complete requests:      20000
#失敗的請求數量
#若是該數字大於零,將打印另外一行,顯示因爲鏈接、讀取、內容長度不正確或異常而失敗的請求的數量。
#須要注意的是,若是後續請求的響應大小與第一個請求的響應大小(Document Length值)不一樣,則此請求會被視爲失敗。所以,若是測試請求的響應大小不固定的話,
#Length失敗請求數應該忽略!! Failed requests:
16525 (Connect: 0, Receive: 0, Length: 16525, Exceptions: 0) #從服務器接收的字節總數。這個數字其實是經過網絡發送的字節數。 Total transferred: 84152648 bytes #測試期間發送的字節總數。若是測試沒有包含要發送的正文,就不會有這個字段。應該是POST請求的數據大小(全部請求之和) Total body sent: 59680000 #從服務器接收的文檔字節總數。這個數字不包括HTTP報頭中的字節數 HTML transferred: 80532648 bytes #吞吐率,指的是某個併發用戶數下單位時間內處理的請求數【第一性能指標】 計算公式:總請求數 / 處理完成這些請求數所花費的時間,即Request per second = Complete requests / Time taken for tests Requests per second: 204.97 [#/sec] (mean) #用戶平均請求等待時間【第二性能指標】 #計算公式:處理完成全部請求數所花費的時間 /(總請求數 / 併發用戶數),即Time per request = Time taken for tests /( Complete requests / Concurrency Level) Time per request: 975.775 [ms] (mean) #服務器平均請求處理時間【第三性能指標】 #計算公式:處理完成全部請求數所花費的時間 / 總請求數,即Time taken for tests / Complete requests Time per request: 4.879 [ms] (mean, across all concurrent requests) #傳輸速率 Transfer rate: 842.21 [Kbytes/sec] received 597.28 kb/s sent 1439.49 kb/s total #消耗時間的分解、統計 #表示在Connect,Processing,Waiting,Total各階段的耗時的最小值(min),平均值(mean),標準差([+/-sd]),中值(median),最大值(max)。 Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.6 0 30 Processing: 17 968 895.8 793 7349 Waiting: 17 968 895.7 793 7349 Total: 18 968 895.7 793 7349 #在特定時間內處理請求數的百分比 Percentage of the requests served within a certain time (ms) 50% 793 #50%請求的響應時間在793ms內 66% 1110 #66%請求的響應時間在1110ms內 75% 1378 #以此類推 80% 1591 90% 2160 95% 2611 98% 3162 99% 3640 100% 7349 (longest request)


 

  參考:網絡

  http://httpd.apache.org/docs/current/programs/ab.html

  https://www.jb51.net/article/28320.htm

相關文章
相關標籤/搜索