壓力測試工具Apache Bench的使用 原

一、Apache Bench簡介

ApacheBench 是 Apache 服務器自帶的一個web壓力測試工具,簡稱ab。ab又是一個命令行工具,對發起負載的本機要求很低,根據ab命令可以創建很多的併發訪問線程,模擬多個訪問者同時對某一URL地址進行訪問,因此可以用來測試目標服務器的負載壓力。總的來說ab工具小巧簡單,上手學習較快,可以提供需要的基本性能指標,但是沒有圖形化結果,不能監控。

 

二、Apache Bench安裝

首先需要安裝Apache服務器,下載地址:https://www.apachelounge.com/download/

建議下載最新版本的,因爲舊版本的ab不支持-r參數。

三、Apache Bench使用

瞭解參數

參數說明:
格式: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    //測試所進行的最大秒數,默認沒有時間限制。
-r Don't exit on socket receive errors.     // 拋出異常繼續執行測試任務 
-p postfile File containing data to POST  //包含了需要POST的數據的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt

-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 顯示版本號並退出。
-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″ 。
-w Print out results in HTML tables  //以HTML表的格式輸出結果。默認時,它是白色背景的兩列寬度的一張表。
-i Use HEAD instead of 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
-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.
-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)

 

參數很多,一般我們用 -c 和 -n 參數就可以了。例如:

# ab -c 10 -n 10 http://www.xiami.com/

結果參數分析:
 

通過上圖,測試結果也一目瞭然,apache測試出的吞吐率爲:Requests per second: 204.89[#/sec](mean)。

除此之外還有其他一些信息,需要說明下,如下:

Server Software表示被測試的Web服務器軟件名稱。

Server Hostname表示請求的URL主機名。

Server Port表示被測試的Web服務器軟件的監聽端口。

Document Path表示請求的URL中的根絕對路徑,通過該文件的後綴名,我們一般可以瞭解該請求的類型。

Document Length表示HTTP響應數據的正文長度。

Concurrency Level表示併發用戶數,這是我們設置的參數之一。

Time taken for tests表示所有這些請求被處理完成所花費的總時間。

Complete requests表示總請求數量,這是我們設置的參數之一。

Failed requests表示失敗的請求數量,這裏的失敗是指請求在連接服務器、發送數據等環節發生異常,以及無響應後超時的情況。如果接收到的HTTP響應數據的頭信息中含有2XX以外的狀態碼,則會在測試結果中顯示另一個名爲“Non-2xx responses”的統計項,用於統計這部分請求數,這些請求並不算在失敗的請求中。

Total transferred表示所有請求的響應數據長度總和,包括每個HTTP響應數據的頭信息和正文數據的長度。注意這裏不包括HTTP請求數據的長度,僅僅爲web服務器流向用戶PC的應用層數據總長度。

HTML transferred表示所有請求的響應數據中正文數據的總和,也就是減去了Total transferred中HTTP響應數據中的頭信息的長度。

Requests per second 每秒請求數(平均),吞吐率,計算公式:Complete requests/Time taken for tests

Time per request 每次併發請求時間(所有併發),用戶平均請求等待時間,計算公式:Time token for tests/(Complete requests/Concurrency Level)。

Time per requet(across all concurrent request) 每一請求時間(併發平均)  服務器平均請求等待時間,計算公式:Time taken for tests/Complete requests,正好是吞吐率的倒數。也可以這麼統計:Time per request/Concurrency Level。

Transfer rate表示這些請求在單位時間內從服務器獲取的數據長度,計算公式:Total trnasferred/ Time taken for tests,這個統計很好的說明服務器的處理能力達到極限時,其出口寬帶的需求量。

Percentage of requests served within a certain time(ms)這部分數據用於描述每個請求處理時間的分佈情況,比如以上測試,80%的請求處理時間都不超過52ms,這個處理時間是指前面的Time per request,即對於單個用戶而言,平均每個請求的處理時間。

調用示例:

參考:
https://www.cnblogs.com/Ryana/p/6279232.html
https://blog.csdn.net/zzycgfans/article/details/6100755
https://blog.csdn.net/u014756827/article/details/52160689
https://www.cnblogs.com/xiaoyaowuming/p/5622660.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章