Apache Jakarta and Beyond: A Java Programmeramp;#039;s Introduction [Electronic resources]

Larne Pekowsky

نسخه متنی -صفحه : 207/ 55
نمايش فراداده

7.4. Using JMeter with Ant

JMeter was originally designed to be used interactively through a graphic inter-face, as has been done in this chapter. However, as previous chapters have illustrated testing becomes even more useful when it is included in the normal build cycle. Therefore, a means to integrate JMeter into Ant has been provided from http://www.programmerplanet.org/ant-jmeter/. Although this is not part of the official JMeter distribution, it works quite well and quite easily.

The code needed to call JMeter from Ant is distributed as a jar file, ant-jmeter.jar, which can be placed in the standard Ant libs directory or manually added to the CLASSPATH. This jar file provides a custom Ant task called JMeterTask which works much like those discussed in 2. Listing 7.1 contains a build.xml file that uses this new task.

Listing 7.1. An Ant build file that invokes JMeter
<project name="JMeter demo" default="format" basedir=".">
<taskdef
name="jmeter"
classname=
"org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
<target name="test">
<jmeter
jmeterhome="${toolbook.home}/jakarta-jmeter-2.0.1"
testplan="demotest.jmx"
resultlog="demotest.jtl"/>
</target>
<target name="format" depends="test">
<xslt
in="demotest.jtl"
out="demotes202"
style="jmeter-results-report.xsl"/>
</target>
</project>

First, the custom task is loaded. Next, the jmeter target uses this task to run the tests. The jmeterhome attribute specifies where JMeter is installed. Here this location is given relative to the toolbook home, which should name the directory where the CD-ROM contents have been placed. The demotest.jmx file contains the test definitions in XML form, and resultlog specifies the name of a file where the results should be written.

When Ant is invoked as

ant -Dtoolbook.home=C:toolbook test

(replacing the value of toolbook.home as appropriate), it will generate the following output:

Buildfile: build.xml
test:
[jmeter] Executing test plan: demotest.jmx
[jmeter] Created the tree successfully
[jmeter] Starting the test
[jmeter] Tidying up ...
[jmeter] ... end of run
BUILD SUCCESSFUL
Total time: 18 seconds

Note that the success, failure, and performance results are not reported here but are all placed in demotest.jtl. This is another XML file. Usually this file will be quite verbose and difficult to read, so an XSLT template such as the one discussed in Chapter 4 has been provided to process the results into a more readable form. This is done by the format task, which leaves the result in demotes202. This file may be viewed in a browser, and the results will look like Figure 7.8.

Figure 7.8. HTML results of a JMeter test.

[View full size image]