Author: Rethink
Date: 2018/12/07
ApacheBench Version 2.3
Apache Bench(簡稱爲ab)是 Apache 提供用於對Apache http server進行基準測試的工具。但是由於其安裝和使用簡單,所以也可以用於對HTTP接口的壓力測試和性能測試。ab是一個命令行工具,使用ab命令可以模擬多線程併發請求,並且對負載機的要求很低,既不會佔用很高CPU,也不會佔用很多內存,但卻會給目標服務器造成巨大的負載,其原理類似DDOS/CC攻擊。
ab可以提供需要的基本性能指標;但是缺點就是沒有圖形化結果,不能監控。
Tips:在帶寬不足的情況下,最好是本機進行測試,建議使用內網的另一臺或者多臺服務器通過內網進行測試,這樣得出的數據,準確度會高很多。遠程對web服務器進行壓力測試,往往效果不理想(因爲網絡延時過大或帶寬不足)
安裝
Apache本身會自帶ab,如果沒有安裝Apache,以下方法可以用來便捷的安裝ab工具:
-
Ubuntu
apt-cache install apache2-util
-
CentOS
yum install httpd-tools
- MacOS
系統自帶apache,查看版本信息:
$ apachectl -v
Server version: Apache/2.4.33 (Unix)
Server built: Apr 3 2018 17:54:07
$ ab -V
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
-
Windows
Windows下可以在官網下載最新版本 ,然後按照引導安裝即可。
使用以上方法安裝完成後,在已添加環境變量的情況下,可以直接使用ab -V
檢查是否安裝成功。
>ab -V
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/</pre>
使用
Usage: ab [options] http://hostname[:port]/path
Options
ab -h
Options are:
-V 顯示版本號並退出
-n requests 總請求數,默認值爲1
-c concurrency 併發請求數,默認值爲1,其值不能超過總請求數。建議是可以被總請求數整除,如 -n 100 –c 10模擬10個併發,每個併發10個請求。
-t timelimit 測試所用的時間上限(單位爲秒),它可以使對服務器的測試限制在一個固定的總時間以內,其內部隱含的默認值等於 -n 50000
-s timeout 每次響應的最大等待時間(單位爲秒),默認值爲30s.注意s是小寫的
-p postfile 用於存放發起post請求時的數據的文件地址,不要忘記設置請求頭。文件內容的格式要視請求頭決定。
-T content-type POST請求所使用的Content-type頭信息,如 -T “application/x-www-form-urlencoded” ,默認值爲“text/plain”
-H attribute 添加頭部Header,需要注意custome-header的格式爲 key:value,eg. User-Agent: ApacheBench/2.3', 這也是ab的默認值User-Agent
-C attribute 對請求附加Cookie,格式爲 key=value ,注意和-H區分!
-m method 設置請求方法
-v verbosity 顯示調試信息,verbosity爲數字,可以看做是輸出信息的級別。3或更大值可以顯示響應代碼(404, 200等), 2或更大值可以顯示警告和其他信息。
-X proxy:port 設置代理地址以及端口信息
-w 以HTML表的格式輸出結果。
-r 拋出異常繼續執行測試任務
-i 使用HEAD請求代替GET
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-u putfile File containing data to PUT. Remember also to set -T
-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
-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.
-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
-h Display usage information (this message)</pre>
Usage
> ab -n 10 -c 10 http://cnblogs.com/
# Get請求,要注意“&”是ab的保留運算符,使用時要用雙引號引起來
> ab -n 10 -c 10 http://httpbin.org/get?name=rethink"&"age=3
# Post請求,post_data文件中的內容要和所指定的Content-Type對應(默認text/plain)
> ab -n 10 -c 10 -p E:\post_data1.txt -T application/x-www-form-urlencoded http://httpbin.org/post
> ab -n 10 -c 10 -p E:\post_data2.txt -T application/json http://httpbin.org/post
# post_data1中的數據爲:name=rethink&age=3&method=post
# post_data1中的數據爲:{"name":"Rethink","method":"post","age":3}
# 使用`-w`參數將結果以HTML的格式輸出,然後將結果寫入到本地文件中:
> ab -n 2 -c 2 -v 2 -w http://httpbin.org/get?name=rethink"&"age=3 > E:\report.html
注意事項:
-
result.html中會打印每次請求的請求頭信息,請求總數較大時,重定向結果輸出時可以不指定
-v
參數; -
使用
-H Content-Type:application/json
不能代替-T "application/json"
, 使用前者服務器可能會返回400 bad requests; -
如果提示
ab: invalid URL
,可能是URL最右邊缺少/,例如 http://www.example.com 需要改爲*http://www.example.com/* ; -
不支持發送https請求;
-
postfile注意使用正確的編碼格式,否則請求參數服務器端可能無法識別;
-
調試請求時,對接口返回的中文字符的支持不友好,會顯示亂碼;
結果分析
從上面可以看到ab支持參數很多,但一般來說只有-c
和-n
參數是必要的,例如:
>PS > ab -n 10 -c 10 http://httpbin.org/get?name=rethink"&"age=3
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking cnblogs.com (be patient).....done
Server Software: gunicorn/19.9.0 服務器類型
Server Hostname: httpbin.org 域名
Server Port: 80 web端口
Document Path: /get?name=rethink&age=3 測試的頁面路徑
Document Length: 280 bytes 測試的頁面大小
Concurrency Level: 10 測試的併發數
Time taken for tests: 2.629 seconds 整個測試活動持續的時間
Complete requests: 10 完成的請求數量(只包含狀態碼==200的請求)
Failed requests: 0 失敗的請求數量(包含狀態碼!=200的請求和超時的請求)
Non-2xx responses: 10 狀態碼不等於2xx的響應數
Total transferred: 5210 bytes 整個過程中的網絡傳輸量
HTML transferred: 2800 bytes 整個過程中的HTML內容傳輸量
Requests per second: 3.80 [/sec] (mean) 每秒的響應請求數(QPS),數值等於 請求總數(-n)/Time taken for tests,後面括號中的mean表示這是一個平均值
Time per request: 2629.486 [ms] (mean) 用戶平均請求等待時間=concurrency * timetaken * 1000 / done
Time per request: 12.893 [ms] (mean, across all concurrent requests) 每個連接請求實際運行時間的平均值
Transfer rate: 25.15 [Kbytes/sec] received 傳輸速率
Connection Times (ms)
min mean[+/-sd] median max
Connect: 210 216 6.1 216 229
Processing: 229 1022 658.7 1081 2181
Waiting: 229 827 558.2 866 1731
Total: 445 1238 659.4 1291 2399
Percentage of the requests served within a certain time (ms)
50% 1291 整體響應時間的分佈比
66% 1507
75% 1731
80% 1949
90% 2399 表示90%的請求在1291ms內得到響應
95% 2399
98% 2399
99% 2399
100% 2399 (longest request)
AB 和 JMETER 比較
下面分別用ab和jmeter壓測一個Get請求,然後通過對比效果,來簡單分析二者的異同。
- ab
在ab命令行中設置10個併發,持續60s,結果如下:
PS> ab -c 10 -t 60 http://httpbin.org/get?name=Rethink"&"date=20181207"&"method=get
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking httpbin.org (be patient)
Finished 1386 requests
Server Software: gunicorn/19.9.0
Server Hostname: httpbin.org
Server Port: 80
Document Path: /get?name=Rethink&date=20181207&method=get
Document Length: 330 bytes
Concurrency Level: 10
Time taken for tests: 60.050 seconds
Complete requests: 1332
Failed requests: 0
Total transferred: 760572 bytes
HTML transferred: 439560 bytes
Requests per second: 22.18 [#/sec] (mean)
Time per request: 450.828 [ms] (mean)
Time per request: 45.083 [ms] (mean, across all concurrent requests)
Transfer rate: 12.37 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 189 219 139.5 200 2245
Processing: 196 227 89.5 212 1573
Waiting: 196 226 89.1 212 1573
Total: 387 446 174.6 414 2551
Percentage of the requests served within a certain time (ms)
50% 414
66% 422
75% 427
80% 431
90% 458
95% 553
98% 1045
99% 1476
100% 2551 (longest request)</pre>
- Jmeter
在JMETER GUI界面中編輯腳本信息,線程數設置10,R-U時間設置1s,循環次數設置Forever。如下圖:
然後使用 JMETER 命令行模式運行腳本,這是因爲使用非GUI模式可以節省負載機資源,測試結果更加準切。
$ jmeter -n -t ~/JmeterWar.jmx -l JmeterWar.csv
Creating summariser <summary>
Created the tree successfully using /Users/rethink/JmeterWar.jmx
Starting the test @ Sun Dec 09 16:56:02 CST 2018 (1544345762429)
Waiting for possible Shutdown/StopTestNow/Heapdump message on port 4448
summary + 1 in 00:00:01 = 1.6/s Avg: 504 Min: 504 Max: 504 Err: 0 (0.00%) Active: 7 Started: 7 Finished: 0
summary + 621 in 00:00:27 = 23.2/s Avg: 427 Min: 389 Max: 1465 Err: 0 (0.00%) Active: 10 Started: 10 Finished: 0
summary = 622 in 00:00:27 = 22.7/s Avg: 427 Min: 389 Max: 1465 Err: 0 (0.00%)
summary + 711 in 00:00:30 = 23.7/s Avg: 421 Min: 389 Max: 1470 Err: 0 (0.00%) Active: 10 Started: 10 Finished: 0
summary = 1333 in 00:00:57 = 23.2/s Avg: 424 Min: 389 Max: 1470 Err: 0 (0.00%)
^Z
[4]+ Stopped jmeter -n -t ~/JmeterWar.jmx -l JmeterWar.csv
在GUI界面中打開JmeterWar.csv文件,如下:
- 二者對比
ab | Jmeter | |
---|---|---|
發送的請求總數 | 1386 | 1350 |
Average | 414ms | 424ms |
Min | 387ms | 389ms |
Median | 414ms | 413ms |
Max | 2551ms | 1470ms |
90% Line | 458ms | 434ms |
95% Line | 553ms | 452ms |
99% Line | 1476ms | 859ms |
Error % | 0.00 | 0.00 |
QPS | 22.18/sec | 23.4/sec |
通過以上表格數據進行分析,可以發現二者的測試效果竟然是如此的接近,但實際上述數據並不是太準確,因爲Jmeter實際不支持精確的測試時間的限制,所以我是到達60s後手動強制殺死進程,這就導致有些請求可能被漏掉,但通過比較聚合數據,基本上誤差是在可以接受的範圍內。
個人總結
1、jmeter是一次完整的請求和返回, 而AB只是發出去請求,並不對返回做處理,只是請求發送成功或者失敗。 所以從準確性來說,Jmeter更準確,而AB速度更快,可以用最少的機器資源產生更多的訪問請求;
2、Jmeter本身支持斷言、可變參數和CSV數據集的輸入,能設定更加靈活多變的的測試場景,而AB則不支持(暫時沒想到);
3、Jmeter可以提供更加詳細的統計結果數據,比如接口錯誤信息、單線程的請求時間等,而AB則不支持;
4、Jmeter不支持精確時間的壓測,比如壓測10分鐘,但是AB支持;
5、Jmeter支持分佈式的壓測集羣,且支持函數,AB不支持;
6、軟件自身耗費資源:Jmeter由於比較重,且統計了很多結果數據,比AB耗時耗費資源多,B屬於超輕量級,在開發測試過程中十分適合做單接口壓測。