[搬运工系列]-JMeter(二十四)搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)
一、環(huán)境準(zhǔn)備:
1、JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html
2、Jmeter:http://jmeter.apache.org/download_jmeter.cgi
3、Ant:http://ant.apache.org/bindownload.cgi
4、Jenkins:http://jenkins-ci.org/
具體可參見(jiàn)我另一篇博客:http://www.cnblogs.com/puresoul/p/4808416.html
??
二、Jemter腳本準(zhǔn)備:
1、腳本目錄:D:\B_TOOLS\apache-jmeter-2.13\demo
2、腳本內(nèi)容:都是簡(jiǎn)單的訪問(wèn)百度或google首頁(yè)
baidu.jmx google.jmx
?
?
三、ANT的build.xml代碼準(zhǔn)備:
build3.xml?
1 <?xml version="1.0" encoding="UTF-8"?>2 3 <project name="ant-jmeter-test" default="run" basedir=".">4 <tstamp>5 <format property="time" pattern="yyyyMMddhhmm" />6 </tstamp>7 8 <property environment="env"/>9 <property name="ReportName" value="TestReport" /> 10 <!-- 需要改成自己本地的 Jmeter 目錄--> 11 <property name="jmeter.home" value="D:\B_TOOLS\apache-jmeter-2.13" /> 12 <!-- jmeter生成jtl、html格式的結(jié)果報(bào)告的路徑--> 13 <property name="jmeter.result.dir" value="${env.WORKSPACE}/results/${env.BUILD_ID}" /> 14 <!-- 生成的報(bào)告的前綴--> 15 <property name="jmeter.result.jtlName" value="${jmeter.result.dir}/${ReportName}.jtl" /> 16 <property name="jmeter.result.htmlName" value="${jmeter.result.dir}/${ReportName}.html" /> 17 18 <target name="run"> 19 <echo message="start..."/> 20 <antcall target="clean" /> 21 <antcall target="test" /> 22 <antcall target="report" /> 23 </target> 24 25 <target name="clean"> 26 <mkdir dir="${env.WORKSPACE}/results/${env.BUILD_ID}" /> 27 </target> 28 29 <target name="test"> 30 <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" /> 31 <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}"> 32 <!-- 聲明要運(yùn)行的腳本"*.jmx"指包含此目錄下的所有jmeter腳本--> 33 <testplans dir="D:\B_TOOLS\apache-jmeter-2.13\demo" includes="*.jmx" /> 34 35 <property name="jmeter.save.saveservice.output_format" value="xml"/> 36 </jmeter> 37 </target> 38 39 <target name="report"> 40 <xslt in="${jmeter.result.jtlName}" 41 out="${jmeter.result.htmlName}" 42 style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl" /> 43 <!-- 因?yàn)樯厦嫔蓤?bào)告的時(shí)候,不會(huì)將相關(guān)的圖片也一起拷貝至目標(biāo)目錄,所以,需要手動(dòng)拷貝 --> 44 <copy todir="${jmeter.result.dir}"> 45 <fileset dir="${jmeter.home}/extras"> 46 <include name="collapse.png" /> 47 <include name="expand.png" /> 48 </fileset> 49 </copy> 50 </target> 51 </project>??
四、配置Jenkins Job并運(yùn)行:
1、job配置如下:
2、job運(yùn)行結(jié)果:?
1 Started by user anonymous2 Building in workspace D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace3 [demo] $ cmd.exe /C '"ant.bat -file build3.xml run && exit %%ERRORLEVEL%%"'4 Buildfile: build3.xml5 6 run:7 [echo] start...8 9 clean: 10 [mkdir] Created dir: D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15 11 12 test: 13 [jmeter] Executing test plan: D:\B_TOOLS\apache-jmeter-2.13\demo\baidu.jmx ==> D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.jtl 14 [jmeter] Creating summariser <summary> 15 [jmeter] Created the tree successfully using D:\B_TOOLS\apache-jmeter-2.13\demo\baidu.jmx 16 [jmeter] Starting the test @ Tue Sep 22 15:50:33 CST 2015 (1442908233010) 17 [jmeter] Waiting for possible shutdown message on port 4445 18 [jmeter] summary + 1 in 1s = 2.0/s Avg: 254 Min: 254 Max: 254 Err: 0 (0.00%) Active: 1 Started: 1 Finished: 0 19 [jmeter] summary + 5 in 1s = 7.3/s Avg: 131 Min: 96 Max: 196 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1 20 [jmeter] summary = 6 in 1.2s = 5.0/s Avg: 151 Min: 96 Max: 254 Err: 0 (0.00%) 21 [jmeter] Tidying up ... @ Tue Sep 22 15:50:34 CST 2015 (1442908234310) 22 [jmeter] ... end of run 23 [jmeter] Executing test plan: D:\B_TOOLS\apache-jmeter-2.13\demo\google.jmx ==> D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.jtl 24 [jmeter] Creating summariser <summary> 25 [jmeter] Created the tree successfully using D:\B_TOOLS\apache-jmeter-2.13\demo\google.jmx 26 [jmeter] Starting the test @ Tue Sep 22 15:50:35 CST 2015 (1442908235240) 27 [jmeter] Waiting for possible shutdown message on port 4445 28 [jmeter] summary + 1 in 1s = 1.9/s Avg: 253 Min: 253 Max: 253 Err: 1 (100.00%) Active: 1 Started: 1 Finished: 0 29 [jmeter] summary + 5 in 1.1s = 4.4/s Avg: 209 Min: 97 Max: 420 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1 30 [jmeter] summary = 6 in 2s = 3.6/s Avg: 216 Min: 97 Max: 420 Err: 1 (16.67%) 31 [jmeter] Tidying up ... @ Tue Sep 22 15:50:36 CST 2015 (1442908236992) 32 [jmeter] ... end of run 33 34 report: 35 [xslt] Processing D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.jtl to D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15\TestReport.html 36 [xslt] Loading stylesheet D:\B_TOOLS\apache-jmeter-2.13\extras\jmeter-results-detail-report_21.xsl 37 [copy] Copying 2 files to D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15 38 39 BUILD SUCCESSFUL 40 Total time: 5 seconds 41 [workspace] $ cmd /c call D:\Users\heman793\AppData\Local\Temp\hudson4765606743104861298.bat 42 43 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace>echo D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace 44 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace 45 46 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace>echo 15 47 15 48 49 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace>cd results 50 51 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results>cd 15 52 53 D:\B_TOOLS\jenkins\jobs\TestJenkins\workspace\results\15>exit 0 54 Finished: SUCCESS3、在job的workspace目錄下會(huì)生成結(jié)果報(bào)告:
4、TestReport.html:
更新補(bǔ)充:
上面生成的報(bào)告有如下兩個(gè)問(wèn)題,解決辦法參見(jiàn)Jmeter自帶報(bào)告優(yōu)化(一):
1、Date report這里的時(shí)間沒(méi)有正確顯示出來(lái)
2、Summary里的字段Min Time和Max Time顯示的是NaN,沒(méi)有顯示正確的時(shí)間。
?
五、配置發(fā)送郵件功能
?1、自已寫一個(gè)發(fā)送郵件的功能并打成sendmail.jar包,放在job的workspace目錄中
?2、jenkins增加構(gòu)建步驟
a)進(jìn)入到測(cè)試報(bào)告的目錄
b) 調(diào)用sendmail.jar命令發(fā)送郵件
?說(shuō)明:
1、由build3.xml的第12、13行可知,報(bào)告文件生成目錄為:${env.WORKSPACE}/results/${env.BUILD_ID},所以這里我要先cd到具體執(zhí)行的那個(gè)build_id目錄下。
? 2、我們也可以把上面的兩行命令寫在成一個(gè)批處理文件,例如我第1步有個(gè)sendmail.bat文件就是的,然后調(diào)用時(shí)直接寫sendmail.bat就好了。
?
另外,我本文所有腳本都在我本地,其實(shí)更好的辦法是使用SVN統(tǒng)一管理,這里就不介紹了,感覺(jué)的可以自己研究下。
轉(zhuǎn)載于:https://www.cnblogs.com/ninefish/p/10008306.html
總結(jié)
以上是生活随笔為你收集整理的[搬运工系列]-JMeter(二十四)搭建持续集成接口测试平台(Jenkins+Ant+Jmeter)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: BZOJ3224普通平衡树
- 下一篇: oracle 清空表数据的2种方式及速度