【測試環(huán)境的安裝】
【ab】
ab就是Apache Benchmark的縮寫,顧名思義它是Apache組織開發(fā)的一款web壓力測試工具,優(yōu)點是使用方便,統(tǒng)計功能強大。
ab作為一款非常流行的壓測工具,使用細節(jié)這里就不多介紹了,直接給出個人的使用體會:
首先是安裝,Ubuntu和CentOS目前都提供自動安裝命令 (至少ubuntu 14, centos 6可以)
ubuntu:sudo apt-get install apache2-utils
centos:yum install httpd-tools
安裝好之后就可以開始進行測試。
ab一般常用參數(shù)就是 -n, -t ,和 -c。
-c(concurrency)表示用多少并發(fā)來進行測試;
-t表示測試持續(xù)多長時間;
-n表示要發(fā)送多少次測試請求。
一般-t或者-n選一個用。
對于模擬GET請求進行測試,ab非常簡單,就是:ab -n 100 -c 10 'http://testurl.com/xxxx?para1=aaa¶2=bbb'
對于模擬POST請求進行測試,則稍微復(fù)雜些,需要把將要post的數(shù)據(jù)(一般是json格式)放在文件里。比如一個post接口需要如下方式訪問
curl -H 'Content-Type:application/json' -X POST? -d '{"actionType":"collect","appId":1,"contentId":"1770730744","contentType":"musictrack","did":"866479025346031","endType":"mobile","recommendId":"104169490_1_0_1434453099#1770730744#musictrack#USER_TO_SONG_TO_SONGS#gsql_similarity_content2content","tabId":0,"uid":"104169490"}' http://localhost:8083/query/leui/v0/post/user/behavior/content
需要吧-d 后面的json數(shù)據(jù)放在一個文件里,比如建立一個文件post_data.txt,放入:
{"actionType":"collect","appId":1,"contentId":"1770730744","contentType":"musictrack","did":"866479025346031","endType":"mobile","recommendId":"104169490_1_0_1434453099#1770730744#musictrack#USER_TO_SONG_TO_SONGS#gsql_similarity_content2content","tabId":0,"uid":"104169490"}
然后用-p參數(shù)解析并發(fā)送這個json數(shù)據(jù):ab -n 100 -c 10 -p post_data.txt -T 'application/json' http://localhost:8083/query/leui/v0/post/user/behavior/content
【Jmeter】
jmeter是一個非常強大和用戶友好的GUI工具,http訪問的參數(shù)設(shè)置基本應(yīng)有盡有,軟件的help里的用戶手冊圖例比較豐富,對于初學者來說很好上手
對于http測試的可配置參數(shù)有(1)http請求配置:測試目標host,端口,url路徑,http請求參數(shù),post的數(shù)據(jù),http header(2)測試全局策略:測試發(fā)送的并發(fā)量,測試循環(huán)次數(shù)(jmeter沒有測試時間的設(shè)定,只有不斷發(fā)送測試請求反復(fù)重復(fù)多少次loop,或者無限重復(fù))。對于(1)里每個可配置的參數(shù)都可以用變量${varaible_name}來代替,而araiable可以來自CSV格式的外部輸入,GUI有“測試計劃(test plan)右鍵點擊-----添加-----配置元件-----CSV Data Set Config 里可以設(shè)置CSV 文件來源和CSV里每列數(shù)據(jù)對應(yīng)的變量名稱。
上面說的功能還有其他強大功能設(shè)置,在help文檔和很多網(wǎng)上資料里都有詳細介紹,比如CSV輸入設(shè)置(博客),這里不再贅述。
一般來說,主要用到以下設(shè)置界面
新建測試計劃(Test Plan)
Test Plan ---- 新建 Thread Group(線程組)
線程組 ---- 配置元件----HTTP Header Manager
線程組-----配置元件---CSV Data Set Config
線程組---sampler(采樣器) -----HTTP 請求, 這里面有兩個tab,一個是“parameters“ tab, 配置http請求參數(shù),一個是“post data” tab,可配置post的數(shù)據(jù),一般是一個json串,json串里的字段都可以用${xxx}這樣的變量來表示。
這幾個界面配置好即可,而且jmeter的配置保存時會生成.jmx文件,這個不只是用來保存修改配置用,而且還有一個更大的用處,就是在非GUI(命令行,shell)下運行。
因為我們的壓測環(huán)境往往是linux系統(tǒng),而且為了最大程度的發(fā)揮壓測工具的性能,不論系統(tǒng)還是壓測工具都最好運行在非GUI模式
linuxshell下,因為jmeter是java開發(fā)的,無需安裝,只需要把jmeter解壓后,在解壓路徑下,運行
{jmeter_install_dir)/}bin/jmeter -n -t $target -l xxxx.jtl
-n 就是靜默模;
-t 后面就跟.jmx配置文件的路徑;
-l 跟一個輸出文件用于記錄每次請求的時間,可以用jmeter GUI打開生成最終統(tǒng)計聚合報告。
在命令行模式下運行jmeter,還可以從命令行(shell)輸入?yún)?shù),只要在jmeter命令后的參數(shù)列表里加上? -JXXXX = value , XXXX被jmeter識別為一個外部輸入變量,其值是value, 在jmerter配置里用${__P(XXXX)}來使用這個外部輸入變量,注意"__P"開頭是連著的兩個下劃線。
【測試構(gòu)建和效果對比】
下面我們用一個實際的例子來進行ab和jmeter壓力測試的效果對比, 首先是GET請求。
【ab】
先用10個并發(fā)壓測100秒。
]# ab -t 100 -c 10 'http://localhost:8083/xxxx?uid=1233435&did=123456789&appId=1'
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxx.xxx.xxx.xxx (be patient)
Finished 733 requests
Server Software:? ? ? ? CppCMS-Embedded/1.0.4
Server Hostname:? ? ? xxx.xxx.xxx.xxx
Server Port:? ? ? ? ? ? 8083
Document Path:? ? ? ? / xxx?uid=79057533&did=123456789&appId=1
Document Length:? ? ? ? 4601 bytes
Concurrency Level:? ? ? 10
Time taken for tests:? 100.137 seconds
Complete requests:? ? ? 733
Failed requests:? ? ? ? 732
(Connect: 0, Receive: 0, Length: 732, Exceptions: 0)
Write errors:? ? ? ? ? 0
Total transferred:? ? ? 3672653 bytes
HTML transferred:? ? ? 3572232 bytes
Requests per second:? ? 7.32 [#/sec] (mean)
Time per request:? ? ? 1366.124 [ms] (mean)
Time per request:? ? ? 136.612 [ms] (mean, across all concurrent requests)
Transfer rate:? ? ? ? ? 35.82 [Kbytes/sec] received
Connection Times (ms)
min? mean[+/-sd] median? max
Connect:? ? ? ? 1? ? 2? 2.4? ? ? 2? ? ? 40
Processing:? 342 1352 636.3? 1183? ? 6046
Waiting:? ? ? 342 1351 636.2? 1183? ? 6046
Total:? ? ? ? 345 1354 636.8? 1186? ? 6049
Percentage of the requests served within a certain time (ms)
50%? 1185
66%? 1333
75%? 1460
80%? 1564
90%? 1835
95%? 2357
98%? 3248
99%? 5205
100%? 6049 (longest request)
【jmeter】
再用jemeter以同樣的配置(Thread數(shù) 為10, ramp時間為1秒,超時閾值為3000ms)
運行結(jié)果如下:
Creating summariser
Created the tree successfully using music_api_uid.jmx
Starting the test @ Thu Nov 19 11:19:43 CST 2015 (1447903183454)
Waiting for possible shutdown message on port 4445
summary +? ? 90 in? ? 16s =? ? 5.7/s Avg:? 1677 Min:? 959 Max:? 3757 Err:? ? 0 (0.00%) Active: 10 Started: 10 Finished: 0
summary +? ? 202 in? 31.1s =? ? 6.5/s Avg:? 1477 Min:? 912 Max:? 2727 Err:? ? 0 (0.00%) Active: 10 Started: 10 Finished: 0
summary =? ? 292 in? ? 46s =? ? 6.4/s Avg:? 1539 Min:? 912 Max:? 3757 Err:? ? 0 (0.00%)
summary +? ? 164 in? ? 31s =? ? 5.3/s Avg:? 1830 Min:? 972 Max:? 5009 Err:? ? 5 (3.05%) Active: 10 Started: 10 Finished: 0
summary =? ? 456 in? ? 76s =? ? 6.0/s Avg:? 1643 Min:? 912 Max:? 5009 Err:? ? 5 (1.10
最終通過GUI打開請求詳細記錄,生成聚合報告的結(jié)果為
samples: 576
average: 1713
median: 1496
90% Line: 2353
min: 912
max: 5009
throught: 5.8/sec
kb/sec: 27.8
error%: 2.08%
GET API壓測結(jié)果對比
abjmeter
發(fā)送總請求數(shù)733576
平均請求時間(ms)13661713
請求時間中位數(shù)(50%<)(ms)11851496
請求時間90%< (ms)18352353
error?2.08%
QPS7.326
對比兩次測試,ab完成了733次,而jmeter完成了576次而ab完成了733次,其實這個數(shù)據(jù)不算準確,因為jmeter不支持精確的測試時間的限制,所以我是按時強制終止了jemeter,所以有些請求可能就被漏掉了。不過我后來測試了同樣總請求數(shù)時(ab用-n設(shè)置,jmeter則用 thread*loops來設(shè)置),jmeter也比ab慢戈15%,可能和jmeter自己要做很多統(tǒng)計有關(guān)系。
因為測試時間不是嚴格相等,所以主要看平均:時間分布,ab普遍低一些。雙方測試環(huán)境和測試參數(shù)都一致,不知道是不是jmeter在計算返回時間上算法不一樣。平均時間,jmeter統(tǒng)計口徑也是偏高
我留了個心眼,查詢了web接口后臺日志,確保了兩次測試的傳參是一樣的,那么結(jié)果的差別只能理解為兩個軟件在統(tǒng)計口徑(比如返回時件的測量標準上),以及http訪問方式上有差別( 比如同樣是設(shè)置10個并發(fā),一般是理解為開10個線程去不斷的請求接口,但線程的調(diào)度策略不一樣,對服務(wù)器的壓力不一樣,返回的性能也不一樣)。
GET測試對比完 , 再來對比一下POST API測試的效果:
【ab】:
#ab -t 100 -c 10 -p post_data.txt -T 'application/json' http://localhost:8083/xxxxx
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxx.xxx.xxx.xxx (be patient)
Completed 5000 requests
Completed 10000 requests
Finished 12937 requests
Server Software:? ? ? ? CppCMS-Embedded/1.0.4
Server Hostname:? ? ? ? xxx.xxx.xxx.xxx
Server Port:? ? ? ? ? ? 8083
Document Path:? ? ? ? ? /xxxxx
Document Length:? ? ? ? 92 bytes
Concurrency Level:? ? ? 10
Time taken for tests:? 100.001 seconds
Complete requests:? ? ? 12937
Failed requests:? ? ? ? 0
Write errors:? ? ? ? ? 0
Total transferred:? ? ? 2962573 bytes
Total POSTed:? ? ? ? ? 4828858
HTML transferred:? ? ? 1190204 bytes
Requests per second:? ? 129.37 [#/sec] (mean)
Time per request:? ? ? 77.299 [ms] (mean)
Time per request:? ? ? 7.730 [ms] (mean, across all concurrent requests)
Transfer rate:? ? ? ? ? 28.93 [Kbytes/sec] received
47.16 kb/s sent
76.09 kb/s total
Connection Times (ms)
min? mean[+/-sd] median? max
Connect:? ? ? ? 1? ? 2? 8.9? ? ? 1? ? 1001
Processing:? ? 31? 76? 78.5? ? 69? ? 2452
Waiting:? ? ? 31? 75? 77.7? ? 69? ? 2452
Total:? ? ? ? 33? 77? 79.0? ? 71? ? 2454
Percentage of the requests served within a certain time (ms)
50%? ? 71
66%? ? 80
75%? ? 88
80%? ? 91
90%? ? 101
95%? ? 113
98%? ? 124
99%? ? 140
100%? 2454 (longest request)
【jmeter】
而同樣參數(shù)配置的jmeter的結(jié)果為:
#../apache-jmeter-2.11/bin/jmeter -n -t post.jmx -JCSV=post_paras.txt -JIP=xxx.xxx.xxx.xxx -JPORT=8083 -JTHREAD=10 -JRAMP=1 -l "post_test.log"
Creating summariser
Created the tree successfully using post_to_recommend_user_action_server.jmx
Starting the test @ Tue Nov 17 20:49:37 CST 2015 (1447764577991)
Waiting for possible shutdown message on port 4445
summary +? 3978 in? 21.1s =? 188.5/s Avg:? ? 51 Min:? ? 32 Max:? 1049 Err:? ? 0 (0.00%) Active: 10 Started: 10 Finished: 0
summary +? 3796 in? 30.1s =? 126.2/s Avg:? ? 78 Min:? ? 34 Max:? 1596 Err:? ? 0 (0.00%) Active: 10 Started: 10 Finished: 0
summary =? 7774 in? 51.1s =? 152.1/s Avg:? ? 64 Min:? ? 32 Max:? 1596 Err:? ? 0 (0.00%)
summary +? 3273 in? 30.1s =? 108.8/s Avg:? ? 91 Min:? ? 37 Max:? 3091 Err:? ? 1 (0.03%) Active: 10 Started: 10 Finished: 0
summary =? 11047 in? 81.1s =? 136.2/s Avg:? ? 72 Min:? ? 32 Max:? 3091 Err:? ? 1 (0.01%)
對post_test.log進行聚合報告分析
samples: 11899?
average: 58
median: 52
90% line: 76
min: 27
max: 3091
error: 0.01%
throughout: 7.6/sec
kb/sec: 1.9
post api 壓測結(jié)果對比
abjmeter
完成請求數(shù)1293711899
平均返回時間(ms)7758
最大返回時間(ms)?3091
最小返回時間(ms)?27
請求時間中位數(shù)(50%<)7152
90%的請求反水時間低于(ms)10176
錯誤率(基本是超時)00.01%
QPS129136
【使用對比總結(jié)】
個人體會是:
統(tǒng)計效果上,ab占優(yōu):ab的優(yōu)點是統(tǒng)計的結(jié)果可讀性更強更能幫助人分析,至于一些參數(shù)的差異,主要看源代碼實現(xiàn)了,但誤差在可接受的范圍內(nèi),作為壓力測試,我們需要的是服務(wù)器面對壓力的一個大致的能力和隨著壓力增大后服務(wù)器性能的變化趨勢,所以ab和jmeter兩個數(shù)字誰更精確是沒太多意義的,反而是他倆數(shù)字差別不大就已經(jīng)起到了互相印證的作用。
壓測方案指定上,jmeter占優(yōu):主要是jmeter支持可變參數(shù)和CSV數(shù)據(jù)集的輸入,能設(shè)定更復(fù)雜的測試樣例,適用范圍更廣。
ab不需要寫配置文件,只需要幾個命令行參數(shù)就能執(zhí)行壓測,適用于接口簡單業(yè)務(wù)邏輯簡單的http服務(wù)的測試。
使用JMeter對WebService進行壓力測試?http://www.linuxidc.com/Linux/2014-10/108088.htm
Windows 下 JMeter 安裝手記?http://www.linuxidc.com/Linux/2013-10/91749.htm
使用JMeter進行基本壓力測試?http://www.linuxidc.com/Linux/2013-04/83086.htm
JMeter中斷言的使用方法?http://www.linuxidc.com/Linux/2013-10/91770.htm
JMeter 監(jiān)控插件JMeterPlugins&PerfMon安裝?http://www.linuxidc.com/Linux/2016-05/131684.htm