一、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/

結果參數分析:
Document Path:測試頁面 Document Length: 頁面大小 Concurrency Level: 測試的並發數 Time taken for tests:整個測試持續的時間 Complete requests:完成的請求數量 Failed requests: 失敗的請求數量 Write errors: 0 Total transferred: 整個過程中的網絡傳輸量 HTML transferred: 整個過程中的HTML內容傳輸量 Requests per second: 最重要的指標之一,相當於LR中的每秒事務數,后面括號中的mean表示這是一個平均值 Time per request: 最重要的指標之二,相當於LR中的平均事務響應時間,后面括號中的mean表示這是一個平均值 Time per request: 每個連接請求實際運行時間的平均值 Transfer rate: 平均每秒網絡上的流量,可以幫助排除是否存在網絡流量過大導致響應時間延長的問題
參考:http://www.ha97.com/4617.html
