專題文章

網頁壓力測試工具 ab - Apache HTTP server benchmarking tool

作者:網管 於 2010-09-17
19831
次閱讀
ab是針對Apache網站伺服器進行HTTP基礎測試的工具,目的是讓您瞭解自己設定的apache參數能夠服務每秒用戶端請求數量。

網頁壓力測試工具 ab - Apache HTTP server benchmarking tool

ab跟abs這支程式是Apache網站伺服器附屬程式,主要是用來測試 Apache 網頁伺服器的效能,特別是可以看出Apache網頁伺服器每秒能送出多少網站,除了Apache以外,也可以用來測試其他的網頁伺服器,例如IIS,如果是進行遠端壓力測試,請記得將防火牆關閉,不然您測到的是防火牆阻擋後的結果。

如何在Windows環境下測試遠端Apache網頁

如果您的桌上型電腦效能與記憶體夠強大的話,我們可以使用一般桌型電腦進行壓力測試,大部份的用戶使用Windows系統,我們可以下載Windows版的Apache網站伺服器,由於下載的軟體已經被整套封裝,我們可以在裝完成Apache後,至Apache目錄下的bin找到ab.exe跟abs.exe,可以將這兩支程式複製出來單獨使用。

將ab.exe放置在C:\Users\XXX下,執行命令提示字元,在DOS模式下指令ab -h

出現錯誤訊息:電腦遺失MSVCR110.dll。這是因為ab使用到 Microsoft Visual C++ 可轉散發套件 2012 的程式。您可在 Microsoft 網頁下載此可轉散發套件的 x86 或 64 版本。視您的ab是32或64位元而定(不是依您系統位元),會需要安裝 32 位元或 64 位元版本。

請參閱以下連結: 下載適用於 Visual Studio 2012 Update 4 的 Visual C++ 可轉散發套件

原廠網頁連結:https://httpd.apache.org/docs/2.4/programs/ab.html

我們只要在命令提示字元下指令ab -h可以取得相關參數。
C:\Users\WR>ab -h
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)
相關中文說明如下:
C:\Users\WR>ab -h
用法: ab [參數] [http://]網址[:port]/網站路徑
參數 列表如下:
    -n requests     要執行的連線次數
    -c concurrency  同時提出多個連線數
    -t timelimit    設定每個基準可以使用最大秒數
                    This implies -n 50000
    -s timeout      接收每個回應最大秒數
                    Default is 30 seconds
    -b windowsize   設定TCP發送/接收緩衝區大小,單位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     以百分比方式產生CSV檔
    -r              Don't exit on socket receive errors.
    -m method       Method name
    -h              Display usage information (this message)
實作同時 10 個連線,連續點擊 500 次
ab -e report.csv -n 500 -c 10 http://baliplant.epd.ntpc.net.tw/tw/index.php
結果如下:
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking baliplant.epd.ntpc.net.tw (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests


Server Software:        Apache
Server Hostname:        baliplant.epd.ntpc.net.tw
Server Port:            80

Document Path:          /tw/index.php
Document Length:        18135 bytes

Concurrency Level:      10
Time taken for tests:   36.340 seconds
Complete requests:      500
Failed requests:        479
   (Connect: 0, Receive: 0, Length: 479, Exceptions: 0)
Total transferred:      580301 bytes
HTML transferred:       380835 bytes
Requests per second:    13.76 [#/sec] (mean)
Time per request:       726.800 [ms] (mean)
Time per request:       72.680 [ms] (mean, across all concurrent requests)
Transfer rate:          15.59 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        6   31  45.3     10     310
Processing:   110  691 589.3    486    3809
Waiting:      104  658 572.6    460    3759
Total:        124  722 588.2    530    3819

Percentage of the requests served within a certain time (ms)
  50%    530
  66%    710
  75%    830
  80%    950
  90%   1391
  95%   2046
  98%   2876
  99%   2972
 100%   3819 (longest request)
中文描述如下:
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking baliplant.epd.ntpc.net.tw (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests


網頁主機使用的系統: Apache
網頁主機名稱:      baliplant.epd.ntpc.net.tw
網頁主機連接埠:    80

測試網站的路徑:    /tw/index.php
網站的回應大小:    18135 bytes

同時壓力測試的連線數: 10
測試時間(秒):        36.340 seconds
完成請求數:          500
請求失敗數:          479 (註1)
            (接通: 0, 接收: 0, 長度: 479, 例外: 0)
數據總傳輸量:        580301 bytes
網站總傳輸量:        380835 bytes
每秒可回應多少請求:   13.76 [#/sec] (平均值)
每個請求所花費的時間:  726.800 [ms] (平均值)
每個請求所花費的時間:   72.680 [ms] (平均值, 含所有連線數)
測試端至主機間傳輸速度: 15.59 [Kbytes/sec] received

連接時間 (ms)
         最小值     平均值[+/-sd]      中間值     最大值
連接:        6         31  45.3          10        310
處理:      110        691 589.3         486       3809
等候:      104        658 572.6         460       3759
總計:      124        722 588.2         530       3819

每個百分比階段完成時間(千分之一秒)
  50%    530
  66%    710
  75%    830
  80%    950
  90%   1391
  95%   2046
  98%   2876
  99%   2972
 100%   3819 (最長的請求)
實作同時 10 個連線,連續點擊 1000 次
ab -e report.csv -n 1000 -c 10 http://baliplant.epd.ntpc.net.tw/tw/index.php
結果如下:
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking baliplant.epd.ntpc.net.tw (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
Server Hostname:        baliplant.epd.ntpc.net.tw
Server Port:            80

Document Path:          /tw/index.php
Document Length:        18135 bytes

Concurrency Level:      10
Time taken for tests:   71.321 seconds
Complete requests:      1000
Failed requests:        979
   (Connect: 0, Receive: 0, Length: 979, Exceptions: 0)
Total transferred:      780436 bytes
HTML transferred:       380835 bytes
Requests per second:    14.02 [#/sec] (mean)
Time per request:       713.210 [ms] (mean)
Time per request:       71.321 [ms] (mean, across all concurrent requests)
Transfer rate:          10.69 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:       10   33  49.2     12     346
Processing:    73  679 484.1    528    4333
Waiting:       73  637 476.9    484    4333
Total:        108  712 480.7    560    4345

Percentage of the requests served within a certain time (ms)
  50%    560
  66%    728
  75%    890
  80%   1010
  90%   1363
  95%   1625
  98%   2137
  99%   2511
 100%   4345 (longest request)

結論:
同時 10 個連線,連續點擊 500 次,完成時間3.819秒
同時 10 個連線,連續點擊 1000 次,完成時間4.345秒

註1:因為baliplant.epd.ntpc.net.tw/tw/index.php是動態產生之內容,當第一次發出 HTTP request 與後續發出的 HTTP request 所得到回應的 HTML 長度都是不同,就會引發 Failed requests 中 Length 問題,本次壓力測試所有的 Failed requests 都落在 Length 這個類別上,可以不予理會。