Showing posts with label jmeter. Show all posts
Showing posts with label jmeter. Show all posts

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>


JMeter: Using Variables

In JMeter value of user.dir varaible is set to jmeter/bin directory. Jmeter saves and loads the test script from user.dir, so by default it is jmeter/bin. But what if your test scripts are saved in directory other than jmeter/bin which is the case most of the time.
We can override the value of this varaible in jmeter/bin/jmeter.properties as mentioned below:
user.dir=C:/perftest/mytest
Now the jmeter wil look for test script in C:/perftest/mytest instead of its default location. We can also specify this varaibale as jvm arg; if we are running jmeter from command prompt. (-Duser.dir=C:/perftest/mytest)

Accessing user.dir value in Jmeter test plan
In Test Plan -> User Defined Variables add a new variable
Name: userdir
Value: ${__property(user.dir)}

Now this userdir varaible can be used in jmeter test plan as location of the test script (${userdir}).

Reading values from a file
${__CSVRead(${userdir}/env.txt,0)} -> It will read the first column of the first line from the env.txt file

__P
This is a property function which is intended for use with properties defined on the command line. Unlike the __property function, there is no option to save the value in a variable, and if no default value is supplied, it is assumed to be 1.
${__P(request.threads)} -> return the value of request.threads
${__P(request.loop)} -> return the value of request.loops
${__javaScript(${__P(request.threads)}*2)} -> multiply the value of request.threads by 2

Use JMeter to load test FLEX Applications

Flex applications use a binary protocol called AMF (Action Message Format) that they use to remotely communicate with the server. The easiest way to use JMeter with a Flex application is to use HTTP proxy server.

- Create a New Test Plan
- Right Click on Test Plan, Add -> Thread Group
- Right Click on Thread Group, Add -> Config Element -> HTTP Request Defaults
- Select 'HTTP Request Defaults'
- Specify Server Name or IP, for eg. www.mysite.com
- Specify Port number, eg. 8888
- Specify protocol, i.e. http
- Specify Path, foe eg. /main/index.html
- Select Work Bench
- Right Click on Work bench, Add -> Non-Test Elements -> HTTP Proxy Server
- Select 'HTTP Proxy Server'
- Specify port number for proxy (default is 8080)
- Set Target Controller to 'Test Plan -> Thread Group'
- Set inlude URL patterns , .* to include all
- Set exclude URL patterns, for eg. , .*\.gif, .*\.png, .*\.css, .*\.js
- Open Internet Explorer
- Goto Tools -> Internet Options -> Connections -> LAN settings
- In Proxy Server panel, specify address as 'localhost' and port number (which is specified in
meter's HTTP proxy server, i.e. 8080)

- Select 'HTTP Proxy Server'
- Click on Start
- Open IE and open URL 'http://www.mysite.com:8888/main/index.html'
- As soon as you visit the URL jmeter starts the recording, and you can see all the requests in
METER

- For AMF requests (for Adobe FLEX), request is saved as file with extension '.binary' in the bin
irectory of jmeter

- Click on Stop button of 'HTTP Proxy Server' to stop the recording.

Test Plan is ready. You can modify it now according to your requirements.

Proxy Server setup document from Apache:
http://jakarta.apache.org/jmeter/usermanual/jmeter_proxy_step_by_step.pdf