ab命令

一、ab的原理

ab是apachebench命令的縮寫。

ab的原理:ab命令會創建多個併發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問。它的測試目標是基於URL的,因此,它既可以用來測試apache的負載壓力,也可以測試nginx、lighthttp、tomcat、IIS等其它Web服務器的壓力。

ab命令對發出負載的計算機要求很低,它既不會佔用很高CPU,也不會佔用很多內存。但卻會給目標服務器造成巨大的負載,其原理類似CC攻擊。自己測試使用也需要注意,否則一次上太多的負載。可能造成目標服務器資源耗完,嚴重時甚至導致死機。

 

二、ab的安裝

yum -y install httpd-tools
ab -V

 

三、ab參數說明

[root@myhost vhost]# ab --help
ab: wrong number of arguments
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 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
    -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
                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

 

四、ab實際使用

 

ab的命令參數比較多,我們經常使用的是-c和-n參數。

[root@myhost vhost]# ab -c 10000 -n 100000 http://www.baidu.com
-c表示併發用戶數
-n表示請求總數

 

五、輸出結果

Server Software:        nginx
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /wordpress
Document Length:        178 bytes   #請求大小

Concurrency Level:      10000       #併發請求量
Time taken for tests:   7.926 seconds    #整個測試持續時間
Complete requests:      100000          #完成的請求數
Failed requests:        0               #失敗的請求數
Write errors:           0
Non-2xx responses:      100000
Total transferred:      36800000 bytes    #整個場景中網絡傳輸量
HTML transferred:       17800000 bytes    #整個場景中的HTML內容傳輸量
Requests per second:    12615.91 [#/sec] (mean)  #吞吐速率,大家最關心的指標之一
Time per request:       792.650 [ms] (mean)    #用戶平均請求等待時間,大家最關心的指標之二
Time per request:       0.079 [ms] (mean, across all concurrent requests)  #服務器平均請求處理的時間,大家最關心的指標之三
Transfer rate:          4533.84 [Kbytes/sec] received  #平均每秒網絡上的流量,可以幫助排除是否有存在網絡流量過大導致響應時間延長的問題

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  510 1056.6      3    7026
Processing:     3   28  89.9      8    1889
Waiting:        0   25  89.4      6    1887
Total:          7  538 1079.8     12    7240

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     22
  75%   1016
  80%   1022
  90%   1231
  95%   3027
  98%   3232
  99%   3828
 100%   7240 (longest request)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章