Wednesday, April 15, 2009

JMeter: run tests using ANT

Do you run your test cases in jmeter GUI, i really never like to run my tests in it. I always like to have command line options or preferably ANT. Following are the steps to run any JMeter test cases using ANT:

1. Install ANT and JMeter
2. Copy ant-jmeter.jar ant ANT_HOME/lib directory.
3. Create a common.xml file (as mentioned below) which can be referred by any Jmeter Test plan.
4. Create a build.xml for every test plan, which refers to common.xml
5. Use xslt task to create the html report of the test cases.

common.xml

<project name="jmeter-test-common" default="run" basedir=".">

<property name="jmeter.dir" value="C:/Jmeter"/>
<taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>

<target name="run-test">

<echo message="${jmeter.dir}"/>
<jmeter jmeterhome="${jmeter.dir}"
testplan="${test.dir}/${jmeter.file}.jmx"
resultlog="${test.dir}/${jmeter.file}.jtl">
<property name="jmeter.save.saveservice.output_format" value="xml"/>
<property name="jmeter.save.saveservice.assertion_results" value="all"/>
<property name="request.threads" value="${request.threads}"/>
<property name="request.loop" value="${request.loop}"/>
<jvmarg value="-Duser.dir=${test.dir}"/>
</jmeter>

<echo message="Generating Report ..."/>

<xslt in="${test.dir}/${jmeter.file}.jtl"
out="${test.dir}/${jmeter.file}_summary.html"
style="${jmeter.dir}/extras/jmeter-results-report_21.xsl"/>

<xslt in="${test.dir}/${jmeter.file}.jtl"
out="${test.dir}/${jmeter.file}_detailed.html"
style="${jmeter.dir}/extras/jmeter-results-detail-report_21.xsl"/>
</target>
</project>


build.xml


<project name="my-test" default="run" basedir=".">

<import file="${ant.file.my-test}/common.xml"/>

<target name="run">
<antcall target="run-test">
<param name="jmeter.file" value="LOGIN"/>
<!-- here LOGIN is the jmx file name -->
<param name="test.dir" value="${ant.file}/.."/>
<param name="request.threads" value="5"/>
<param name="request.loop" value="1"/>
</antcall>
</target>

</project>


25 comments:

Unknown said...

Hi Vishnu,
I tried using the ant script which u me hv given, but the properties request.threads, request.loops are ignoring when the test plan is executed. Its taking only the values which is mentioned in jmeter gui for that test plan. Could you please reply on this how to pass the thread request and loop count dynamically through ant script

Unknown said...

Hi Vishnu,

The html report generated through the ANT scripts shows all the values
for the test plan like Failures,Success Rate, Avg time , but the values for the paramaters Max Time and Min Time is ignoring, it showing like "NaN".
Could you please reply on this

Unknown said...

