Web 性能壓測工具 ApacheBench(ab)使用總結

導語

對於 web 服務,不管是上新,還是平時產品運營,節假日活動等,在這之前都需要評估現網壓力承受能力,提前進行擴容,並做到防患於未然。所以對網站性能壓力測試是必不可少的,這樣才能充分了解自己部署的 web 服務 QPS。只有讓服務器處在高壓情況下才能真正體現出各種設置所暴露的問題。Apache 中有個自帶的,名為 ab 的程序,可以對 Apache 或其它類型的服務器進行網站訪問壓力測試。

1. ApacheBench 命令原理

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

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

Web 性能壓測工具 ApacheBench(ab)使用總結

2. ApacheBench 參數說明

1) Synopsis

<code>ab [ -A auth-username:password ] [ -b windowsize ] [ -B local-address ] [ -c concurrency ] [ -C cookie-name=value ] [ -d ] [ -e csv-file ] [ -f protocol ] [ -g gnuplot-file ] [ -h ] [ -H custom-header ] [ -i ] [ -k ] [ -l ] [ -m HTTP-method ] [ -n requests ] [ -p POST-file ] [ -P proxy-auth-username:password ] [ -q ] [ -r ] [ -s timeout ] [ -S ] [ -t timelimit ] [ -T content-type ] [ -u PUT-file ] [ -v verbosity] [ -V ] [ -w ] [ -x-attributes ] [ -X proxy[:port] ] [ -y-attributes ] [ -z-attributes ] [ -Z ciphersuite ] [http[s]://]hostname[:port]/path/<code>

2) Options

<code>-n requests Number of requests to perform/<code>

//在測試會話中所執行的請求個數(本次測試總共要訪問頁面的次數)。默認時,僅執行一個請求。

<code>-c concurrency Number of multiple requests to make/<code>

//一次產生的請求個數(併發數)。默認是一次一個。

<code>-t timelimit Seconds to max. wait for responses/<code>

//測試所進行的最大秒數。其內部隱含值是-n 50000。它可以使對服務器的測試限制在一個固定的總時間以內。默認時,沒有時間限制。

<code>-p postfile File containing data to POST/<code>

//包含了需要 POST 的數據的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt 。 (配合-T)

<code>-T content-type Content-type header for POSTing/<code>

//POST 數據所使用的 Content-type 頭信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p)

<code>-v verbosity How much troubleshooting info to print/<code>

//設置顯示信息的詳細程度 – 4 或更大值會顯示頭信息, 3 或更大值可以顯示響應代碼(404, 200 等), 2 或更大值可以顯示警告和其他信息。 -V 顯示版本號並退出。

<code>-w Print out results in HTML tables/<code>

//以 HTML 表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。

<code>-i Use HEAD instead of GET/<code>

// 執行 HEAD 請求,而不是 GET。

<code>-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)/<code>

//-C cookie-name=value 對請求附加一個 Cookie:行。 其典型形式是 name=value 的一個參數對。此參數可以重複,用逗號分割。

提示:可以藉助 session 實現原理傳遞 JSESSIONID 參數, 實現保持會話的功能,如

<code>-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 attributesare a colon separated username and password.-P attribute Add Basic Proxy Authentication, the attributesare a colon separated username and password./<code>

//-P proxy-auth-username:password 對一箇中轉代理提供 BASIC 認證信任。用戶名和密碼由一個:隔開,並以 base64 編碼形式發送。無論服務器是否需要(即, 是否發送了 401 認證需求代碼),此字符串都會被髮送。

<code>-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)/<code>

//-attributes 設置屬性的字符串. 缺陷程序中有各種靜態聲明的固定長度的緩衝區。另外,對命令行參數、服務器的響應頭和其他外部輸入的解析也很簡單,這可能會有不良後果。它沒有完整地實現 HTTP/1.x; 僅接受某些’預想’的響應格式。 strstr(3) 的頻繁使用可能會帶來性能問題,即你可能是在測試 ab 而不是服務器的性能。

參數很多,常用的就兩個,例如:

<code># ab -c 1000 -n 100 http://10.50.120.121/test/blank/<code>

3. ApacheBench 用法詳解

在 Linux 系統,一般安裝好 Apache 後可以直接執行;

