【壓測】壓力測試ab的用法【原創】.md

簡介

ab(Apache Bench)是Apache附帶的壓力測試軟件,容易使用,功能徹底可以知足咱們的要求,安裝apache時會自帶該壓力測試軟件web

參數說明

ab自帶的參數說明:apache

[root@xiaozhu images]# ab -h
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)

ab的參數比較多,經常使用的就幾個:windows

  • -n :在測試會話中執行的請求個數,即請求數,默認執行一個請求
  • -c :要建立的併發用戶數,即併發數,默認建立一個用戶數
  • -t :等待Web服務器響應的最大時間(單位:秒),默認沒有時間限制
  • -k :使用Keep-Alive(持久鏈接)特性
  • -C :對請求附加一個Cookie,形式爲 name=value

用法

下面使用ab進行一次壓力測試:ruby

[root@xiaozhu images]# ab -c5 -n100 -k xxx.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking cms.dev.paidui.com (be patient).....done

Server Software: Tengine
Server Hostname: cms.dev.paidui.com
Server Port: 80

Document Path: /
Document Length: 30692 bytes

Concurrency Level: 5
Time taken for tests: 20.663 seconds
Complete requests: 100
Failed requests: 10
   (Connect: 0, Receive: 0, Length: 10, Exceptions: 0)
Write errors: 0
Non-2xx responses: 10
Keep-Alive requests: 10
Total transferred: 2881762 bytes
HTML transferred: 2822279 bytes
Requests per second: 4.84 [#/sec] (mean)
Time per request: 1033.169 [ms] (mean)
Time per request: 206.634 [ms] (mean, across all concurrent requests)
Transfer rate: 136.19 [Kbytes/sec] received

Connection Times (ms)
              min mean[+/-sd] median max
Connect: 0 5 2.1 6 11
Processing: 9 984 1007.0 739 7508
Waiting: 9 330 230.7 257 1278
Total: 9 989 1007.2 744 7513

Percentage of the requests served within a certain time (ms)
  50% 744
  66% 919
  75% 1130
  80% 1222
  90% 1772
  95% 2422
  98% 4720
  99% 7513
 100% 7513 (longest request)

測試結果一目瞭然,吞吐率爲4.84 reqs/s(吞吐率即單位時間內服務器處理的請求數)服務器

測試結果中關注如下內容:cookie

  • 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: 全部請求的響應數據中正文數據的總和,也就是減去Total transferred中HTTP響應數據中頭信息的長度
  • Requests per second: Web服務器的吞吐率,等於 Complete requests/Time taken for tests
  • Time per request: 用戶平均請求等待時間,等於 Time taken for tests/(Complete requests/Concurrency Level)
  • Transfer rate: 這些請求在單位時間內從服務器取得的數據長度,等於 Total transferred/Time taken for tests

持久鏈接

持久鏈接(Keep-Alive)又稱長鏈接,指TCP鏈接中連續發送多份數據而不斷開的鏈接,與之對應的稱爲短鏈接,就是鏈接後發送一次數據便斷開的鏈接。長鏈接對於密集型圖片或者網頁等小數據請求處理有明顯的加速做用。架構

在請求大量小文件時,長鏈接可以有效地減小從新創建鏈接的開銷。在啓動ab時加上-k參數可使用Keep-Alive模式進行測試併發

好比:app

先測試不使用長鏈接請求一個圖片:socket

[root@xiaozhu images]# ab -c10 -n1000 cms.dev.paidui.com/images/3gphost.jpg
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking cms.dev.paidui.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: Tengine
Server Hostname: cms.dev.paidui.com
Server Port: 80

Document Path: /images/3gphost.jpg
Document Length: 169 bytes

Concurrency Level: 10
Time taken for tests: 5.166 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 659912 bytes
HTML transferred: 169000 bytes
Requests per second: 193.59 [#/sec] (mean)
Time per request: 51.657 [ms] (mean)
Time per request: 5.166 [ms] (mean, across all concurrent requests)
Transfer rate: 124.76 [Kbytes/sec] received

Connection Times (ms)
        min mean[+/-sd] median max
Connect: 4 5 1.2 5 17
Processing: 14 46 26.9 39 219
Waiting: 14 46 26.9 39 219
Total: 19 51 26.9 44 224

Percentage of the requests served within a certain time (ms)
  50% 44
  66% 49
  75% 52
  80% 55
  90% 64
  95% 113
  98% 174
  99% 193
 100% 224 (longest request)

不使用長鏈接的時候吞吐率爲193.59

下面使用長鏈接:

[root@xiaozhu images]# ab -c10 -n1000 -k cms.dev.paidui.com/images/3gphost.jpg
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking cms.dev.paidui.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: Tengine
Server Hostname: cms.dev.paidui.com
Server Port: 80

Document Path: /images/3gphost.jpg
Document Length: 169 bytes

Concurrency Level: 10
Time taken for tests: 3.086 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Keep-Alive requests: 992
Total transferred: 648975 bytes
HTML transferred: 169000 bytes
Requests per second: 324.08 [#/sec] (mean)
Time per request: 30.856 [ms] (mean)
Time per request: 3.086 [ms] (mean, across all concurrent requests)
Transfer rate: 205.39 [Kbytes/sec] received

Connection Times (ms)
       min mean[+/-sd] median max
Connect: 0 0 0.8 0 7
Processing: 12 31 21.7 27 232
Waiting: 12 31 21.7 27 232
Total: 12 31 22.2 27 238

Percentage of the requests served within a certain time (ms)
  50% 27
  66% 31
  75% 34
  80% 35
  90% 40
  95% 46
  98% 53
  99% 220
 100% 238 (longest request)

使用長鏈接的吞吐率爲324,除此以外,Keep-Alive request爲992,即便用長鏈接的有992個請求,也就是說有992個請求重用同一個TCP鏈接。

參考資料

《PHP核心技術與最佳實踐》的第11章 高性能網站架構方案

相關文章
相關標籤/搜索