基準測試載入器:ab

來源:互聯網
上載者:User
基準測試載入器(ab)

ab是什嗎?
ab是基於命令列的工具,均可運行在windows、linux平台下。
為什麼選用這個工具?
便於使用,輸入少量命令選項,即可得出結果,同時易於安裝。

安裝ab

win:如果本地有apache的開發環境,那麼ab預設在apache目錄下的bin目錄下,開啟bin目錄可以看到
linux:直接yum install httpd-tools,安裝即可

簡單上手

直接敲入下面代碼(注意切換ab所在的目錄)
-c 10代表並發數是10
-n 10總共進行100次訪問
後面接要訪問的網址,切記不可缺少http://

  1. D:\phpStudy\Apache\bin>ab -c 10 -n 1000 http://example.com/phpinfo.php
複製代碼

待ab自己跑一下,得出下面結果

  1. #ab工具的描述,注意其中的版本號碼
  2. This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. #測試結果,共測試1000次,每100次顯示訪問進度,訪問的網站是example.com
  6. Benchmarking example.com (be patient)
  7. Completed 100 requests
  8. Completed 200 requests
  9. Completed 300 requests
  10. Completed 400 requests
  11. Completed 500 requests
  12. Completed 600 requests
  13. Completed 700 requests
  14. Completed 800 requests
  15. Completed 900 requests
  16. Completed 1000 requests
  17. Finished 1000 requests
  18. #伺服器的資訊
  19. Server Software: nginx/1.6.2 #伺服器web軟體
  20. Server Hostname: example.com #主機地址
  21. Server Port: 80 #訪問連接埠
  22. #文檔資訊
  23. Document Path: /phpinfo.php #訪問的文檔
  24. Document Length: 94804 bytes #返回結果大小位元組數(包含html,js,css,圖片及響應中的所有內容位元組數總和)
  25. #連結資訊
  26. Concurrency Level: 10 #並發數,-c 10 設定了10個並發
  27. Time taken for tests: 7.498 seconds #總耗時,單位秒
  28. Complete requests: 1000 #總共請求中已完成的請求總數的和
  29. Failed requests: 219 #失敗的請求數總和
  30. (Connect: 0, Receive: 0, Length: 219, Exceptions: 0)
  31. Non-2xx responses: 9 #未收到2xx系列成功的請求總數
  32. Total transferred: 94135779 bytes #整個請求總數中響應總數距的總大小位元組數,包含頭資訊
  33. HTML transferred: 93952076 bytes #整個請求總數中本文內容的總大小位元組
  34. Requests per second: 133.36 [#/sec] (mean) #每秒支援的並發數,這裡是每秒支援133.36個並發
  35. Time per request: 74.984 [ms] (mean) #完成一個請求的總耗時
  36. Time per request: 7.498 [ms] (mean, across all concurrent requests) #完成所有並發請求中一個請求的總耗時
  37. Transfer rate: 12259.83 [Kbytes/sec] received #每秒收到的位元組總數(KB)
  38. #測試的時間結果資料,留意Total一項,10個並發的情況下,完成一個請求,最小耗時(min)9毫秒,最大耗時(max)146毫秒
  39. Connection Times (ms)
  40. min mean[+/-sd] median max
  41. Connect: 0 0 0.7 0 20
  42. Processing: 9 74 22.2 68 146
  43. Waiting: 6 73 22.1 67 143
  44. Total: 9 74 22.2 68 146
  45. #完成請求百分比,例如第一項,50% 68,代表一半的請求在68毫秒內完成,95% 129,代表1000的請求裡,百分之九十八的請求在129毫秒裡完成
  46. Percentage of the requests served within a certain time (ms)
  47. 50% 68
  48. 66% 78
  49. 75% 86
  50. 80% 92
  51. 90% 109
  52. 95% 122
  53. 98% 129
  54. 99% 133
  55. 100% 146 (longest request)
複製代碼ab選項

輸入下面命令得到ab協助文檔,該協助文檔,分別說明了用法和命令選項的含義

  1. D:\phpStudy\Apache\bin>ab -h
複製代碼

ab協助文檔的輸出

  1. Usage: ab [options] [http://]hostname[:port]/path
  2. Options are:
  3. -n requests Number of requests to perform
  4. -c concurrency Number of multiple requests to make at a time
  5. -t timelimit Seconds to max. to spend on benchmarking
  6. This implies -n 50000
  7. -s timeout Seconds to max. wait for each response
  8. Default is 30 seconds
  9. -b windowsize Size of TCP send/receive buffer, in bytes
  10. -B address Address to bind to when making outgoing connections
  11. -p postfile File containing data to POST. Remember also to set -T
  12. -u putfile File containing data to PUT. Remember also to set -T
  13. -T content-type Content-type header to use for POST/PUT data, eg.
  14. 'application/x-www-form-urlencoded'
  15. Default is 'text/plain'
  16. -v verbosity How much troubleshooting info to print
  17. -w Print out results in HTML tables
  18. -i Use HEAD instead of GET
  19. -x attributes String to insert as table attributes
  20. -y attributes String to insert as tr attributes
  21. -z attributes String to insert as td or th attributes
  22. -C attribute Add cookie, eg. 'Apache=1234'. (repeatable)
  23. -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
  24. Inserted after all normal header lines. (repeatable)
  25. -A attribute Add Basic WWW Authentication, the attributes
  26. are a colon separated username and password.
  27. -P attribute Add Basic Proxy Authentication, the attributes
  28. are a colon separated username and password.
  29. -X proxy:port Proxyserver and port number to use
  30. -V Print version number and exit
  31. -k Use HTTP KeepAlive feature
  32. -d Do not show percentiles served table.
  33. -S Do not show confidence estimators and warnings.
  34. -q Do not show progress when doing more than 150 requests
  35. -l Accept variable document length (use this for dynamic pages)
  36. -g filename Output collected data to gnuplot format file.
  37. -e filename Output CSV file with percentages served
  38. -r Don't exit on socket receive errors.
  39. -m method Method name
  40. -h Display usage information (this message)
複製代碼

英文看不懂,附上中文解釋,網上抄的,我沒試過

  1. -n 即requests,用於指定壓力測試總共的執行次數。
  2. -c 即concurrency,用於指定壓力測試的並發數。
  3. -t 即timelimit,等待響應的最大時間(單位:秒)。
  4. -b 即windowsize,TCP發送/接收的緩衝大小(單位:位元組)。
  5. -p 即postfile,發送POST請求時需要上傳的檔案,此外還必須設定-T參數。
  6. -u 即putfile,發送PUT請求時需要上傳的檔案,此外還必須設定-T參數。
  7. -T 即content-type,用於設定Content-Type要求標頭資訊,例如:application/x-www-form-urlencoded,預設值為text/plain。
  8. -v 即verbosity,指定列印協助資訊的冗餘層級。
  9. -w 以HTML表格形式列印結果。
  10. -i 使用HEAD請求代替GET請求。
  11. -x 插入字串作為table標籤的屬性。
  12. -y 插入字串作為tr標籤的屬性。
  13. -z 插入字串作為td標籤的屬性。
  14. -C 添加cookie資訊,例如:"Apache=1234"(可以重複該參數選項以添加多個)。
  15. -H 添加任意的要求標頭,例如:"Accept-Encoding: gzip",要求標頭將會添加在現有的多個要求標頭之後(可以重複該參數選項以添加多個)。
  16. -A 添加一個基本的網路認證資訊,使用者名稱和密碼之間用英文冒號隔開。
  17. -P 添加一個基本的代理認證資訊,使用者名稱和密碼之間用英文冒號隔開。
  18. -X 指定使用的Proxy 伺服器和連接埠號碼,例如:"126.10.10.3:88"。
  19. -V 列印版本號碼並退出。
  20. -k 使用HTTP的KeepAlive特性。
  21. -k 使用HTTP的KeepAlive特性。
  22. -d 不顯示百分比。
  23. -S 不顯示預估和警告資訊。
  24. -g 輸出結果資訊到gnuplot格式的檔案中。
  25. -e 輸出結果資訊到CSV格式的檔案中。
  26. -r 指定接收到錯誤資訊時不退出程式。
  27. -h 顯示用法資訊,其實就是ab -help。
複製代碼

一般用-n,-c,-t就好了
-n請求總數
-c並發數
-t指定秒數內的並發數

-t的用法說一下,下面的意思是在20秒內並發數10個訪問

  1. D:\phpStudy\Apache\bin> ab -c 10 -t 20 http://example.com/phpinfo.php
  2. This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
  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 example.com (be patient)
  6. Finished 3011 requests
  7. Server Software: nginx/1.6.2
  8. Server Hostname: example.com
  9. Server Port: 80
  10. Document Path: /phpinfo.php
  11. Document Length: 94804 bytes
  12. Concurrency Level: 10
  13. Time taken for tests: 20.006 seconds
  14. Complete requests: 3011
  15. Failed requests: 495
  16. (Connect: 0, Receive: 0, Length: 495, Exceptions: 0)
  17. Non-2xx responses: 54
  18. Total transferred: 280896464 bytes
  19. HTML transferred: 280344222 bytes
  20. Requests per second: 150.50 [#/sec] (mean)
  21. Time per request: 66.444 [ms] (mean)
  22. Time per request: 6.644 [ms] (mean, across all concurrent requests)
  23. Transfer rate: 13711.44 [Kbytes/sec] received
  24. Connection Times (ms)
  25. min mean[+/-sd] median max
  26. Connect: 0 0 0.5 0 10
  27. Processing: 6 66 17.6 63 203
  28. Waiting: 5 64 17.3 61 202
  29. Total: 6 66 17.6 63 203
  30. Percentage of the requests served within a certain time (ms)
  31. 50% 63
  32. 66% 69
  33. 75% 72
  34. 80% 75
  35. 90% 82
  36. 95% 92
  37. 98% 113
  38. 99% 125
  39. 100% 203 (longest request)
複製代碼

特別說明:如果訪問http://www.example.com/,沒有指定特定檔案,記得要加最後的那個/,一定要加!!!否則可能會出現訪問不到的情況!!!

可能會影響基準測試的情況

地理位置和網路問題

回應檔大小

代碼的複雜度

瀏覽器解析

Web伺服器的配置

地理位置和網路問題

如果Web伺服器存放在境外,那麼我們從本地訪問境外的網站,可想而知,經過的路由器結點,伺服器結點,再通過太平洋海底的光纜,而後再訪問到Web伺服器,Web伺服器處理後,再繼續海底光纜,各個伺服器、路由結點的跳轉返回,這樣的話肯定會影響到測試結果(我的建議是,直接在伺服器上進行ab測試)

回應檔大小

假如發送一個3MB的頁面,伺服器網卡會把3MB的資料拆分成單個很小的資料包,傳輸過程中,只有一個資料包損壞或丟失,則會重新發送所有的資料包,所以發送的包越小越好,同時,越小的資料轉送,越快傳輸到使用者的機器上。

代碼的複雜度

代碼的複雜度即是對商務邏輯複雜的處理,還有對檔案調用、資料庫訪問、遠端API介面的調用等等都會影響處理時間

瀏覽器解析

每個瀏覽器都有自己對js、css、html的處理方法,想想IE8以下和chrome的區別即可知道

Web伺服器設定

一般Web伺服器安裝後,簡單設定,即可實現Web服務(開箱即用),但是這樣的設定沒有發揮Web伺服器的最大效能,需要資深的工程師對伺服器進行相關配置,使之發揮最大效能,這裡簡單實用Keep-Alive說明。
Keep-Alive的作用就是,Web伺服器開啟特定數量的串連,讓這些串連處於開啟狀態,使之能快速處理傳入的請求,這樣就不會為每一個傳入的請求開啟一個串連然後處理請求,減少伺服器的處理請求的進程數,從而增加並發數。
在ab中使用-k進行測試,例如下面這句

  1. #-k,允許伺服器儲存5個並發串連處於開啟並允許的狀態,讓其他請求快速處理,減少建立新請求的處理時間
  2. ab -c 5 -t 100 -k http://www.example.com/
複製代碼
測試載入器
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.