<code>#ab -k -n 100000 -c 500 -H "Accept-Encoding: gzip, deflate, sdch" -H "User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Mobile/14C92 QQ/7.0.0.3002_GrayThree_309592_04-27_23:04 V1_IPH_SQ_7.0.0_5_HDBM_T Pixel/750 Core/UIWebView NetType/WIFI QBWebViewType/1" -C "uin=o0369491785;skey=@MC7dyF5lu;" http://10.51.170.39/sport/home?_wv=2163715/<code>

如果是 win 系統下,打開 cmd 命令行窗口,cd 到 apache 安裝目錄的 bin 目錄下

上述命令的含義是:

在長連接狀態下,模擬移動端,請求頭帶 gzip 壓縮,並且帶登錄態,一次 500 個併發,總請求量為 100000,向指定機器 IP 和頁面的 URL 發送請求
URL 也可以是域名,本地配置 HOST 指向要壓測的機器 IP 也可以~

稍等片刻,執行結果及分析如下:

<code>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 10.51.170.39 (be patient)Completed 10000 requestsCompleted 20000 requestsCompleted 30000 requestsCompleted 40000 requestsCompleted 50000 requestsCompleted 60000 requestsCompleted 70000 requestsCompleted 80000 requestsCompleted 90000 requestsCompleted 100000 requestsFinished 100000 requestsServer Software:                         //測試服務端類型Server Hostname:        10.51.170.39     //測試服務器 HOSTServer Port:            80               //測試服務端端口號Document Path:          /sport/home?_wv=2163715   //The request URI parsed from the command line string.Document Length:        62346 bytes               //This is the size in bytes of the first successfully returned document. If the document length changes during testing, the response is considered an error.Concurrency Level:      500                       //The number of concurrent clients used during the testTime taken for tests:   158.969 seconds           //This is the time taken from the moment the first socket connection is created to the moment the last response is receivedComplete requests:      100000                    //The number of successful responses receivedFailed requests:        0                         //The number of requests that were considered a failure. Write errors:           0                         //he number of errors that failed during write (broken pipe).Keep-Alive requests:    100000                    //The number of connections that resulted in Keep-Alive requestsTotal transferred:      6266233588 bytes          //The total number of bytes received from the server. This number is essentially the number of bytes sent over the wire.HTML transferred:       6236217388 bytes          //The total number of document bytes received from the server. This number excludes bytes received in HTTP headersRequests per second:    629.05 [#/sec] (mean)     //This is the number of requests per second. This value is the result of dividing the number of requests by the total time takenTime per request:       794.847 [ms] (mean)       //The average time spent per request. The first value is calculated with the formula concurrency * timetaken * 1000 / done while the second value is calculated with the formula timetaken * 1000 / doneTime per request:       1.590 [ms] (mean, across all concurrent requests)Transfer rate:          38494.01 [Kbytes/sec] received            //The rate of transfer as calculated by the formula totalread / 1024 / timetakenConnection Times (ms)              min  mean[+/-sd] median   maxConnect:        0    0   2.1      0      34Processing:   296  793 151.5    809    2962Waiting:      232  690 153.2    713    2724Total:        296  793 152.1    809    2992//整個場景中所有請求的響應情況。在場景中每個請求都有一個響應時間,其中 50%的用戶響應時間小於 809 毫秒,66%的用戶響應時間小於 854 毫秒,最大的響應時間小於 2992 毫秒。對於併發請求,cpu 實際上並不是同時處理的,而是按照每個請求獲得的時間片逐個輪轉處理的,所以基本上第一個 Time per request 時間約等於第二個 Time per request 時間乘以併發請求數。Percentage of the requests served within a certain time (ms)  50%    809  66%    854  75%    881  80%    899  90%    947  95%    998  98%   1075  99%   1146 100%   2992 (longest request)/<code> 

4. 總結

一般我們在對 web 服務器進行壓力測試時,建議使用內網的另一臺或者多臺服務器通過內網進行測試,這樣得出的數據,準確度會高很多。如果只有單獨的一臺服務器,可以直接本地測試,比遠程測試效果要準確。

參考文檔:

httpd.apache.org/docs/2.4/pr…


分享到:


相關文章: