首页 > 代码库 > ANT批量执行Jmeter脚本
ANT批量执行Jmeter脚本
ANT批量执行Jmeter脚本
一、环境准备:
1、Jdk1.6或以上:http://www.oracle.com/technetwork/java/javase/downloads/index.html
命令行输入:java -version,出现如下提示说明安装成功
2、ANT下载:http://ant.apache.org/bindownload.cgi
命令行输入:ant -v,出现如下提示说明安装成功
3、Jmeter下载:http://jmeter.apache.org/download_jmeter.cgi
4、将 jmeter的extras目录中ant-jmeter-1.1.1.jar包拷贝至ant安装目录下的lib目录中
5、修改Jmeter的bin目录下jmeter.properties文件的配置:jmeter.save.saveservice.output_format=xml
二、Jmeter脚本:
1、如何编写脚本请参见:http://www.cnblogs.com/puresoul/p/4740436.html
2、脚本目录:D:\apache-jmeter-2.13\demo
3、脚本内容:
测试百度.jmx Google1.jmx
三、build.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<projectname="ant-jmeter-test" default="run"basedir=".">
<tstamp>
<formatproperty="time" pattern="yyyyMMddhhmm" />
</tstamp>
<!-- 需要改成自己本地的Jmeter 目录-->
<propertyname="jmeter.home" value="http://www.mamicode.com/D:/apache-jmeter-2.13" />
<!-- jmeter生成jtl格式的结果报告的路径-->
<property name="jmeter.result.jtl.dir"value="http://www.mamicode.com/D:/apache-jmeter-2.13/demo/report/jtl" />
<!-- jmeter生成html格式的结果报告的路径-->
<propertyname="jmeter.result.html.dir"value="http://www.mamicode.com/D:/apache-jmeter-2.13/demo/report/html" />
<!-- 生成的报告的前缀-->
<propertyname="ReportName" value="http://www.mamicode.com/TestReport" />
<propertyname="jmeter.result.jtlName" value="http://www.mamicode.com/${jmeter.result.jtl.dir}/${ReportName}${time}.jtl"/>
<propertyname="jmeter.result.htmlName"value="http://www.mamicode.com/${jmeter.result.html.dir}/${ReportName}${time}.html" />
<target name="run">
<antcalltarget="test" />
<antcalltarget="report" />
</target>
<target name="test">
<taskdefname="jmeter"classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
<jmeterjmeterhome="${jmeter.home}"resultlog="${jmeter.result.jtlName}">
<!-- 声明要运行的脚本。"*.jmx"指包含此目录下的所有jmeter脚本-->
<testplansdir="D:\apache-jmeter-2.13\demo" includes="*.jmx" />
<propertyname="jmeter.save.saveservice.output_format"value="http://www.mamicode.com/xml"/>
</jmeter>
</target>
<targetname="report">
<xsltin="${jmeter.result.jtlName}"
out="${jmeter.result.htmlName}"
style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"/>
<!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝-->
<copy todir="${jmeter.result.html.dir}">
<filesetdir="${jmeter.home}/extras">
<includename="collapse.png" />
<includename="expand.png" />
</fileset>
</copy>
</target>
</project>
四、运行脚本:
1、cmd进入脚本目录:D:\apache-jmeter-2.13\demo
2、输入:ant 或 ant run(run为build.xml中的task名),执行结果:
D:\apache-jmeter-2.13\demo>ant
Buildfile: build.xml
all:
test:
[jmeter] Executing test plan:D:\apache-jmeter-2.13\demo\Google1.jmx ==>D:\apache-jmeter-2.13\demo\report\jtl\TestReport201509141114.jtl
[jmeter] Creating summariser<summary>
[jmeter] Created the tree successfullyusing D:\apache-jmeter-2.13\demo\Google1.jmx
[jmeter] Starting the test @ Mon Sep14 23:14:32 CST 2015 (1442243672984)
[jmeter] Waiting for possible shutdownmessage on port 4445
[jmeter] summary + 1 in 1s = 1.9/s Avg: 248 Min: 248 Max: 248 Err: 1 (100.00%) Active: 1 Started: 1 Finished:0
[jmeter] summary + 5 in 1s = 7.8/s Avg: 119 Min: 107 Max: 137 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1
[jmeter] summary = 6 in 1.2s = 5.2/s Avg: 140 Min: 107 Max: 248 Err: 1 (16.67%)
[jmeter] Tidying up ... @ Mon Sep 14 23:14:34 CST 2015(1442243674232)
[jmeter] ... end of run
[jmeter] Executing test plan:D:\apache-jmeter-2.13\demo\测试百度.jmx ==>D:\apache-jmeter-2.13\demo\report\jtl\TestReport201509141114.jtl
[jmeter] Creating summariser<summary>
[jmeter] Created the tree successfullyusing D:\apache-jmeter-2.13\demo\测试百度.jmx
[jmeter] Starting the test @ Mon Sep14 23:14:35 CST 2015 (1442243675376)
[jmeter] Waiting for possible shutdownmessage on port 4445
[jmeter] summary + 1 in 0.5s = 2.1/s Avg: 196 Min: 196 Max: 196 Err: 0 (0.00%) Active: 1 Started: 1 Finished: 0
[jmeter] summary + 5 in 1s = 8.4/s Avg: 113 Min: 107 Max: 133 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1
[jmeter] summary = 6 in 1.1s = 5.6/s Avg: 126 Min: 107 Max: 196 Err: 0 (0.00%)
[jmeter] Tidying up ... @ Mon Sep 14 23:14:36 CST 2015(1442243676535)
[jmeter] ... end of run
report:
[xslt] ProcessingD:\apache-jmeter-2.13\demo\report\jtl\TestReport201509141114.jtl toD:\apache-jmeter-2.13\demo\report\html\TestReport201509141114.html
[xslt] Loading stylesheetD:\apache-jmeter-2.13\extras\jmeter-results-detail-report_21.xsl
[copy] Copying 2 files toD:\apache-jmeter-2.13\demo\report\html
BUILD SUCCESSFUL
Total time: 5 seconds
3、测试报告目录:D:\apache-jmeter-2.13\demo\report\html,其中有一个红色失败的案例是我故意设置失败,以便查看的。
四:最后,说一个我在使用过程中碰到一个问题,上面build.xml文件第29行有一句必须要加上:<propertyname="jmeter.save.saveservice.output_format"value="http://www.mamicode.com/xml"/>,否则会报如下的错误:
report:
[xslt] ProcessingD:\Tools\apache-jmeter-2.13\demo\report\jtl\TestReport201509210923.jtl toD:\Tools\apache-jmeter-2.13\demo\report\html\TestReport201509210923.html
[xslt] Loading stylesheet D:\Tools\apache-jmeter-2.13\extras\jmeter-results-detail-report_21.xsl
[xslt] : Error! 前言中不允许有内容。
[xslt] : Error!com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: 前言中不允许有内容。
[xslt] Failed to processD:\Tools\apache-jmeter-2.13\demo\report\jtl\TestReport201509210923.jtl
因为不加上这一句,生成的.jtl文件是文本文件不是xml文件,使用xsl去转换.jtl文件时就会报错。
更新补充:
上面生成的报告有如下两个问题,解决办法参见Jmeter自带报告优化(一):
1、Date report这里的时间没有正确显示出来
2、Summary里的字段Min Time和Max Time显示的是NaN,没有显示正确的时间。
参考:
1、http://rainbow702.iteye.com/blog/2159244
2、http://www.programmerplanet.org/projects/jmeter-ant-task/
ANT批量执行Jmeter脚本