ab性能測試工具


Apache自帶的壓力測試工具——ab初體驗

    我們知道壓力測試的軟件確實很多,諸如微軟的WAST,惠普的LoadRunner以及等等其他的,但這些軟件學習起來還是需要花費些時間,在選擇上實在頭痛,后來在郭欣的那本《構建高性能WEB站點》上看到了他介紹的這款Apache自帶的壓力測試工具ab,十分喜愛,於是今天終於有機會體驗下ab對網站的壓力測試。

    實驗之前我的apache已經安裝了,操作系統:Ubuntu 10.04 VMware 7.0

1、先查看一下版本信息 ab -V(注意是大寫的V)

  1. studiogang@studiogang:~$ ab -V
  2. This is ApacheBench, Version 2.3 <$Revision: 655654 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/

2、我們也可以使用小寫的v查看下ab命令的一些屬性 ab -v

  1. studiogang@studiogang:~$ ab -v
  2. ab: option requires an argument -- v
  3. ab: wrong number of arguments
  4. Usage: ab [options] [http[s]://]hostname[:port]/path
  5. Options are:
  6. -n requests Number of requests to perform
  7. -c concurrency Number of multiple requests to make
  8. -t timelimit Seconds to max. wait for responses
  9. -b windowsize Size of TCP send/receive buffer, in bytes
  10. -p postfile File containing data to POST. Remember also to set -T
  11. -u putfile File containing data to PUT. Remember also to set -T
  12. -T content-type Content-type header for POSTing, eg.
  13. 'application/x-www-form-urlencoded'
  14. Default is 'text/plain'
  15. -v verbosity How much troubleshooting info to print
  16. -w Print out results in HTML tables
  17. -i Use HEAD instead of GET
  18. -x attributes String to insert as table attributes
  19. -y attributes String to insert as tr attributes
  20. -z attributes String to insert as td or th attributes
  21. -C attribute Add cookie, eg. 'Apache=1234. (repeatable)
  22. -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
  23. Inserted after all normal header lines. (repeatable)
  24. -A attribute Add Basic WWW Authentication, the attributes
  25. are a colon separated username and password.
  26. -P attribute Add Basic Proxy Authentication, the attributes
  27. are a colon separated username and password.
  28. -X proxy:port Proxyserver and port number to use
  29. -V Print version number and exit
  30. -k Use HTTP KeepAlive feature
  31. -d Do not show percentiles served table.
  32. -S Do not show confidence estimators and warnings.
  33. -g filename Output collected data to gnuplot format file.
  34. -e filename Output CSV file with percentages served
  35. -r Don't exit on socket receive errors.
  36. -h Display usage information (this message)
  37. -Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers)
  38. -f protocol Specify SSL/TLS protocol (SSL2, SSL3, TLS1, or ALL)

