Jmeter是性能测试的工具,java编写、开源,小巧方便,可以图形界面运行也可以在命令行下运行。网上已经有人使用ant来运行,,既然可以使用ant运行,那和hudson、jenkins集成很方便了,而且jenkins上也有相应的插件Performance Plugin,可以自动收集jmeter的测试结果,展示出来。
  首先去下载jmeter,在2.8版本中测试通过,2.9版本测试未通过。下载ant-jmeter-1.1.1.jar放在jmeter主目录lib文件夹下。
  下载需要的ant包,包含配置文件和一些jar包。里面的build.xml是配置文件,可以自定义。需要修改其中jmeter路径,然后直接ant运行即可。
<?xml version="1.0" encoding="utf-8"?>
<project default="all">
<!-- Define your Jmeter Home & Your Report Title & Interval Time Between Test-->
<property name="report.title" value="WebLoad Test Report"/>
<property name="jmeter-home" location="D:workapache-jmeter-2.8" />
<property name = "interval-time-in-seconds" value ="10"/>
<!-- default path config, you can modify for your own requirement;Generally, you do not need to modify -->
<property environment="env" />
<property name="runremote" value="false"/>
<property name="resultBase" value="results"/>
<property name="results.jtl" value="jtl"/>
<property name="results.html" value ="html"/>
<property name="jmxs.dir"  value= "jmxs"/>
<tstamp><format property="report.datestamp" pattern="yyyy-MM-dd-HH-mm-ss"/></tstamp>
<property name="time" value="${report.datestamp}"/>
<!--  Diffrent version of Jmeter has its own ant-jmeter.jar,Please input the right versioin -->
<path id="ant.jmeter.classpath">
<pathelement location="${jmeter-home}/lib/ant-jmeter-1.1.1.jar" />
</path>
<taskdef name="jmeter"
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"
classpathref="ant.jmeter.classpath" />
<!-- just to support foreach by ant -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties" >
<classpath>
<pathelement location="./libs/ant-contrib-20020829.jar" />
</classpath>
</taskdef>
<!-- use this config to generate html report; if not, may not display Min/Max Time in html-->
<path id="xslt.classpath">
<fileset dir="./libs" includes="xalan-2.7.1.jar"/>
<fileset dir="./libs" includes="serializer-2.9.1.jar"/>
</path>
<!--运行之前首先创建临时结果文件夹-->
<target name="create-folder">
<delete dir="${resultBase}/temp"/>
<mkdir dir="${resultBase}/temp/${results.jtl}" />
<mkdir dir="${resultBase}/temp/${results.html}" />
</target>
<target name="all-test" depends="create-folder">
<foreach  param="jmxfile" target="test" >
<fileset dir="${jmxs.dir}">
<include name="*.jmx" />
</fileset>
</foreach>
</target>
<target name="test" >
<basename property="jmx.filename" file="${jmxfile}" suffix=".jmx"/>
<echo message="---------- Processing ${jmxfile} -----------"/>
<echo message="resultlogdir===${resultBase}/temp/${results.jtl}"/>
<jmeter jmeterhome="${jmeter-home}" resultlogdir="${resultBase}/temp/${results.jtl}" runremote="${runremote}" resultlog="${jmx.filename}.jtl"
testplan="${jmxs.dir}/${jmx.filename}.jmx">
<jvmarg value="-Xincgc"/>
<jvmarg value="-Xms1024m"/>
<jvmarg value="-Xm1024m"/>
</jmeter>
<sleep seconds="20"></sleep>
<!--Generate html report-->
<xslt   in="${resultBase}/temp/${results.jtl}/${jmx.filename}.jtl"
out="${resultBase}/temp/${results.html}/${jmx.filename}.html"  classpathref="xslt.classpath"
style="${jmeter-home}/extras/jmeter-results-report_21.xsl" >
<param name="dateReport" expression="${report.datestamp}"/>
<param name="showData" expression="n"/>
<param name="titleReport" expression="${report.title}:[${jmx.filename}]"/>
</xslt>
<echo message="Sleep ${interval-time-in-seconds} Seconds, and then start next Test; Please waiting ......"/>
<sleep seconds="${interval-time-in-seconds}"></sleep>
</target>
<target name="copy-images" depends="all-test">
<copy file="${jmeter-home}/extras/expand.png" tofile="${results.html}/expand.png"/>
<copy file="${jmeter-home}/extras/collapse.png" tofile="${results.html}/collapse.png"/>
<copydir src="${resultBase}/temp" dest="${resultBase}/${report.datestamp}"/>
<delete dir="${resultBase}/temp"/>
</target>
<target name="all" depends="all-test, copy-images" />
</project>
  jmxs文件夹存放jmeter脚本,ant会顺序执行其中的脚本,执行结果会放在results文件夹中,包含统计的html文件和jmeter的请求详细jtl文件。
  后和jenkins集成,搭建jenkins环境,安装Performance Plugin插件,新建一个job,选择目标机器(机器上要有ant),填好svn或者cvs、定时执行、构建命令等。在Add post-build action中可以添加一个Publish Performance test result report用来收集jmeter测试结果,选择meter,然后在Report files中填写**/*.jtl即可。