1.参考Jenkins+Ant+JMeter集成,安装Jenkins(不需要安装Performance Plugin插件),建立Slave节点,连接Slave节点,创建任务等。
2.将Jenkins+Ant+JMeter集成中创建的任务的第5步“构建后操作”删除,并将第1步修改(点击configure进行删除或修改)如下:
3.构建前,创建目录及所需文件:
目录1:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <table align="center"> <tr align="center"><td><H1>Performance Test Report</H1></td></tr> </table> <table align="center"> <tr align="center"><td><H2>TPS</H2></td></tr> <tr align="center"><td><IMG SRC="TPS.png" WIDTH="800" HEIGHT="600" BORDER="0" ALT=""></td></tr> </table> <table align="center"> <tr align="center"><td><H2>Response Time</H2></td></tr> <tr align="center"><td><IMG SRC="ResponseTimeOverTime.png" WIDTH="800" HEIGHT="600" BORDER="0" ALT=""></td></tr> </table> </BODY> </HTML>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0"?> <project name="ant-jmeter" default="all"> <description> Jmeter+Ant+Jenkins <!--文件描述,可选择项--> </description> <!--定义变量并赋值,能够减少配置工作量,后面会用到--> <!--定义JMeter的工作目录,与%JMETER_HOME%指向同一个目录--> <property name="jmeter.home" value="D:softwareapache-jmeter-5.1"/> <!--定义JMeter脚本存放目录--> <property name="script.dir" value="D:jmeterTestscript"/> <!--定义JMeter的测试结果存放目录--> <property name="jmeter.result.dir" value="D:jmeterTest esultLog"/> <!--定义Jenkins的工作目录,将来用来存放测试结果--> <property name="jmeter.jenkins" value="D:jenkinsWorkshopworkspacePerformanceTest"/> <!--定义测试报告Title--> <property name="report.title" value="Performance Test Summary Report"/> <!--定义测试结果名称--> <property name="jmeter.result.name" value="Test"/> <!--测试报告中是否显示失败事务,n为不显示--> <property name="show-data" value="n"/> <!--定义样式版本,有更新时只需要修改此处即可,其他调用处不用更改--> <property name="format" value="2.1"/> <condition property="style_version" value=""> <equals arg1="${format}" arg2="2.0"/> </condition> <condition property="style_version" value="_21"> <equals arg1="${format}" arg2="2.1"/> </condition> <condition property="funcMode"> <equals arg1="${show-data}" arg2="y"/> </condition> <condition property="funcMode" value="false"> <not> <equals arg1="${show-data}" arg2="y"/> </not> </condition> <!--定义依赖包目录--> <path id="jmeter.classpath"> <fileset dir="${jmeter.home}extras"> <include name="ant-jmeter*.jar"/> </fileset> </path> <!--定义任务,即测试活动,依赖于ant-jmeter.jar包--> <taskdef name="jmeter" classpathref="jmeter.classpath" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/> <!--定义任务,run运行脚本,generatePNG生成图片报表,report生成报告,copy-report复制结果--> <target name="all" depends="run,generatePNG,report,copy-report"/> <!--定义generatePNG任务--> <target name="generatePNG"> <exec dir="D:jmeterTest" executable="cmd.exe"> <arg line="/c D:jmeterTestgenerateRT.bat" /> <arg line="/c exit" /> </exec> <exec dir="D:jmeterTest" executable="cmd.exe"> <arg line="/c D:jmeterTestgenerateTPS.bat" /> <arg line="/c exit" /> </exec> </target> <!--定义run任务--> <target name="run"> <!--显示上面定义的${show-data}取值--> <echo>funcMode = ${funcMode}</echo> <!--删除前一次的测试报告,Html格式的,上面有定义${jmeter.result.name}变量的值--> <delete file="${jmeter.result.dir}/${jmeter.result.name}.html"/> <!--删除前一次的测试报告,Html格式的,上面有定义${jmeter.result.name}变量的值--> <delete file="${jmeter.result.dir}/${jmeter.result.name}.jtl"/> <jmeter jmeterhome="${jmeter.home}" jmeterproperties="${jmeter.home}/bin/jmeter.properties" runremote="true" resultlog="${jmeter.result.dir}/${jmeter.result.name}.jtl"> <!-- 指定测试脚本。"*.jmx"指包含此目录下的所有jmeter脚本 --> <testplans dir="${script.dir}" includes="*.jmx" /> <!--指定测试结果输出格式,在此指定的是xml,也可以指定成文本格式及CSV格式--> <!--先暂时指定成xml格式,后续会变换成文本格式--> <property name="jmeter.save.saveservice.output_format" value="xml"/> <property name="jmeter.save.saveservice.samplerData" value="true"/> <!--是否记录错误信息,上面已经定义了${funcMode}=n--> <property name="jmeter.save.saveservice.response_data.on_error" value="${funcMode}"/> </jmeter> </target> <!--定义依赖包目录--> <property name="lib.dir" value="${jmeter.home}/lib"/> <!-- Use xalan copy from JMeter lib directory to ensure consistent processing with Java 1.4+ --> <path id="xslt.classpath"> <fileset dir="${lib.dir}" includes="xalan*.jar"/> <fileset dir="${lib.dir}" includes="serializer*.jar"/> </path> <!--保留ant利用XSL来生成的报告 start--> <target name="report" depends="xslt-report,copy-images"> <echo>Report generated at ${report.datestamp}</echo> </target> <!--xslt-report是使用xslt来把xml文件转换成Html格式来显示--> <!--_message_xalan 被xslt-report依赖,由它生成报告--> <target name="xslt-report" depends="_message_xalan"> <tstamp><format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/></tstamp> <!--in:将测试结果作为输入--> <!--style:xslt定义的模板文件地址--> <xslt classpathref="xslt.classpath" force="true" in="${jmeter.result.dir}/${jmeter.result.name}.jtl" out="${jmeter.result.dir}/${jmeter.result.name}.html" style="${jmeter.home}/extras/jmeter-results-detail-report${style_version}.xsl"> <param name="showData" expression="${show-data}"/> <param name="titleReport" expression="${report.title}"/> <param name="dateReport" expression="${report.datestamp}"/> </xslt> </target> <!--copy-images用来cpoy图片,xslt模板中的图片由copy-images任务copy到报告目录--> <target name="copy-images" depends="verify-images" unless="samepath"> <!--生成Html报告时需要Copy图标到报告目录,expand.png展开图标,collapse.png收起图标--> <copy file="${jmeter.home}/extras/expand.png" tofile="${jmeter.result.dir}/expand.png"/> <copy file="${jmeter.home}/extras/collapse.png" tofile="${jmeter.result.dir}/collapse.png"/> </target> <target name="verify-images"> <condition property="samepath"> <equals arg1="${jmeter.result.dir}" arg2="${jmeter.home}/extras/" /> </condition> </target> <!--保留ant利用XSL来生成的报告 end--> <!--格式化一个时间戳,以此时间戳为名建立一个文件夹,每次构建一个文件夹,方便保存历次结果--> <tstamp> <format property="CURRHOUR" pattern="yyyyMMddhhmm" offset="0" unit="hour" /> </tstamp> <property name="RESULTNAME" value="${CURRHOUR}"/> <!--把Ant XSL生成的报告与.jtl文件复制到Jenkins Slave的工作区--> <target name="copy-report" depends="copy-images-jenkins"> <!--建立一个文件夹存放结果--> <mkdir dir="${jmeter.jenkins}${RESULTNAME}" /> <copy file="${jmeter.result.dir}/${jmeter.result.name}.html" tofile="${jmeter.jenkins}/${RESULTNAME}/${jmeter.result.name}.html"/> <copy file="${jmeter.result.dir}/${jmeter.result.name}.jtl" tofile="${jmeter.jenkins}/${RESULTNAME}/${jmeter.result.name}.jtl"/> <!--复制集成JMeter Plugins图表的HTML模板--> <copy file="${jmeter.result.dir}/PerformanceTestReport.html" tofile="${jmeter.jenkins}/${RESULTNAME}/PerformanceTestReport.html"/> </target> <!--Ant XSL生成的报告所需的图片也需要拷贝到Jenkins Slave的工作区--> <target name="copy-images-jenkins"> <copydir dest="${jmeter.jenkins}/${RESULTNAME}" src="${jmeter.result.dir}"> <include name="*.png" /> </copydir> </target> <!--检查依赖--> <condition property="xalan.present"> <and> <!-- No need to check all jars; just check a few --> <available classpathref="xslt.classpath" classname="org.apache.xalan.processor.TransformerFactoryImpl"/> <available classpathref="xslt.classpath" classname="org.apache.xml.serializer.ExtendedContentHandler"/> </and> </condition> <target name="_message_xalan" unless="xalan.present"> <echo>Cannot find all xalan and/or serialiser jars</echo> <echo>The XSLT formatting may not work correctly.</echo> <echo>Check you have xalan and serializer jars in ${lib.dir}</echo> </target> <!--仅仅打印前面定义的变量的值,检查是否如你所愿,在定义变量时做调试用,可以去掉--> <echo>jmeter.home = ${jmeter.home}</echo> </project>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
rem TransactionsPerSecond
D:softwareapache-jmeter-5.1inPluginsManagerCMD.bat --tool Reporter --generate-png D:jmeterTest
esultLogTPS.png --input-jtl D:jmeterTest
esultLogTest.jtl --plugin-type TransactionsPerSecond --width 800 --height 600
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
%ANT_HOME%inant -f D:jmeterTestuild.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
rem ResponseTime OverTime
D:softwareapache-jmeter-5.1inPluginsManagerCMD.bat --tool Reporter --generate-png D:jmeterTest
esultLogResponseTimeOverTime.png --input-jtl D:jmeterTest
esultLogTest.jtl --plugin-type ResponseTimesOverTime --width 800 --height 600
注意:可以仿照上面的generateTPS.bat生成其它的所需报表,具体用法见:https://jmeter-plugins.org/wiki/JMeterPluginsCMD/
目录2:
4.修改jmeter.properties文件,使之可以远程启动:
# Remote Hosts - comma delimited remote_hosts=127.0.0.1 #remote_hosts=localhost:1099,localhost:2010
5.双击启动jmeter-server.bat,如果报错如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Found ApacheJMeter_core.jar Server failed to start: java.rmi.server.ExportException: Listen failed on port: 0; nested exception is: java.io.FileNotFoundException: rmi_keystore.jks (系统找不到指定的文件。) An error occurred: Listen failed on port: 0; nested exception is: java.io.FileNotFoundException: rmi_keystore.jks (系统找不到指定的文件。) errorlevel=1 请按任意键继续. . .
则修改jmeter.properties文件即可:
# Set this if you don't want to use SSL for RMI server.rmi.ssl.disable=true
6.点击“立即构建”。构建成功后两个目录如下:
目录1:
目录2:
7. 在Jenkins工作目录中打开PerformanceTestReport.html即可查看报表。