一、环境准备:
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/
具体可参见我另一篇博客:http://www.cnblogs.com/puresoul/p/4808416.html
二、Jemter脚本准备:
1、脚本目录:D:B_TOOLSapache-jmeter-2.13demo
2、脚本内容:都是简单的访问百度或google首页
baidu.jmx google.jmx
三、ANT的build.xml代码准备:
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_TOOLSapache-jmeter-2.13" /> 12 <!-- jmeter生成jtl、html格式的结果报告的路径--> 13 <property name="jmeter.result.dir" value="${env.WORKSPACE}/results/${env.BUILD_ID}" /> 14 <!-- 生成的报告的前缀--> 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 <!-- 声明要运行的脚本"*.jmx"指包含此目录下的所有jmeter脚本--> 33 <testplans dir="D:B_TOOLSapache-jmeter-2.13demo" 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 <!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝 --> 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并运行:
1、job配置如下:
2、job运行结果:
1 Started by user anonymous 2 Building in workspace D:B_TOOLSjenkinsjobsTestJenkinsworkspace 3 [demo] $ cmd.exe /C '"ant.bat -file build3.xml run && exit %%ERRORLEVEL%%"' 4 Buildfile: build3.xml 5 6 run: 7 [echo] start... 8 9 clean: 10 [mkdir] Created dir: D:B_TOOLSjenkinsjobsTestJenkinsworkspace esults15 11 12 test: 13 [jmeter] Executing test plan: D:B_TOOLSapache-jmeter-2.13demoaidu.jmx ==> D:B_TOOLSjenkinsjobsTestJenkinsworkspace esults15TestReport.jtl 14 [jmeter] Creating summariser <summary> 15 [jmeter] Created the tree successfully using D:B_TOOLSapache-jmeter-2.13demoaidu.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_TOOLSapache-jmeter-2.13demogoogle.jmx ==> D:B_TOOLSjenkinsjobsTestJenkinsworkspace esults15TestReport.jtl 24 [jmeter] Creating summariser <summary> 25 [jmeter] Created the tree successfully using D:B_TOOLSapache-jmeter-2.13demogoogle.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_TOOLSjenkinsjobsTestJenkinsworkspace esults15TestReport.jtl to D:B_TOOLSjenkinsjobsTestJenkinsworkspace esults15TestReport.html 36 [xslt] Loading stylesheet D:B_TOOLSapache-jmeter-2.13extrasjmeter-results-detail-report_21.xsl 37 [copy] Copying 2 files to D:B_TOOLSjenkinsjobsTestJenkinsworkspace esults15 38 39 BUILD SUCCESSFUL 40 Total time: 5 seconds 41 [workspace] $ cmd /c call D:Usersheman793AppDataLocalTemphudson4765606743104861298.bat 42 43 D:B_TOOLSjenkinsjobsTestJenkinsworkspace>echo D:B_TOOLSjenkinsjobsTestJenkinsworkspace 44 D:B_TOOLSjenkinsjobsTestJenkinsworkspace 45 46 D:B_TOOLSjenkinsjobsTestJenkinsworkspace>echo 15 47 15 48 49 D:B_TOOLSjenkinsjobsTestJenkinsworkspace>cd results 50 51 D:B_TOOLSjenkinsjobsTestJenkinsworkspace esults>cd 15 52 53 D:B_TOOLSjenkinsjobsTestJenkinsworkspace esults15>exit 0 54 Finished: SUCCESS
3、在job的workspace目录下会生成结果报告:
4、TestReport.html:
更新补充:
上面生成的报告有如下两个问题,解决办法参见Jmeter自带报告优化(一):
1、Date report这里的时间没有正确显示出来
2、Summary里的字段Min Time和Max Time显示的是NaN,没有显示正确的时间。
五、配置发送邮件功能
1、自已写一个发送邮件的功能并打成sendmail.jar包,放在job的workspace目录中
2、jenkins增加构建步骤
a)进入到测试报告的目录
b) 调用sendmail.jar命令发送邮件
说明:
1、由build3.xml的第12、13行可知,报告文件生成目录为:${env.WORKSPACE}/results/${env.BUILD_ID},所以这里我要先cd到具体执行的那个build_id目录下。
2、我们也可以把上面的两行命令写在成一个批处理文件,例如我第1步有个sendmail.bat文件就是的,然后调用时直接写sendmail.bat就好了。
另外,我本文所有脚本都在我本地,其实更好的办法是使用SVN统一管理,这里就不介绍了,感觉的可以自己研究下。