3、現在我們就對51CTO的網站進行一次壓力測試吧,使用命令ab -n1000 -c10 http://www.51cto.com/index.php,其中 -n1000 表示總請求數 -c10表示並發用戶數為10 http://www.51cto.com/index.php 表示請求的URL,下面是測試的結果,其中我們最關心的三個指標,我已經注釋出來了。

 

  1. studiogang@studiogang:~$ ab -n1000 -c10 http://www.51cto.com/index.php
  2. This is ApacheBench, Version 2.3 <$Revision: 655654 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. Benchmarking www.51cto.com (be patient)
  6. Completed 100 requests
  7. Completed 200 requests
  8. Completed 300 requests
  9. Completed 400 requests
  10. Completed 500 requests
  11. Completed 600 requests
  12. Completed 700 requests
  13. Completed 800 requests
  14. Completed 900 requests
  15. Completed 1000 requests
  16. Finished 1000 requests
  17. /*WEB服務器用的是nginx*/
  18. Server Software: nginx
  19. Server Hostname: www.51cto.com
  20. Server Port: 80
  21. Document Path: /index.php
  22. Document Length: 154 bytes
  23. Concurrency Level: 10
  24. Time taken for tests: 74.373 seconds
  25. Complete requests: 1000
  26. Failed requests: 0
  27. Write errors: 0
  28. Non-2xx responses: 1000
  29. Total transferred: 330000 bytes
  30. HTML transferred: 154000 bytes
  31. /*大家最關心的指標之一,指的是吞吐率
  32. 相當於 LR 中的 每秒事務數 ,后面括號中的 mean 表示這是一個平均值*/
  33. Requests per second: 13.45 [#/sec] (mean)
  34. /*大家最關心的指標之二,指的是用戶平均請求等待時間
  35. 相當於 LR 中的 平均事務響應時間 ,后面括號中的 mean 表示這是一個平均值*/
  36. Time per request: 743.726 [ms] (mean)
  37. /*大家最關心的指標之三,指的是服務器平均請求處理時間
  38. Time per request: 74.373 [ms] (mean, across all concurrent requests)
  39. Transfer rate: 4.33 [Kbytes/sec] received
  40. Connection Times (ms)
  41. min mean[+/-sd] median max
  42. Connect: 129 163 245.3 145 3154
  43. Processing: 129 576 1510.8 147 11756
  44. Waiting: 129 567 1502.0 147 11756
  45. Total: 261 739 1543.7 294 11888
  46. Percentage of the requests served within a certain time (ms)
  47. 50% 294
  48. 66% 297
  49. 75% 304
  50. 80% 308
  51. 90% 1290
  52. 95% 3452
  53. 98% 7582
  54. 99% 7962
  55. 100% 11888 (longest request)

4、為了使結果更有對比性,我們將並發用戶更改為100個進行壓力測試,我這里只將三個指標貼出來。

  1. Requests per second: 190.95 [#/sec] (mean)
  2. Time per request: 523.694 [ms] (mean)
  3. Time per request: 5.237 [ms] (mean, across all concurrent requests)

5、將並發用戶改為200個進行測試

  1. Requests per second: 186.00 [#/sec] (mean)
  2. Time per request: 1149.433 [ms] (mean)
  3. Time per request: 5.747 [ms] (mean, across all concurrent requests)

6、500個並發用戶時的情況

  1. Requests per second: 180.99 [#/sec] (mean)
  2. Time per request: 2631.662 [ms] (mean)
  3. Time per request: 5.263 [ms] (mean, across all concurrent requests)

     我們來分析下測試的結果,先對比下吞吐率,當並發用戶的時候吞吐率最高為190 reqs/s,當並發用戶數為200,500 吞吐率下降了,隨之用戶的等待時間更是明顯增加了,已經有2s的等待時間了。這說明性能明顯下降了。當然分析這個測試結果並不是說明51CTO的網站的並發用戶只能在500左右,因為我是在服務器負荷的情況下就行測試的,這顯然不能說明問題。另外我們在生產環境下測試的時候,最好能將測試結果做成報表,這樣可以非常清晰地對比出問題來,好了,我該准備下,給上面提交一份我們公司網站的測試報告了。

http://studiogang.blog.51cto.com/505887/386852

 

 

網站壓力測試工具ab使用詳解

就是APACHE自帶的測試工具AB(apache benchmark).在APACHE的bin目錄下。
格式: ./ab [options] [http://]hostname[:port]/path
參數:
-n requests     Number of requests to perform
//在測試會話中所執行的請求個數。默認時,僅執行一個請求
-c concurrency Number of multiple requests to make
//一次產生的請求個數。默認是一次一個。 
-t timelimit    Seconds to max. wait for responses
//測試所進行的最大秒數。其內部隱含值是-n 50000。它可以使對服務器的測試限制在一個固定的總時間以內。默認時,沒有時間限制。
    -p postfile     File containing data to POST
//包含了需要POST的數據的文件. 
-T content-type Content-type header for POSTing
//POST數據所使用的Content-type頭信息。
-v verbosity    How much troubleshooting info to print
//設置顯示信息的詳細程度 - 4或更大值會顯示頭信息, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。 -V 顯示版本號並退出。
    -w              Print out results in HTML tables
//以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。
    -i              Use HEAD instead of GET
// 執行HEAD請求,而不是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)
//-C cookie-name=value 對請求附加一個Cookie:行。 其典型形式是name=value的一個參數對。此參數可以重復。
-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.
//-P proxy-auth-username:password 對一個中轉代理提供BASIC認證信任。用戶名和密碼由一個:隔開,並以base64編碼形式發送。無論服務器是否需要(即, 是否發送了401認證需求代碼),此字符串都會被發送。
-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
-h              Display usage information (this message)
//-attributes 設置 屬性的字符串. 缺陷程序中有各種靜態聲明的固定長度的緩沖區。另外,對命令行參數、服務器的響應頭和其他外部輸入的解析也很簡單,這可能會有不良后果。它沒有完整地實現 HTTP/1.x; 僅接受某些'預想'的響應格式。 strstr(3)的頻繁使用可能會帶來性能問題,即, 你可能是在測試ab而不是服務器的性能。
參數很多,一般我們用 -c 和 -n 參數就可以了. 例如:
./ab -c 1000 -n 1000 
http://127.0.0.1/index.php
這個表示同時處理1000個請求並運行1000次index.php文件.
#/usr/local/xiaobai/apache2054/bin/ab -c 1000 -n 1000 
http://127.0.0.1/index.html.zh-cn.gb2312

This is ApacheBench, Version 2.0.41-dev  apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, 
http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, 
http://www.apache.org/
Benchmarking 127.0.0.1 (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
Finished 1000 requests
Server Software:        Apache/2.0.54
//平台apache 版本2.0.54
Server Hostname:        127.0.0.1
//服務器主機名
Server Port:            80
//服務器端口
Document Path:          /index.html.zh-cn.gb2312
//測試的頁面文檔
Document Length:        1018 bytes
//文檔大小
Concurrency Level:      1000 
//並發數
Time taken for tests:   8.188731 seconds
//整個測試持續的時間
Complete requests:      1000
//完成的請求數量
Failed requests:        0
//失敗的請求數量
Write errors:           0
Total transferred:      1361581 bytes
//整個場景中的網絡傳輸量
HTML transferred:       1055666 bytes
//整個場景中的HTML內容傳輸量
Requests per second:    122.12 [#/sec] (mean)
//大家最關心的指標之一,相當於 LR 中的 每秒事務數 ,后面括號中的 mean 表示這是一個平均值
Time per request:       8188.731 [ms] (mean)
//大家最關心的指標之二,相當於 LR 中的 平均事務響應時間 ,后面括號中的 mean 表示這是一個平均值
Time per request:       8.189 [ms] (mean, across all concurrent requests)
//每個請求實際運行時間的平均值
Transfer rate:          162.30 [Kbytes/sec] received
//平均每秒網絡上的流量,可以幫助排除是否存在網絡流量過大導致響應時間延長的問題
Connection Times (ms)
min mean[+/-sd] median   max
Connect:        4 646 1078.7     89    3291
Processing:   165 992 493.1    938    4712
Waiting:      118 934 480.6    882    4554
Total:        813 1638 1338.9   1093    7785
//網絡上消耗的時間的分解,各項數據的具體算法還不是很清楚
Percentage of the requests served within a certain time (ms)
50%   1093
66%   1247
75%   1373
80%   1493
90%   4061
95%   4398
98%   5608
99%   7368
100%   7785 (longest request)
//整個場景中所有請求的響應情況。在場景中每個請求都有一個響應時間,其中50%的用戶響應時間小於1093 毫秒,
60% 的用戶響應時間小於1247 毫秒,最大的響應時間小於7785 毫秒 由於對於並發請求,cpu實際上並不是同時處理的,而是按照每個請求獲得的時間片逐個輪轉處理的,
所以基本上第一個Time per request時間約等於第二個Time per request時間乘以並發請求數

 

 

 

Apache壓力測試工具ab使用詳解

      Apache附帶的壓力測試工具ab,非常容易使用,並且完全可以摸你各種條件對Web服務器發起測試請求。ab可以直接在Web服務器本地發起測試請求,這對於需要了解服務器的處理性能至關重要,因為它不包括數據的網絡傳輸時間以及用戶PC本地的計算時間。       下面我們開始壓力測試(環境說明:win7,需要在命令行cmd中切換至ab.exe所在目錄),執行以下命令:

ab -n1000 -c10 http://localhost/index.php/

      執行結果如下:

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 localhost (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/2.2.21
Server Hostname:        localhost
Server Port:            80

Document Path:          /index.php/
Document Length:        0 bytes

Concurrency Level:      10
Time taken for tests:   1.047 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      187000 bytes
HTML transferred:       0 bytes
Requests per second:    955.06 [#/sec] (mean)
Time per request:       10.471 [ms] (mean)
Time per request:       1.047 [ms] (mean, across all concurrent requests)
Transfer rate:          174.41 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       4
Processing:     2   10   4.5      9      42
Waiting:        2   10   4.5      9      41
Total:          2   10   4.5      9      42

Percentage of the requests served within a certain time (ms)
  50%      9
  66%     11
  75%     13
  80%     15
  90%     17
  95%     18
  98%     20
  99%     21
 100%     42 (longest request)

1.執行ab時3個參數的意思分別表示:

-n1000 :總請求數為1000 -c10 :並發用戶數為10 http://localhost/index.php/ :表示這些請求的目標url

2.ab測試結果的各項說明:

Server Software:表示被測試的Web服務器軟件名稱 Server Hostname:表示請求的URL中的主機名稱,這里是localhost Server Port:表示被測試的Web服務器軟件的監聽端口 Document Path:表示請求的URL中的根絕對路徑 Document Length:表示HTTP響應數據的正文長度 Concurrency Level:表示並發用戶數,這是我們設置的參數 Time taken for tests:表示所有這些請求被處理完成所要花費的總時間 Complete requests:表示總請求數,這也是我們設置的參數 Failed requests:表示失敗的總請求數,這里的失敗指請求在連接服務器、發送數據、接收數據等環節發生異常,以及無響應超時等情況 Total transferred:表示所有請求的響應數據長度總和,包括HTTP響應的頭信息和正文數據的長度 HTML transferred:表示所有請求的響應數據中正文數據的總和 Requests per second:表示服務器吞吐率(這是我們應該重點關注的) Time per request:表示用戶平均請求的等待時間 Time per request (mean, across all concurrent requests):表示服務器平均請求處理時間 Transfer rate:表示這些請求在單位時間內從服務器獲取的數據長度 Percentage of the requests served within a certain time (ms):這部分數據描述每個請求處理時間的分布情況


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM