Wednesday, December 09, 2009

jmeter ant script

this ant script run jmeter and also generates reports

this ant script takes in two folder locations, and many test scripts and a time to run from each folder.

this is used to run one set of the test then change folder and run another set of the same tests with different throughput numbers



<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project name="ant-jmeter" default="all">

<property file="${Properties}">

<description>

Sample build file for use with ant-jmeter.jar
See http://www.programmerplanet.org/pages/projects/jmeter-ant-task.php

To run a test and create the output report:
ant -Dtest=script

To run a test only:
ant -Dtest=script run

To run report on existing test output
ant -Dtest=script report

The "script" parameter is the name of the script without the .jmx suffix.

Additional options:

-Dtest2=script2
-Dtest3=script3
-Dtest4=script4
-Dtest5=script5
-Dtest6=script6

-DshutdownPort=4445
-DshutdownPort2=4446
-DshutdownPort3=4447
-DshutdownPort4=4448
-DshutdownPort5=4449
-DshutdownPort6=4450

-Dtestpath=xyz - path to test file(s) (default user.dir).
N.B. Ant interprets relative paths against the build file
-Dtestpath2=xyz - path to test file(s) second home eg 125 phl
N.B. Ant interprets relative paths against the build file

-DtestTime=y - time to run each test set

-DtestTimeOut=x - time for timeout to stop the test set in milliseconds eg, 30min = 30 * 60 * 1000

-Djmeter.home=.. - path to JMeter home directory (defaults to parent of this build file)
-Dshow-data=y - include response data in Failure Details

Deprecated:
-Dformat=2.0 - use version 2.0 JTL files rather than 2.1

</description>

<!-- this is the test time for the test -->
<property name="testTime" value="1">
<!-- timeout is in milliseconds, and must be 4mins more than test time eg, 30min = 30 * 60 * 100-->
<property name="testTimeOut" value="300000">

<property name="testpath" value="${user.dir}">
<property name="jmeter.home" value="${basedir}/..">

<!-- Name of test (without .jmx) -->
<property name="test" value="Test">

<property name="shutdownPort" value="4445">
<property name="shutdownPort2" value="4446">
<property name="shutdownPort3" value="4447">
<property name="shutdownPort4" value="4448">
<property name="shutdownPort5" value="4449">
<property name="shutdownPort6" value="4450">

<!-- Should report include response data for failures? -->
<property name="show-data" value="n">

<property name="output" value="out">

<property name="format" value="2.1">

<condition property="style_version" value="">
<equals arg1="${format}" arg2="2.0">
</condition>

<condition property="style_version" value="_21">
<equals arg1="${format}" arg2="2.1">
</condition>

<condition property="funcMode">
<equals arg1="${show-data}" arg2="y">
</condition>

<condition property="funcMode" value="false">
<not>
<equals arg1="${show-data}" arg2="y">
</not>
</condition>

<!-- Allow jar to be picked up locally -->
<path id="jmeter.classpath">
<fileset dir="${basedir}">
<include name="ant-jmeter*.jar">
</fileset>
</path>

<taskdef
name="jmeter"
classpathref="jmeter.classpath"
classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>

<target name="all" depends="run,report">

<target name="run">

<antcall target="jmeterParallel">
<param name="jmParallelPath" value="${testpath}">
<param name="jmParallelRunTime" value="${testTime}">
<param name="jmParallelTimeout" value="${testTimeOut}">
</antcall>
<antcall target="jmeterParallel">
<param name="jmParallelPath" value="${testpath2}">
<param name="jmParallelRunTime" value="${testTime}">
<param name="jmParallelTimeout" value="${testTimeOut}">
</antcall>
</target>

<target name="jmeterParallel">
<parallel failonany="false" timeout="${jmParallelTimeout}">
<antcall target="jmeterStart">
<param name="jmTest" value="${test}">
<param name="jmPort" value="${shutdownPort}">
<param name="jmPath" value="${jmParallelPath}">
</antcall>
<antcall target="jmeterStart">
<param name="jmTest" value="${test2}">
<param name="jmPort" value="${shutdownPort2}">
<param name="jmPath" value="${jmParallelPath}">
</antcall>
<antcall target="jmeterStart">
<param name="jmTest" value="${test3}">
<param name="jmPort" value="${shutdownPort3}">
<param name="jmPath" value="${jmParallelPath}">
</antcall>
<antcall target="jmeterStart">
<param name="jmTest" value="${test4}">
<param name="jmPort" value="${shutdownPort4}">
<param name="jmPath" value="${jmParallelPath}">
</antcall>
<antcall target="jmeterStart">
<param name="jmTest" value="${test5}">
<param name="jmPort" value="${shutdownPort5}">
<param name="jmPath" value="${jmParallelPath}">
</antcall>
<antcall target="jmeterStart">
<param name="jmTest" value="${test6}">
<param name="jmPort" value="${shutdownPort6}">
<param name="jmPath" value="${jmParallelPath}">
</antcall>
<sequential>
<!-- sleep till we want to shut down -->
<sleep minutes="${jmParallelRunTime}">
<antcall target="shutdownJmeter">
</sequential>
</parallel>

</target>

<target name="jmeterStart" depends="jmeterStart.check" if="jmeterStart.run">
<echo>funcMode = ${funcMode}</echo>
<delete file="${output}/${jmPath}/${jmTest}.html">
<jmeter
jmeterhome="${jmeter.home}"
testplan ="${jmPath}/${jmTest}.jmx"
resultlog="${output}/${jmPath}/${jmTest}.jtl">
<jvmarg value="-Xmx256m">
<!--
<jvmarg value="-Xincgc">
<jvmarg value="-Xmx128m">
<jvmarg value="-Dproperty=value">
<jmeterarg value="-qextra.properties">
-->
<!-- Force suitable defaults -->
<property name="jmeterengine.nongui.port" value="${jmPort}">
<property name="jmeter.save.saveservice.output_format" value="xml">
<property name="jmeter.save.saveservice.assertion_results" value="all">
<property name="jmeter.save.saveservice.bytes" value="true">
<property name="file_format.testlog" value="${format}">
<property name="jmeter.save.saveservice.response_data.on_error" value="${funcMode}">
</jmeter>
</target>

<target name="jmeterStart.check">
<condition property="jmeterStart.run">
<and>
<not>
<matches pattern ="^\$\{.+" string="${jmPath}">
</not>
<not>
<matches pattern ="^\$\{.+" string="${jmTest}">
</not>
</and>
</condition>
</target>

<target name="shutdownJmeter">
<antcall target="jmeterStop">
<param name="jmPort" value = "${shutdownPort}">
</antcall>

<antcall target="jmeterStop">
<param name="jmPort" value= "${shutdownPort2}">
</antcall>
<antcall target="jmeterStop">
<param name="jmPort" value= "${shutdownPort3}">
</antcall>
<antcall target="jmeterStop">
<param name="jmPort" value= "${shutdownPort4}">
</antcall>
<antcall target="jmeterStop">
<param name="jmPort" value= "${shutdownPort5}">
</antcall>
<antcall target="jmeterStop">
<param name="jmPort" value= "${shutdownPort6}">
</antcall>
</target>

<target name="jmeterStop">
<java classname="org.apache.jmeter.util.ShutdownClient" timeout="100">
<arg value="StopTestNow">
<arg value="${jmPort}">
<classpath>
<pathelement location="${jmeter.home}/bin/ApacheJMeter.jar">
</classpath>
</java>
<java classname="org.apache.jmeter.util.ShutdownClient" timeout="100">
<arg value="shutdown">
<arg value="${jmPort}">
<classpath>
<pathelement location="${jmeter.home}/bin/ApacheJMeter.jar">
</classpath>
</java>
</target>


<property name="lib.dir" value="${jmeter.home}/lib">
<property name="xalan.jar" value="${lib.dir}/xalan_2_7_1.jar">
<property name="serializer.jar" value="${lib.dir}/serializer-2_9_1.jar">

<!-- Use xalan copy from JMeter lib directory to ensure consistent processing with Java 1.4+ -->
<path id="xslt.classpath">
<pathelement location="${xalan.jar}">
<pathelement location="${serializer.jar}">
</path>

<target name="report" depends="_message_xalan">
<parallel>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath}">
<param name="jmTest" value = "${test}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath}">
<param name="jmTest" value = "${test2}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath}">
<param name="jmTest" value = "${test3}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath}">
<param name="jmTest" value = "${test4}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath}">
<param name="jmTest" value = "${test5}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath}">
<param name="jmTest" value = "${test6}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath2}">
<param name="jmTest" value = "${test}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath2}">
<param name="jmTest" value = "${test2}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath2}">
<param name="jmTest" value = "${test3}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath2}">
<param name="jmTest" value = "${test4}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath2}">
<param name="jmTest" value = "${test5}">
</antcall>
<antcall target="reportGenerate">
<param name="jmPath" value = "${testpath2}">
<param name="jmTest" value = "${test6}">
</antcall>
</parallel>
</target>


<target name="reportGenerate" depends="reportGenerate.check" if="reportGenerate.run">
<xslt
classpathref="xslt.classpath"
force="true"
in="${output}/${jmPath}/${jmTest}.jtl"
out="${output}/${jmPath}/${jmTest}.html"
style="${jmeter.home}/extras/jmeter-results-detail-report${style_version}.xsl">
<param name="showData" expression="${show-data}">
</xslt>
&lt;/target>

<target name="reportGenerate.check">
<condition property="reportGenerate.run">
<and>
<not>
<matches pattern ="^\$\{.+" string="${jmPath}">
</not>
<not>
<matches pattern ="^\$\{.+" string="${jmTest}">
</not>
</and>
</condition>
</target>


<!-- Check that the xalan libraries are present -->
<condition property="xalan.present">
<and>
<!-- No need to check all jars; just check a few -->
<available classpathref="xslt.classpath" classname="org.apache.xalan.processor.TransformerFactoryImpl">
<available classpathref="xslt.classpath" classname="org.apache.xml.serializer.ExtendedContentHandler">
</and>
</condition>

<target name="_message_xalan" unless="xalan.present">
<echo>Cannot find all xalan and/or serialiser jars</echo>
<echo>The XSLT formatting may not work correctly.</echo>
<echo>Check you have ${xalan.jar} and ${serializer.jar}</echo>
</target>


</project>

No comments: