目的:只需把jmx文件和build.xml放在同一目录下,就可以执行当前目录下的所有jmx文件,至于输出结果,都会生成到Report/html文件夹中。后期build.xml不需要再修改。
步骤:
1、jenkins上安装ant插件
2、要实现循环执行jmeter文件,需要下载 ant-contrib-1.0b3.jar 并放在ant的lib目录下,https://www.findjar.com/jar/ant-contrib/ant-contrib/1.0b3/ant-contrib-1.0b3.jar.html
3、配置的build.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project name="ant-jmeter" default="run" basedir="."> 3 <tstamp> 4 <!--生成HTML报告用时间--> 5 <format property="report.datestamp" pattern="yyyy/MM/dd HH:mm" /> 6 <format property="currentTime" pattern="yyyyMMdd-HHmmss" locale="en,UK" /> 7 </tstamp> 8 9 <!-- 定义jmeter相关信息:报告名称、jmeter目录地址--> 10 <property environment="env" /> 11 <property name="report.title" value="Jmeter Test Report" /> 12 <property name="testpath" value="${basedir}"/> 13 <property name="reportpath" value="${basedir}/Report"/> 14 <property name="results.jtl" value="jtl" /> 15 <property name="results.html" value="html" /> 16 17 <!--windows设置用env--> 18 <!--property name="jmeter.home" value="${env.JMETER_HOME}"/> 19 <property name="ant.lib" value="${env.ANT_HOME}/lib"/--> 20 <!--mac或linux设置用,需要手动配置--> 21 <property name="jmeter.home" value="/usr/apache-jmeter-5.0"/> 22 <property name="ant.lib" value="/usr/local/tomcat-9.0.20/webapps/jenkins/plugins/ant/lib"/> 23 <property name="lib.dir" value="${jmeter.home}/lib"/> 24 <!--测试间隔时间--> 25 <property name="interval-time-in-seconds" value="1" /> 26 27 <!-- HTML报告配置相关的jar包--> 28 <path id="xslt.classpath"> 29 <fileset dir="${lib.dir}" includes="xalan*.jar" /> 30 <fileset dir="${lib.dir}" includes="serializer*.jar" /> 31 </path> 32 <!-- Allow jar to be picked up locally --> 33 <path id="jmeter.classpath"> 34 <fileset dir="${jmeter.home}/extras"> 35 <include name="ant-jmeter*.jar"/> 36 </fileset> 37 </path> 38 <!-- jmeter相关class--> 39 <taskdef name="jmeter" 40 classpathref="jmeter.classpath" 41 classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/> 42 <!-- 初始化:删除并重建结果目录--> 43 <target name="init"> 44 <delete dir="${reportpath}/" /> 45 <mkdir dir="${reportpath}/${results.jtl}" /> 46 <mkdir dir="${reportpath}/${results.html}" /> 47 </target> 48 49 <!-- 引入ant循环用jar包--> 50 <target name="all-test" depends="init"> 51 <!-- antcontrib:support for by ant --> 52 <taskdef resource="net/sf/antcontrib/antlib.xml"> 53 <classpath> 54 <pathelement location="${ant.lib}/ant-contrib-1.0b3.jar" /> 55 </classpath> 56 </taskdef> 57 58 <!--循环获取jmx文件--> 59 <for param="file" > 60 <path> 61 <fileset dir="${testpath}"> 62 <include name="*.jmx" /> 63 </fileset> 64 </path> 65 <sequential> 66 <exec-jmeter file-path="@{file}"/> 67 </sequential> 68 </for> 69 </target> 70 71 72 <macrodef name="exec-jmeter"> 73 <attribute name="file-path"/> 74 <sequential> 75 <!--运行jmx脚本--> 76 <var name="jmx.filename" unset = "true"/> 77 <basename property="jmx.filename" file="@{file-path}" suffix=".jmx" /> 78 <echo message="---------- Processing ${jmx.filename}.jmx -----------" /> 79 <jmeter jmeterhome="${jmeter.home}" 80 resultlogdir="${reportpath}/${results.jtl}" 81 resultlog="${jmx.filename}.jtl" 82 testplan="${testpath}/${jmx.filename}.jmx"> 83 <property name="jmeter.save.saveservice.response_data" value="true"/> 84 <property name="jmeter.save.saveservice.samplerData" value="true"/> 85 <property name="jmeter.save.saveservice.responseHeaders" value="true"/> 86 <property name="jmeter.save.saveservice.requestHeaders" value="true"/> 87 <property name="jmeter.save.saveservice.encoding" value="true"/> 88 <property name="jmeter.save.saveservice.url" value="true"/> 89 <property name="jmeter.save.saveservice.filename" value="true"/> 90 <property name="jmeter.save.saveservice.hostname" value="true"/> 91 <property name="jmeter.save.saveservice.thread_counts" value="true"/> 92 <property name="jmeter.save.saveservice.sample_count" value="true"/> 93 <property name="jmeter.save.saveservice.idle_time" value="true"/> 94 <property name="jmeter.save.saveservice.output_format" value="xml"/> 95 <property name="jmeter.save.saveservice.assertion_results" value="all"/> 96 <property name="jmeter.save.saveservice.bytes" value="true"/> 97 </jmeter> 98 99 <!--生成HTML报告--> 100 <xslt 101 classpathref="xslt.classpath" 102 force="true" 103 in="${reportpath}/${results.jtl}/${jmx.filename}.jtl" 104 out="${reportpath}/${results.html}/${jmx.filename}_${currentTime}.html" 105 style="${jmeter.home}/extras/jmeter-results-hph_report.xsl"> 106 <param name="dateReport" expression="${report.datestamp}" /> 107 <param name="titleReport" expression="${report.title}:[${jmx.filename}]" /> 108 </xslt> 109 <echo message="Sleep ${interval-time-in-seconds} Seconds, and then start next Test; Please waiting..." /> 110 <sleep seconds="${interval-time-in-seconds}"></sleep> 111 </sequential> 112 </macrodef> 113 114 <target name="run" depends="all-test" /> 115 </project>
4、Jenkins配置ant的run
5、jenkins中执行结果的附件添加