Ant是一个快速开发的工具,类似Unix下的make,很方便的组织项目,编译,打包,布署等。对于Ant的使用这里就不介绍了,若未使用过,可以在网上找到很多文章和例子。也可联系我
[email protected]。
JMeter也是Apache的一个开源工具,是一个比较流行的WEB应用测试工具,当然作为测试工具,也可以进行其它测试,比如数据库,JDBC等。其本身的使用,比如如何创建一个测试计划,建立监听器,比如使用表格进行结果查看,将测试结果记录到文件中等,也不在这里介绍,如果不清楚,也可以联系我
[email protected]。
由于JMeter本身主要用于性能测试,也即是峰值测试,同时在线的人数和系统压力等。而在实际系统中还需要对“稳定性”测试。比如根据一天的总量(如60000警情),平均间隔多久就会有一个 新警情发生,要求服务器连续工作24或7*24小时的情况进行测试。这样思想就是根据估计,间隔一定时间重复运行一个“测试计划”达到此要求。
1,使用ant进行jmeter测试,同时利用xsl样式查看结果。详细可以参考文章。
2,这里进入本主题,利用ant进行稳定测试。
a,这里我有一个http的测试web1.jmx,测试结果记录到web1.jtl中。利用ant的build.xml文件可以是这样的。
<project name="antjmeter" default="run" basedir=".">
<property name="out" value="."/>
<target name="test">
<taskdef
name="jmeter"
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
<jmeter
jmeterhome="D:\ProgramFiles\jakarta-jmeter-2.1.1"
testplan="${basedir}/web1.jmx"
resultlog="${out}/web1.jtl">
<jvmarg value="-Dgroup1.threads=10"/>
</jmeter>
<echo message="jmeter web1.jmx finished"/>
</target>
<target name="interval">
<echo message="sleep 5 seconds"/>
<sleep seconds="5"/>
<echo message="continue"/>
</target>
<target name="tfile">
<xslt
in="${basedir}/web1.jtl"
out="${basedir}/web1.html"
style="${basedir}/jmeter-results-report.xsl"/>
</target>
<target name="run" depends="test,interval,tfile"/>
</project>
运行结果查看。
b,从上面可以运行一次“测试计划”,如果要连续运行24小时呢?其中加了一个sleep目的就是想在每次运行完后暂停一下。这个根据你的系统要求估算。
c,从基本思想上大家可以想象,如果ant有loop或for的话,也可设定运行次数来达到这个目的。但是目前Ant没有此功能。所以达不到。通过我查看文档,其中有ant和antcall的task,是不是可以利用这个来达到目的呢?
即build.xml中写为
<target name="test">
<taskdef
name="jmeter"
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
<jmeter
jmeterhome="D:\ProgramFiles\jakarta-jmeter-2.1.1"
testplan="${basedir}/web1.jmx"
resultlog="${out}/web1.jtl">
<jvmarg value="-Dgroup1.threads=10"/>
</jmeter>
<echo message="jmeter web1.jmx finished"/>
<antcall target="interval"/>
<antcall target="test"/> <!--循环调用自已来达到持续运行的目的。-->
<!--ant antfile="build.xml"/-->
</target>
结果是使用ant或是antCall都不行。错误如下:
antcall运行报错
BUILD FAILED
D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web3\build.xml:18: antcall task call
ing its own parent target.
ant 运行报错
BUILD FAILED
D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web3\build.xml:19: ant task calling
a target that depends on its parent target 'test'.
d,不过从前者的结果看,是不能call its own。因此我将原target test一样考贝一份取名test2。
则在test中<antcall target="test2"/>,在test2中<antcall target="test"/>,这样运行就可以达到
我想要的连续测试的要求。
3,只是这样也有缺陷,因为这没有编程功能,不能在之中加入比如测500次或测24小时后就不再antcall
而结束的功能。所以要在你想停止的时候手动停止了。
4,这样xslt转换就不能写在这个功能中了,最后手动停止后再做xslt转换或直接在xml中加入xsl显示即可。
D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web3>ant
Buildfile: build.xml
test:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test2:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test2:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test2:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test2:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test2:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
[echo] continue
test:
[jmeter] Executing test plan: D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web
3\web1.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
[echo] jmeter web1.jmx finished
interval:
[echo] sleep 5 seconds
终止批处理操作吗(Y/N)? y
D:\ProgramFiles\jakarta-jmeter-2.1.1\mytest\web3>
e,综合,可以使用此方法来达要求。当你想停止测试时,则手动停止即可。查看上面的这个build
<!-- Simple Ant Jmeter test-->
<project name="antjmeter" default="run" basedir=".">
<property name="out" value="."/>
<target name="test">
<taskdef
name="jmeter"
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
<jmeter
jmeterhome="D:\ProgramFiles\jakarta-jmeter-2.1.1"
testplan="${basedir}/web1.jmx"
resultlog="${out}/web1.jtl">
<jvmarg value="-Dgroup1.threads=10"/>
</jmeter>
<echo message="jmeter web1.jmx finished"/>
<antcall target="interval"/>
<antcall target="test2"/>
<!--ant antfile="build.xml"/-->
</target>
<!--for execute target test many times,copy the target test
to target test2-->
<target name="test2">
<taskdef
name="jmeter"
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
<jmeter
jmeterhome="D:\ProgramFiles\jakarta-jmeter-2.1.1"
testplan="${basedir}/web1.jmx"
resultlog="${out}/web1.jtl">
<jvmarg value="-Dgroup1.threads=10"/>
</jmeter>
<echo message="jmeter web1.jmx finished"/>
<antcall target="interval"/>
<antcall target="test"/>
<!--ant antfile="build.xml"/-->
</target>
<target name="interval">
<echo message="sleep 5 seconds"/>
<sleep seconds="5"/>
<echo message="continue"/>
</target>
<target name="tfile">
<xslt
in="${basedir}/web1.jtl"
out="${basedir}/web1.html"
style="${basedir}/jmeter-results-detail-report.xsl"/>
</target>
<target name="run" depends="test"/>
</project>