Web必備性能壓力測試工具WebBench與ApcheBench(ab)詳解

  在運維工做中,壓力測試是一項很重要的工做。好比在一個網站上線以前,能承受多大訪問量、在大訪問量狀況下性能怎樣,這些數據指標好壞將會直接影響用戶體驗。可是,在壓力測試中存在一個共性,那就是壓力測試的結果與實際負載結果不會徹底相同,就算壓力測試工做作的再好,也不能保證100%和線上性能指標相同。面對這些問題,咱們只能儘可能去千方百計去模擬。因此,壓力測試很是有必要,有了這些數據,咱們就能對本身作維護的平臺作到心中有數。網站性能壓力測試是性能調優過程當中必不可少的一環,只有讓服務器處在高壓狀況下才能真正體現出各類設置所暴露的問題。php


下面就介紹經常使用的兩款最實用的web性能壓力測試工具html

目錄:nginx

  1.Web必備性能壓力測試工具WebBench詳解web

  2.Web必備性能壓力測試工具ApcheBench詳解算法




一:Web必備性能壓力測試工具WebBench詳解apache

  Webbench是知名的網站壓力測試工具,它是由Lionbridge公司(http://www.lionbridge.com)開發。tomcat

  Webbench能測試處在相同硬件上,不一樣服務的性能以及不一樣硬件上同一個服務的運行情況。webbench的標準測試能夠向咱們展現服務器的兩項內容:每秒鐘相應請求數和每秒鐘傳輸數據量。webbench不但能具備便準靜態頁面的測試能力,還能對動態頁面(ASP,PHP,JAVA,CGI)進 行測試的能力。還有就是他支持對含有SSL的安全網站例如電子商務網站進行靜態或動態的性能測試。Webbench最多能夠模擬3萬個併發鏈接去測試網站的負載能力。安全

  官方主頁:http://home.tiscali.cz/~cz210552/webbench.htmlbash


一、WebBench安裝:
服務器

wget http://home.tiscali.cz/~cz210552/distfiles/webbench-1.5.tar.gz
mkdir /usr/local/man/man1 -p  
tar zxvf webbench-1.5.tar.gz
cd webbench-1.5makemake install


二、WebBench用法:

webbench -c 併發數 -t 運行測試時間 URL
用法示例以下:
webbench -c 1000 -t 60 http://192.168.1.8/phpinfo.php


Apache測試實例結果:
#當併發300時,顯示爲有0個連接failed了,說明沒有超負荷

[root@aliyun webbench-1.5]# webbench -c 300 -t 60 http://192.168.80.157/phpinfo.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.


Benchmarking: GET http://192.168.80.157/phpinfo.php
300 clients, running 60 sec.

Speed=24525 pages/min, 20794612 bytes/sec.
Requests: 24525 susceed, 0 failed.

#當併發1000時,已經顯示有87個鏈接failed了,說明超負荷了。

[root@aliyun webbench-1.5]# webbench -c 1000 -t 60 http://192.168.80.157/phpinfo.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.


Benchmarking: GET http://192.168.80.157/phpinfo.php
1000 clients, running 60 sec.

Speed=24920 pages/min, 21037312 bytes/sec.
Requests: 24833 susceed, 87 failed.

併發1000運行60秒後產生的TCP鏈接數達到27000多個:

[root@aliyun ~]# netstat -nat|grep -i "80"|wc -l
27391


使用WebBench工具總結:
  一、壓力測試工做必定要放到產品上線以前,而不是上線之後。
  二、測試時併發應當由小逐漸加大,好比並發100時觀察一下網站負載是多少、打開頁面是否流暢,併發200時又是多少、網站打開緩慢時併發是多少、網站打不開時併發又是多少,最後作好數據統計。對結果進行更細緻的分析
  三、更詳細的進行某個頁面測試,對着訪問比重比較大的文件。如電商網站能夠着重測試購物車、推廣頁面等,由於這些頁面佔整個網站訪問量比重較大。

  



二:Web必備性能壓力測試工具ApcheBench詳解

ApacheBench命令原理:

  ab命令會建立不少的併發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問。它的測試目標是基於URL的,所以,既能夠用來測試Apache的負載壓力,也能夠測試nginx、lighthttp、tomcat、IIS等其它Web服務器的壓力。

  ab命令對發出負載的計算機要求很低,既不會佔用很高CPU,也不會佔用不少內存,但卻會給目標服務器形成巨大的負載,其原理相似CC×××。本身測試使用也須注意,不然一次上太多的負載,可能形成目標服務器因資源耗完,嚴重時甚至致使死機。


ApacheBench參數說明
格式: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的數據的文件,文件格式如「p1=1&p2=2」.使用方法是 -p 111.txt 。 (配合-T)

-T content-type Content-type header for POSTing
#POST數據所使用的Content-type頭信息,如 -T 「application/x-www-form-urlencoded」 。 (配合-p)

-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. -C 「c1=1234,c2=2,c3=3」 (repeatable)
#-C cookie-name=value 對請求附加一個Cookie:行。其典型形式是name=value的一個參數對。此參數能夠重複,用逗號分割。
#提示:能夠藉助session實現原理傳遞JSESSIONID參數,實現保持會話的功能,如-C 」c1=1234,c2=2,c3=3,JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8"。
-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 5000 -n 600 http://127.0.0.1/index.php


ApacheBench用法詳解:

#在Linux系統下,通常安裝好Apache後能夠直接執行;
 ab -n 4000 -c 1000 http://192.168.1.3/index.html

#若是是Win系統下,打開cmd命令行窗口,cd到apache安裝目錄的bin目錄下;
-n後面的4000表明總共發出4000個請求;-c後面的1000表示採用1000個併發(模擬1000我的同時訪問),後面的網址表示測試的目標URL。


稍等一會獲得相似以下顯示結果(省略部分):

[root@aliyun html]# ab -n 4000 -c 1000 http://192.168.1.3/index.html
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 192.168.1.3 (be patient)
Completed 400 requests
Completed 800 requests
Completed 1200 requests
Completed 1600 requests
Completed 2000 requests
Completed 2400 requests
Completed 2800 requests
Completed 3200 requests
Completed 3600 requests
Completed 4000 requests
Finished 4000 requests


結果分析以下:



This is ApacheBench, Version 2.3
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.3 (be patient)

Completed 400 requests
Completed 800 requests
Completed 1200 requests
Completed 1600 requests
Completed 2000 requests
Completed 2400 requests
Completed 2800 requests
Completed 3200 requests
Completed 3600 requests
Completed 4000 requests
Finished 4000 requests

Server Software: Apache/2.2.15
Server Hostname: 192.168.1.3      

Server Port: 80                              #測試的頁面端口

Document Path: /index.html        #測試的頁面

Document Length: 50797 bytes   #頁面大小

Concurrency Level: 1000              #測試的併發數
Time taken for tests: 11.846 seconds    #整個測試持續的時間
Complete requests: 4000            #完成的請求數量
Failed requests: 0                        #失敗的請求數量
Write errors: 0
Total transferred: 204586997 bytes                #整個過程當中的網絡傳輸量
HTML transferred: 203479961 bytes                #整個過程當中的HTML內容傳輸量
Requests per second: 337.67 [#/sec] (mean)    #最重要的指標之一,至關於LR中的每秒事務數,後面括號中的mean表示這是一個平均值
Time per request: 2961.449 [ms] (mean)
#最重要的指標之二,至關於LR中的平均事務響應時間,後面括號中的mean表示這是一個平均值
Time per request: 2.961 [ms] (mean, across all concurrent requests)
#每一個鏈接請求實際運行時間的平均值
Transfer rate: 16866.07 [Kbytes/sec] received
#平均每秒網絡上的流量,能夠幫助排除是否存在網絡流量過大致使響應時間延長的問題
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 483 1773.5 11 9052
Processing: 2 556 1459.1 255 11763
Waiting: 1 515 1459.8 220 11756
Total: 139 1039 2296.6 275 11843
#網絡上消耗的時間的分解,各項數據的具體算法還不是特別清楚

Percentage of the requests served within a certain time (ms)
50% 275
66% 298
75% 328
80% 373
90% 3260
95% 9075
98% 9267
99% 11713
100% 11843 (longest request)
#整個場景中全部請求的響應狀況。在場景中每一個請求都有一個響應時間,其中50%的用戶響應時間小於275毫秒,66%的用戶響應時間小於298毫秒,最大的響應時間小於11843毫秒。對於併發請求,cpu實際上並非同時處理的,而是按照每一個請求得到的時間片逐個輪轉處理的,因此基本上第一個Time per request時間約等於第二個Time per request時間乘以併發請求數。


 

  使用ApcheBench(ab)總結:在遠程對web服務器進行壓力測試,每每效果不理想(由於網絡延時過大),建議使用內網的另外一臺或者多臺服務器經過內網進行測試,這樣得出的數據,準確度會高不少。若是隻有單獨的一臺服務器,能夠直接本地測試,比遠程測試效果要更準確。(可是要注意在沒上線的狀況下)

相關文章
相關標籤/搜索