Thursday 6 September 2012

Running JMeter tests with Apache Ant


As we all know JMeter can run tests very efficiently but when it comes to reporting and publishing results, it is not good as other tools available in the market.


What i have learned recently is we can create fancy .html reports of our JMeter tests results with apache-ant and publish to anywhere you like (example: wiki or confluence). 

So following are the steps for running JMeter with Apache Ant.
  1. Download apache-ant-1.8.4 and extract to your “/opt/” folder.
  2. Download xalan-2.1.0.jar & serializer-2.7.1.jar and put them in the “/opt/apache-ant-1.8.4/lib” folder of ant.
  3. Make sure you have ant-jmeter-*.jar in your jmeter/extras folder, if not then download it and save into same folder.
  4. Now set the "ANT_HOME" environment variable, "/opt/apache-ant-1.8.4" in your (vi ~/.bashrc). Example: export ANT_HOME=/opt/apache-ant-1.8.4
  5. Add Apache Ant to your class path element "$ANT_HOME/bin" in your (vi ~/.bashrc). Example: export PATH=$JAVA_HOME/bin:$GRADLE_HOME/bin:$JODA:$ANT_HOME/bin:$PATH
  6. To make sure Apache Ant is successfully added into classpath, type echo $PATH and you should see something similar like Pic 1.
  7.  Now finally download this build.xml and replace with the one which comes with ant, save the downloded build.xml into "/optapache-ant-1.8.4/bin" folder.
Pic1
You are now ready to run your jmeter tests with ant now. But first let's understand what build.xml is doing.

Pic 2

The above build.xml has 3 properties jmeter.home, test and test.plan which we will define while running the ant command. <jmeter.home> is the home directory of jmeter. <test> is the test script name which we want to run with ant. <test.plan> is the test plan directory where jmeter scripts are exist.

If you need to make you ant script more fixable and want to pass optional parameters and properties to JMeter than use similar like following.

<jvmarg value="-Xincgc"/>
<jvmarg value="-Xmx128m"/>
<jvmarg value="-Dproperty=value"/>
<property name="request.threads" value="5"/>
<property name="request.loop" value="50"/>
<property name="jmeter.save.saveservice.assertion_results" value="all"/>
<property name="jmeter.save.saveservice.output_format" value="xml"/>

under the target name run-functional-tests you can see testplans dir element id which triggers the jmeter.

<testplans dir="${jmeter.home}/bin" includes="${test}.jmx"/>

Code under the <xslt> tag basically do the transformation for us and create the fancy .html result report of JMeter test.

Now let' see command to run the JMeter test with ant

Open terminal and navigates to $ /opt/apache-ant-1.8.4/bin

Type:  ant -Djmeter.home=/opt/JMeter/apache-jmeter-2.6 -Dtest=SanityTest

If everything is perfectly setup you will see following message in your console

Pic 3

After running tests you will see the BUILD SUCCESSFUL message in your console like this.



Pic 4

To see .html report you need to navigate to the target folder of JMeter and open the Sa.html in browser, which will appear like this

Pic 5
Tips:
1. If your looking NaN in you report under (Min Time & Max Time) column it means xalan-2.1.0.jar & serializer-2.7.1.jar are not present in the ant. 

You can also reference them in your build.xml as well.

Well that's it for now and i hope this is very helpful.

5 comments:

  1. Hi Abdul,
    I am trying to display jmeter load testing reports using ANT but I am getting the first request being displayed twice.Did you face such Kind of issue.
    (to take example the first Request CRMReg User 1 is displayed twice) the rest of it is correct.Please help if you know why I am facing such issues.

    ReplyDelete
  2. Hi Abdul,
    I am trying to display jmeter load testing reports using ANT but I am getting the first request being displayed twice.Did you face such Kind of issue.
    (to take example the first Request CRMReg User 1 is displayed twice) the rest of it is correct.Please help if you know why I am facing such issues.

    ReplyDelete
  3. Hi Abdul,
    I am trying to display jmeter load testing reports using ANT but I am getting the first request being displayed twice.Did you face such Kind of issue.
    (to take example the first Request CRMReg User 1 is displayed twice) the rest of it is correct.Please help if you know why I am facing such issues.

    ReplyDelete
  4. Hi I am facing the same issue, first result is displayed twice plus min/max time is always NaN

    ReplyDelete
  5. if i am running the ant jmeter programme through the command prompt then how we will show some particular result which we want to check...if after executing the the programme i need to get some output other than the result which u shown. then where to write the code.

    ReplyDelete