1. 程式人生 > >使用ApacheBench(ab)對URL進行壓力測試(HTTP直壓)

使用ApacheBench(ab)對URL進行壓力測試(HTTP直壓)

ApacheBench(ab)安裝

  • ubuntu 安裝

    apt-get install apache2-utils // 可單獨安裝,與apache2無捆綁關係。若無法安裝,請更新源後重試。

  • 其它系統安裝,自行搜尋。

ApacheBench(ab)demo

Server Software: BWS/1.1
Server Hostname: www.baidu.com
Server Port: 80

Document Path: /
Document Length: 100255 bytes

Concurrency Level: 10 // 併發數量
Time taken for tests: 1.074 seconds // 測試總時間
Complete requests: 100 // 請求總數
Failed requests: 97
(Connect: 0, Receive: 0, Length: 97, Exceptions: 0)
Total transferred: 10113449 bytes
HTML transferred: 10015078 bytes
Requests per second: 93.10 [#/sec] (mean) // 吞吐量(URL每秒能承受的訪問次數)
Time per request: 107.409 [ms] (mean) // 使用者等待時間(等於 伺服器等待時間*併發數)
Time per request: 10.741 [ms] (mean, across all concurrent requests) // 伺服器等待時間(即伺服器完成一個請求————接受請求,處理請求,返回請求————所需要的時間)(與吞吐量互為倒數關係)(計算公式:伺服器等待時間=測試總時間/請求總數)
Transfer rate: 9195.15 [Kbytes/sec] received

Connection Times (ms)
// min:最小值;mean:平均值;median:中位值;max:最大值;
min mean[+/-sd] median max
Connect: 5 23 11.2 23 54
Processing: 35 78 49.0 70 323
Waiting: 6 25 32.8 20 250
Total: 46 102 50.9 95 329

Percentage of the requests served within a certain time (ms)
50% 95
66% 102 // 66%的客戶響應時間 <= 102ms
75% 107
80% 113
90% 143
95% 164
98% 316
99% 329
100% 329 (longest request)

ApacheBench(ab)引數說明

格式: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而不是伺服器的效能。

ApacheBench(ab)簡介

ApacheBench 是apache中自帶的一個指令列程式,專門用來執行網站伺服器的執行效能,特別是針對Apache 網站伺服器。這原本是用來檢測 Apache 網站伺服器能夠提供的效能,特別是可以看出Apache能提供每秒能送出多少網頁,常用來做網站效能壓力測試。
網站效能壓力測試是效能調優過程中必不可少的一環。只有讓伺服器處在高壓情況下才能真正體現出各種設定所暴露的問題。

ApacheBench(ab)原理

ab命令會建立很多的併發訪問執行緒,模擬多個訪問者同時對某一URL地址進行訪問。它的測試目標是基於URL的,因此,既可以用來測試Apache的負載壓力,也可以測試nginx、lighthttp、tomcat、IIS等其它Web伺服器的壓力。
ab命令對發出負載的計算機要求很低,既不會佔用很高CPU,也不會佔用很多記憶體,但卻會給目標伺服器造成巨大的負載,其原理類似CC攻擊。自己測試使用也須注意,否則一次上太多的負載,可能造成目標伺服器因資源耗完,嚴重時甚至導致宕機。