1. ab簡介
ab全稱Apache Bench,是apache附帶的一個小工具,它可以同時模擬多個並發請求,測試apache等Web服務器的最大負載壓力。
本文通過一個簡單的示例,介紹了使用ab進行web頁面性能測試、查看結果方法及運行參數詳解。
運行環境為Windows 10系統。
2. ab下載
Apache官網:
http://httpd.apache.org/download.cgi
2.1 Windows系統
對於windows系統,可訪問頁面
https://www.apachelounge.com/download/下載相應版本的zip包,我們下載的版本是:httpd-2.4.29-Win64-VC15.zip

也可以下載我提取出來的ab.exe,解壓即可使用:
https://files.cnblogs.com/files/lovesoo/ab.zip
2.2 linux系統
使用如下命令安裝Apache
yum install httpd
3. ab運行
使用cmd/shell進入到Apache24\bin目錄下,運行ab即可

4. 使用ab進行性能測試
最常用兩種運行命令如下:
ab -n 10 -c 10 http://www.cnblogs.com # -n 總共10次請求,-c 模擬10個並發用戶,即10個並發請求博客園首頁,共請求10次 ab -t 10 -c 10 http://www.cnblogs.com # -t 是測試執行時間,-c 模擬10個並發用戶,即10個並發請求博客園首頁,持續10秒
5. 測試結果
6. 結果解析
Server Software: Server Hostname: www.cnblogs.com Server Port: 80 Document Path: / Document Length: 147 bytes #HTTP響應數據的正文長度 Concurrency Level: 10 #並發數 Time taken for tests: 10.183 seconds #測試執行時間 Complete requests: 169 #完成請求數 Failed requests: 0 #失敗請求數 Non-2xx responses: 169 Total transferred: 65403 bytes #網絡總傳輸量 HTML transferred: 24843 bytes #HTML內容傳輸量 Requests per second: 16.60 [#/sec] (mean) #每秒請求數 Time per request: 602.545 [ms] (mean) #用戶平均請求等待時間,計算公式:測試執行總時間/(總請求數/並發用戶數) Time per request: 60.254 [ms] (mean, across all concurrent requests) #服務器平均請求等待時間,計算公式:測試執行總時間/總請求數 Transfer rate: 6.27 [Kbytes/sec] received #平均傳輸速率 Connection Times (ms) #響應時間小、中、大值 min mean[+/-sd] median max Connect: 2 40 325.5 3 3006 Processing: 41 382 928.1 63 3072 Waiting: 41 376 922.1 58 3063 Total: 43 422 971.5 68 3075 Percentage of the requests served within a certain time (ms) #一定時間內請求完成的百分比,如50%用戶完成的響應時間在68ms內, 66%用戶完成的響應時間在73ms內,100%用戶在3075ms內完成 50% 68 66% 73 75% 81 80% 85 90% 3060 95% 3066 98% 3069 99% 3075 100% 3075 (longest request)
7. ab運行參數詳解
官方幫助文檔如下:
Usage: ab [options] [http://]hostname[:port]/path Options are: -n requests Number of requests to perform -c concurrency Number of multiple requests to make at a time -t timelimit Seconds to max. to spend on benchmarking This implies -n 50000 -s timeout Seconds to max. wait for each response Default is 30 seconds -b windowsize Size of TCP send/receive buffer, in bytes -B address Address to bind to when making outgoing connections -p postfile File containing data to POST. Remember also to set -T -u putfile File containing data to PUT. Remember also to set -T -T content-type Content-type header to use for POST/PUT data, eg. 'application/x-www-form-urlencoded' Default is 'text/plain' -v verbosity How much troubleshooting info to print -w Print out results in HTML tables -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 -C attribute Add cookie, eg. 'Apache=1234'. (repeatable) -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. -q Do not show progress when doing more than 150 requests -l Accept variable document length (use this for dynamic pages) -g filename Output collected data to gnuplot format file. -e filename Output CSV file with percentages served -r Don't exit on socket receive errors. -m method Method name -h Display usage information (this message)
中文翻譯如下:
-n 即requests,用於指定壓力測試/總請求數。 -c 即concurrency,用於指定壓力測試的並發數。 -t 即timelimit,測試執行最大秒數,它可以讓測試限制在一個固定的總時間以內,默認值為50000。 -s 即timeout,請求最大等待時長,默認30s -b 即windowsize,TCP發送/接收的緩沖大小(單位:字節)。 -p 即postfile,發送POST請求時需要上傳的文件,文件格式如"p1=1&p2=2"。使用方法是 -p 123.txt 。 (配合-T) -u 即putfile,發送PUT請求時需要上傳的文件。(配合-T) -T 即content-type,用於設置Content-Type請求頭信息,如 -T "application/x-www-form-urlencoded”,默認值為text/plain。(配合-p) -v 即verbosity,設置顯示信息的詳細程度 – 4或更大值會顯示頭信息, 3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。 -w 以HTML表格形式打印結果。 -i 使用HEAD請求代替GET請求。 -x 插入字符串作為table標簽的屬性。 -y 插入字符串作為tr標簽的屬性。 -z 插入字符串作為td標簽的屬性。 -C 添加cookie信息,例如:"Apache=1234"。此參數可以重復,用逗號分割。提示:可以借助session實現原理傳遞 JSESSIONID參數, 實現保持會話的功能,如-C "c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8"。 -H 添加任意的請求頭,例如:"Accept-Encoding: gzip",請求頭將會添加在現有的多個請求頭之后(可以重復該參數選項以添加多個)。 -A 添加一個基本的網絡認證信息,用戶名和密碼之間用英文冒號隔開。 -P 添加一個基本的代理認證信息,用戶名和密碼之間用英文冒號隔開。如-P proxy-auth-username:password -X 指定使用的代理服務器和端口號,例如:"126.10.10.3:88"。 -V 顯示版本號並退出。 -k 使用HTTP的KeepAlive特性。 -d 不顯示百分比。 -S 不顯示預估和警告信息。 -q 超過150個請求后不顯示進度 -l 接受可變文檔長度(用於動態頁面) -g filename 輸出結果信息到gnuplot格式的文件中。 -e filename 輸出結果信息到CSV格式的文件中。 -r 指定接收到錯誤信息時不退出程序。 -m method 方法名 -h 幫助