Hi Vishnu,
I have two questions here
1:-
In jmeter ant task , i know that we can run all the test plans at a time by using the tag testplans=${path}/*.jmx.But i have requirement where to run more than one test plan by mentioning the names of test plans through command arguments while running through ant script.
C:/>ant -D=testplan1;-D=testplan2;-D=testplan3 ... run.
2:-
I have read in some site, that to run the testplan, its necessary to give the testplan name as command line argument like
C:/>ant -Dtest=testPlanName run even though we are not mentioning any command line arguments for testplan names in build.xml file
Could you please reply my above two questions, if u have any solutions on this.This will be help full for me to move further where iam to automate the entire process to run the Testplans

Vishnu Agrawal said...

Hi Kiran,

for request.threads and request.loops, you should use following properties in the test plan ${__P(request.threads)} and ${__P(request.loop)} for thread request and loop count respectively.

Vishnu Agrawal said...

Hi Kiran,

for NaN issue, which java version you are using? I think it's an issue with java versions above 1.4.2. It should work fine in Java 1.4.2

Unknown said...

u mean, need to mention the properties ${__P(request.threads)} and ${__P(request.loop)} in jmeter GUI for the specific test plan. so the properties request.thread & request.loop values which i will be giving in ant script will be considered or not?

Unknown said...

Iam using java 1.6 version, u mean to Say, i need say , that it will work for till java 1.4

Vishnu Agrawal said...

Yes, you need to mention the properties ${__P(request.threads)} and ${__P(request.loop)} in jmeter GUI for the specific test plan. so the properties request.threads & request.loop values which you will be giving in ant script will be considered.

Vishnu Agrawal said...

Can you try it with Java 1.4.2 once?

Unknown said...

Where exactly i need to give those values in GUI..iam a begineer in jmeter. Will u please mention the step to be followed in GUI.It will be helpfull for me.

Vishnu Agrawal said...

Test Plan -> Thread Group

In Thread properties panel :

Number of Threads (users) : ${__P(request.threads)}

Loop Count: ${__P(request.loop)}

Also make sure that in ant you are using the same variable i.e. request.threads and request.loop

Let me know, if is still clear to you.

Unknown said...

Thanx a lot...i will reply u in next 5 minutes...let me try now itself

Unknown said...

hi,
i tried for thread request and loop request. It worked out..thanx a lot.
But if we run all the test plans at a time using testplans tag, how is it possible to apply
different values to Thread.request and loop.request for different test plans in ant script
Could you please reply on this.

Unknown said...

Hi vishnu,
Is it possible to write out own test plans using jmeter api with out using the jmeter gui.
If u have any idea reply me.
and also reply for the 3rd question.
Thanx a lot

Vishnu Agrawal said...

Kiran,

No idea about using testplans in the ant build file. Btw, if your src directory is same and only testplans name are different, then there should not be any problem but if directories are also different then i need to invest time on this to make it work. At this time no idea on it.

Also not worked on using JMeter APIs

Unknown said...

The html report generated through the ANT scripts shows all the values
for the test plan like Failures,Success Rate, Avg time , but the values for the paramaters Max Time and Min Time is ignoring, it showing like "NaN".
As if you mentioned, i have tried with java 1.4.2. It worked fine .but what need to be done to show the values for Maxtime and minTime using java1.6
Please reply on ASAP

ramemon said...

Use the build file
http://svn.apache.org/repos/asf/jakarta/jmeter/trunk/extras/build.xml

Include
1. xalan.jar
2. serializer.jar both in jmeter lib directory


Regards
R.A.M

Unknown said...

Hi Vishnu, I have a question on little bit complex scenario.

I need to apply wrap secure tokens around URL before sending URL to hit server.
For the tokens I have a Java program written and I can call it in Ant. That part is working.

But how to make sure every is gets wrapped with this logic before sending to server.?

Can you please let me know if you have any suggestion for this.

Appreciate your help.

Thank you,
Satish

anilchandna said...

Hi Vishnu
Can we customize the Output file (.jtl) that will get used for the report generation?

What kind of changes :-
Everytime for a failed sampler, i am getting result like "Internal Server Error" for every failed sampler. and the HTML response for those samplers are like "invalid DBuser"
"instance missing"

Can we customise that .jtl file to get some RM like "instance missing" or some other solution
It would be easier then to distinguish between the failure of different samplers

@blogger said...

Hi
Is it possible to use the build.xml file for distributed testing?If yes can you provide insights into how to achieve that.
thanks

Vishnu Agrawal said...

@blogger - Can you please elaborate on what you want to achieve.

balu said...

hi vishnu,

can we mention host no and port number from build.xml

Vishnu Agrawal said...

@balu - ofcourse yes. define variables as UDV and pass them thru build.xml as mentioned in this post.

Shrilekha said...

Hi vishnu,

I have "Java 1.7.0" version on my machine and I am getting an error of response time i.e. it is showing "Nan". I do not want to use older version of java.
Please suggest me some solution for it...

Teleb, Hanan said...

Hi Vishnu,

Thank you very much for sharing your info. actually i have tried the build.xml and common.xml file you mentioned, with only changed the directory of jmeter and test file, but i am getting error when run ant as below

C:\apache-ant-1.7.1\bin>ant
Buildfile: build.xml

BUILD FAILED
C:\apache-ant-1.7.1\bin\build.xml:3: Cannot find C:\apache-ant-1.7.1\bin\build.x
ml/common.xml imported from C:\apache-ant-1.7.1\bin\build.xml

Total time: 0 seconds
C:\apache-ant-1.7.1\bin>