1. 程式人生 > >Apache壓力(並發)測試工具ab的使用教程收集

Apache壓力(並發)測試工具ab的使用教程收集

ros 格式 單位 需要 程序 () ont pat 此外

說明:用ab的好處,在處理多並發的情況下不用自己寫線程模擬。

官網:

http://httpd.apache.org/(Apache服務器)

http://httpd.apache.org/docs/2.0/programs/(Apache工具文檔大全)

http://httpd.apache.org/docs/2.0/programs/ab.html(文檔教程)

http://httpd.apache.org/docs/current/programs/ab.html(文檔教程)

教程:

要對Apache及其上的web程序進行壓力測試其實非常簡單,我們也不用再額外下載安裝什麽測試工具,因為Apache HTTP Server已經給我們準備了一個壓力測試工具——ab。

ab,即Apache Benchmark,只要我們安裝了Apache,就能夠在Apache的安裝目錄中找到它。它的居住地址是Apache安裝目錄/bin/ab。現在,我們就來看看如何使用ab.exe來進行壓力測試。

在使用之前我們先來學習一下ab的用法。Apache安裝目錄/bin/。然後鍵入幫助命令ab -help(或者ab /?ab -h),我們就可以看到如下的用法介紹界面。

技術分享圖片

ab的用法介紹

通過上面的用法介紹可以得知,ab的使用方法就是輸入如下命令:

ab [可選的參數選項] 需要進行壓力測試的url

此外,我們再根據上面的用法介紹界面來詳細了解每個參數選項的作用。

-n:即requests,用於指定壓力測試總共的執行次數。
-c:即concurrency,用於指定壓力測試的並發數。
-t:即timelimit,等待響應的最大時間
(單位:秒)。 -b:即windowsize,TCP發送/接收的緩沖大小(單位:字節)。 -p:即postfile,發送POST請求時需要上傳的文件,此外還必須設置-T參數。 -u:即putfile,發送PUT請求時需要上傳的文件,此外還必須設置-T參數。 -T:即content-type,用於設置Content-Type請求頭信息,例如:application/x-www-form-urlencoded,默認值為text/plain。 -v:即verbosity,指定打印幫助信息的冗余級別。 -w:以HTML表格形式打印結果。 -i:使用HEAD請求代替GET請求。 -x:插入字符串作為table標簽的屬性。 -y:插入字符串作為tr標簽的屬性。 -z:插入字符串作為td標簽的屬性。 -C:添加cookie信息,例如:
"Apache=1234"(可以重復該參數選項以添加多個)。 -H:添加任意的請求頭,例如:"Accept-Encoding: gzip",請求頭將會添加在現有的多個請求頭之後(可以重復該參數選項以添加多個)。 -A:添加一個基本的網絡認證信息,用戶名和密碼之間用英文冒號隔開。 -P:添加一個基本的代理認證信息,用戶名和密碼之間用英文冒號隔開。 -X:指定使用的代理服務器和端口號,例如:"126.10.10.3:88"。 -V:打印版本號並退出。 -k:使用HTTP的KeepAlive特性。 -k:使用HTTP的KeepAlive特性。 -d:不顯示百分比。 -S:不顯示預估和警告信息。 -g:輸出結果信息到gnuplot格式的文件中。 -e:輸出結果信息到CSV格式的文件中。 -r:指定接收到錯誤信息時不退出程序。 -h:顯示用法信息,其實就是ab -help。

雖然ab可以配置的參數選項比較多,但是,一般情況下我們只需要使用形如ab -n 數字 -c 數字 url路徑的命令即可。譬如,我們對位於本地Apache服務器上、URL為localhost/index.php的頁面進行壓力測試。測試總次數為1000,並發數為100(相當於100個用戶同時訪問,他們總共訪問1000次)。我們輸入命令ab -n 1000 -c 100 localhost/index.php,打印結果如下:

ab -n 1000 -c 100 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.25 (服務器軟件名稱及版本信息)
Server Hostname:        localhost (服務器主機名)
Server Port:            80 (服務器端口)
Document Path:          /index.php (供測試的URL路徑)
Document Length:        10 bytes (供測試的URL返回的文檔大小)
Concurrency Level:      100 (並發數)
Time taken for tests:   0.247 seconds (壓力測試消耗的總時間)
Complete requests:      1000 (壓力測試的總次數)
Failed requests:        0 (失敗的請求數)
Write errors:           0 (網絡連接寫入錯誤數)
Total transferred:      198000 bytes (傳輸的總數據量)
HTML transferred:       10000 bytes (HTML文檔的總數據量)
Requests per second:    4048.34 [#/sec] (mean) (平均每秒的請求數)
Time per request:       24.701 [ms] (mean) (所有並發用戶(這裏是100)都請求一次的平均時間)
Time per request:       0.247 [ms] (mean, across all concurrent requests) (單個用戶請求一次的平均時間)
Transfer rate:          782.78 [Kbytes/sec] received (傳輸速率,單位:KB/s)
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       1
Processing:     6   23   4.2     24      30
Waiting:        5   20   5.3     21      29
Total:          6   23   4.2     24      30

Percentage of the requests served within a certain time (ms)
  50%     24
  66%     25
  75%     26
  80%     26
  90%     27
  95%     27
  98%     28
  99%     29
 100%     30 (longest request)

在上面的測試中,我們設置的壓力測試總次數以及並發數並沒有讓服務器感覺到什麽「壓力」,現在我們再來看一個「壓力山大」的執行命令:ab -n 100000 -c 1000 localhost/index.php,這個時候apache就直接罷工——拒絕訪問了:

ab -n 100000 -c 1000 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)  
Test aborted after 10 failures
apr_socket_connect(): 由於目標計算機積極拒絕,無法連接。   (730061)

註意:在上面的壓力測試中,Apache使用的是默認配置,並沒有經過任何優化措施處理。實際上,Apache在經過配置優化後,只要服務器硬件夠用,Apache服務器是能夠撐起1000的並發量的。

參考:

http://www.365mini.com/page/apache-benchmark.htm(以上內容轉自此篇文章)

http://www.php.cn/php-weizijiaocheng-371644.html

http://www.jianshu.com/p/1f3acc45ca89

http://www.jb51.net/article/59469.htm

http://blog.51cto.com/510512/740219

http://blog.csdn.net/blueheart20/article/details/52170790

http://xuezaijiongtu.blog.163.com/blog/static/197576271201251101527325/

http://www.jianshu.com/p/43d04d8baaf7

http://www.jb51.net/article/56644.htm

Apache壓力(並發)測試工具ab的使用教程收集