Jmeter+Ant配置

build.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
   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">
    <description>
		文件描述
    </description>
	<!--生成时间-->
    <tstamp>
		<format property="time" pattern="yyyyMMddhhmmss"/>
	</tstamp>
	<!--${user.dir}当前目录的意思-->
    <property name="basedir" value="${user.dir}"/>
	<!--${user.dir}当前目录的意思,在此指向%JMETER_HOME%目录-->
    <property name="jmeter.home" value="${user.dir}/.."/>
	
	
	
	<!--自我定义脚本-->
	<!--定义Jmeter的脚本存放目录-->
	<property name="script.dir" value="${jmeter.home}/project"/>
	<!--定义Jmeter的结果存放目录-->
	<property name="result" value="${script.dir}/result"/>
	<!--自我定义脚本-->
	
	
	
	
	<!--定义测试报告的title-->
    <property name="report.title" value="标题"/>
    
    <!-- 定义测试结果名称(without .jmx) -->
    <property name="testName" value="${time}"/>
    
	<!-- 测试报告中是否显示失败事物,n为不显示 -->
	<property name="show-data" value="n"/>
	
    <property name="format" value="2.1"/>
    
    <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">
		<!--${basedir}指向当前目录,此build.xml所在目录-->
        <fileset dir="${jmeter.home}/extras">
			<!--把ant-jmeter字符开头的包.jar结尾的包都引入进来,在此*一般是版本信息-->
			<!--在此*一般是版本信息,我们使用的jmeter中是ant-jmeter-1.1.1.jar-->
			<!--ant-jmeter-1.1.1.jar是ant与jmeter连接的桥梁-->
          <include name="ant-jmeter*.jar"/>
        </fileset>
    </path>

    <taskdef
        name="jmeter"
        classpathref="jmeter.classpath"
        classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>
    <!--定义任务,一个是run,既运行脚本,一个是report,既生成报告-->
    <target name="all" depends="run,report"/>
	<!--定义run任务-->
    <target name="run">
	<!--显示上面定义的${show-data}取值-->
        <echo>funcMode = ${funcMode}</echo>
		<!--删除前一次的测试报告,html格式的上面有定义${test}变量的值-->
        <!--<delete file="${jmeter.home}/${test}.html"/>-->
		
		
		<!--
		jmeterproperties:加载运行时设置,设置配置再jmeter.properties文件中
		runremote:是否可以远程执行   runremote="true"
		resultlog:测试结果保存的目录
		testplan ="${testpath}/${test}.jmx"
		-->
        <jmeter
            jmeterhome="${jmeter.home}"
			jmeterproperties="${jmeter.home}/bin/jmeter.properties"

            resultlog="${result}/${time}.jtl">
			
			<!--
			指定测试脚本或者测试脚本所在的文件目录
			如果是单一脚本,直接指定脚本名称,如果是执行多个脚本,指定脚本目录
			下面定义的是一个脚本
			-->
			<testplans dir="${script.dir}" includes="SCRM.jmx"/> 
		
			
        <!--
            <jvmarg value="-Xincgc"/>
            <jvmarg value="-Xmx128m"/>
            <jvmarg value="-Dproperty=value"/>
            <jmeterarg value="-qextra.properties"/>
        -->
            <!-- Force suitable defaults -->
			<!-- 声明ant执行jmeter时,传入jmeter的属性值,可以自定义 -->
			<!-- 超过3个可能会吧报错,换JDK,或者少于3个 -->
            <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>
	<!--定义依赖包-->
    <property name="lib.dir" value="${jmeter.home}/lib"/>

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


	<!--
	定义report任务,此任务依赖于xslt-report,copy-images这两个任务
	xslt-report是使用xslt来吧xml文件转换成功html格式来显示的
	copy-images用来copy图片,xslt模板中的图片由copy-images任务copy到报告目录
	-->
    <target name="report" depends="xslt-report,copy-images">
        <echo>Report generated at ${report.datestamp}</echo>
    </target>
	<!--
	_message_xalan 被 xslt-reportxslt 依赖,由它生成报告
	-->
    <target name="xslt-report" depends="_message_xalan">
        <tstamp><format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/></tstamp>
		<!--
		in:测试结果作为输出,一定要与  $resultlog一致
		style:xslt定义的模板文件地址
		即%JMETER_HOME%/extras/jmeter-results-detail-report$_21.xsl%
		-->
        <xslt
            classpathref="xslt.classpath"
            force="true"
            in="${result}/${time}.jtl"
            out="${result}/${time}.html"
            style="${basedir}/jmeter-results-report${style_version}.xsl">
            <param name="showData" expression="${show-data}"/>
            <param name="titleReport" expression="${report.title}"/>
            <param name="dateReport" expression="${report.datestamp}"/>
        </xslt>
    </target>

    <!-- 生成Html报告是需要copy图标到报告目录,expand.png展开图标,collapse.png收起图标 -->
    <target name="copy-images" depends="verify-images" unless="samepath">
        <copy file="${basedir}/expand.png" tofile="${result}/expand.png"/>
        <copy file="${basedir}/collapse.png" tofile="${result}/collapse.png"/>
    </target>
	<!--判断${jmeter.home}与${basedir}是否存在指向方向同一目录-->
    <target name="verify-images">
        <condition property="samepath">
                <equals arg1="${basedir}" arg2="${jmeter.home}/extras" />
        </condition>
    </target>

    <!-- 检查依赖 -->
    <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 and serializer jars in ${lib.dir}</echo>
    </target>


</project>


jmeter-results-report_21

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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.
-->

<!-- 
	Stylesheet for processing 2.1 output format test result files 
	To uses this directly in a browser, add the following to the JTL file as line 2:
	<? xml-stylesheet type="text/xsl" href="../extras/jmeter-results-report_21.xsl" ?>
	and you can then view the JTL in a browser
-->
	
<xsl:output method="html" indent="yes" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />

<xsl:param name="titleReport" select="'Load Test Results'"/>
<xsl:param name="dateReport" select="'date not defined'"/>

<xsl:template match="testResults">
	<html>
		<head>
			<title><xsl:value-of select="$titleReport" /></title>
			<style type="text/css">
				body {
					font:normal 68% verdana,arial,helvetica;
					color:#000000;
				}
				table tr td, table tr th {
					font-size: 68%;
				}
				table.details tr th{
				    color: #ffffff;
					font-weight: bold;
					text-align:center;
					background:#2674a6;
					white-space: nowrap;
				}
				table.details tr td{
					background:#eeeee0;
					white-space: nowrap;
				}
				h1 {
					margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
				}
				h2 {
					margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
				}
				h3 {
					margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
				}
				.Failure {
					font-weight:bold; color:red;
				}
			</style>
		</head>
		<body>
		
			<xsl:call-template name="pageHeader" />
			
			<xsl:call-template name="summary" />
			<hr size="1" width="95%" align="center" />
			
			<xsl:call-template name="pagelist" />
			<hr size="1" width="95%" align="center" />
			
			<xsl:call-template name="detail" />

		</body>
	</html>
</xsl:template>

<xsl:template name="pageHeader">
	<h1><xsl:value-of select="$titleReport" /></h1>
	<table width="100%">
		<tr>
			<td align="left">Date report: <xsl:value-of select="$dateReport" /></td>
			<td align="right">Designed for use with <a href="http://jmeter.apache.org/">JMeter</a> and <a href="http://ant.apache.org">Ant</a>.</td>
		</tr>
	</table>
	<hr size="1" />
</xsl:template>

<xsl:template name="summary">
	<h2>Summary</h2>
	<table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
		<tr valign="top">
			<th># Samples</th>
			<th>Failures</th>
			<th>Success Rate</th>
			<th>Average Time</th>
			<th>Min Time</th>
			<th>Max Time</th>
			<th>Last Time</th>
			<th>TPS</th>
		</tr>
		<tr valign="top">
			<xsl:variable name="allCount" select="count(/testResults/*)" />
			<xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />
			<xsl:variable name="allSuccessCount" select="count(/testResults/*[attribute::s='true'])" />
			<xsl:variable name="allSuccessPercent" select="$allSuccessCount div $allCount" />
			<xsl:variable name="allTotalTime" select="sum(/testResults/*/@t)" />
			<xsl:variable name="allAverageTime" select="$allTotalTime div $allCount" />
			<xsl:variable name="allMinTime">
				<xsl:call-template name="min">
					<xsl:with-param name="nodes" select="/testResults/*/@t" />
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="allMaxTime">
				<xsl:call-template name="max">
					<xsl:with-param name="nodes" select="/testResults/*/@t" />
				</xsl:call-template>
			</xsl:variable>
			<!-- QPS/TPS -->
			<!-- 场景开始时间 -->
			<xsl:variable name="startTime">
				<xsl:call-template name="sTime">
					<xsl:with-param name="nodes" select="/testResults/*/@ts" />
				</xsl:call-template>
			</xsl:variable>
			<!-- endtime -->
			<xsl:variable name="endTime">
				<xsl:call-template name="eTime">
					<xsl:with-param name="nodes" select="/testResults/*/@ts" />
				</xsl:call-template>
			</xsl:variable>
			<!-- lasettime -->
			<xsl:variable name="lastTime">
				<xsl:for-each select="/testResults/*">
					<xsl:if test="./@ts = $endTime">
						<xsl:value-of select="./@t"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:variable>
			<!--QPS/TPS-->
			<!--
			TPS=执行的请求时间的总数/执行的总时间
			执行的总时间=最后一个请求开始时间 - 第一个请求的开始时间 + 最后一个请求的执行时间 
			-->
			<xsl:variable name="tps" select="$allCount * 1000 div ($endTime + $lastTime - $startTime)"/>
			
			
			<xsl:attribute name="class">
				<xsl:choose>
					<xsl:when test="$allFailureCount &gt; 0">Failure</xsl:when>
				</xsl:choose>
			</xsl:attribute>
			<td align="center">
				<xsl:value-of select="$allCount" />
			</td>
			<td align="center">
				<xsl:value-of select="$allFailureCount" />
			</td>
			<td align="right">
				<xsl:call-template name="display-percent">
					<xsl:with-param name="value" select="$allSuccessPercent" />
				</xsl:call-template>
			</td>
			<td align="right">
				<xsl:call-template name="display-time">
					<xsl:with-param name="value" select="$allAverageTime" />
				</xsl:call-template>
			</td>
			<td align="right">
				<xsl:call-template name="display-time">
					<xsl:with-param name="value" select="$allMinTime" />
				</xsl:call-template>
			</td>
			<td align="right">
				<xsl:call-template name="display-time">
					<xsl:with-param name="value" select="$allMaxTime" />
				</xsl:call-template>
			</td>
			<!-- lastTime -->
			<td align="right">
				<xsl:call-template name="display-time">
					<xsl:with-param name="value" select="$lastTime" />
				</xsl:call-template>
			</td>
			<td align="right">
				<xsl:call-template name="display-time">
					<xsl:with-param name="value" select="$tps" />
				</xsl:call-template>
			</td>
		</tr>
	</table>
</xsl:template>
<xsl:template name="pagelist">
	<h2>Pages</h2>
	<table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
		<tr valign="top">
			<th>URL</th>
			<th># Samples</th>
			<th>Failures</th>
			<th>Success Rate</th>
			<th>Average Time</th>
			<th>Min Time</th>
			<th>Max Time</th>
			<th>QPS/TPS</th>
		</tr>
		<xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]">
			<xsl:variable name="label" select="@lb" />
			<xsl:variable name="count" select="count(../*[@lb = current()/@lb])" />
			<xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" />
			<xsl:variable name="successCount" select="count(../*[@lb = current()/@lb][attribute::s='true'])" />
			<xsl:variable name="successPercent" select="$successCount div $count" />
			<xsl:variable name="totalTime" select="sum(../*[@lb = current()/@lb]/@t)" />
			<xsl:variable name="averageTime" select="$totalTime div $count" />
			<xsl:variable name="minTime">
				<xsl:call-template name="min">
					<xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" />
				</xsl:call-template>
			</xsl:variable>
			<xsl:variable name="maxTime">
				<xsl:call-template name="max">
					<xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" />
				</xsl:call-template>
			</xsl:variable>
			
			<!-- QPS/TPS -->
			<!-- startTime -->
			<xsl:variable name="startTime">
				<xsl:call-template name="sTime">
					<xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@ts" />
				</xsl:call-template>
			</xsl:variable>
			<!-- endTime -->
			<xsl:variable name="endTime">
				<xsl:call-template name="eTime">
					<xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@ts" />
				</xsl:call-template>
			</xsl:variable>
			<!-- lastTime -->
			<xsl:variable name="lastTime">
				<xsl:for-each select="../*[@lb = current()/@lb]">
					<xsl:if test="./@ts = $endTime">
						<xsl:value-of select="./@t"/>
					</xsl:if>
				</xsl:for-each>
			</xsl:variable>
			<!--QPS/TPS-->
			<xsl:variable name="tps" select="$count * 1000 div ($endTime + $lastTime - $startTime)"/>
			
			
			<tr valign="top">
				<xsl:attribute name="class">
					<xsl:choose>
						<xsl:when test="$failureCount &gt; 0">Failure</xsl:when>
					</xsl:choose>
				</xsl:attribute>
				<td>
					<xsl:value-of select="$label" />
				</td>
				<td align="center">
					<xsl:value-of select="$count" />
				</td>
				<td align="center">
					<xsl:value-of select="$failureCount" />
				</td>
				<td align="right">
					<xsl:call-template name="display-percent">
						<xsl:with-param name="value" select="$successPercent" />
					</xsl:call-template>
				</td>
				<td align="right">
					<xsl:call-template name="display-time">
						<xsl:with-param name="value" select="$averageTime" />
					</xsl:call-template>
				</td>
				<td align="right">
					<xsl:call-template name="display-time">
						<xsl:with-param name="value" select="$minTime" />
					</xsl:call-template>
				</td>
				<td align="right">
					<xsl:call-template name="display-time">
						<xsl:with-param name="value" select="$maxTime" />
					</xsl:call-template>
				</td>
				<td align="right">
					<xsl:call-template name="display-time">
						<xsl:with-param name="value" select="$tps" />
					</xsl:call-template>
				</td>
				
			</tr>
		</xsl:for-each>
	</table>
</xsl:template>

<xsl:template name="detail">
	<xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />

	<xsl:if test="$allFailureCount > 0">
		<h2>Failure Detail</h2>

		<xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]">

			<xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" />

			<xsl:if test="$failureCount > 0">
				<h3><xsl:value-of select="@lb" /></h3>

				<table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
				<tr valign="top">
					<th>Response</th>
					<th>Failure Message</th>
				</tr>
			
				<xsl:for-each select="/testResults/*[@lb = current()/@lb][attribute::s='false']">
					<tr>
						<td><xsl:value-of select="@rc | @rs" /> - <xsl:value-of select="@rm" /></td>
						<td><xsl:value-of select="assertionResult/failureMessage" /></td>
					</tr>
				</xsl:for-each>
				
				</table>
			</xsl:if>

		</xsl:for-each>
	</xsl:if>
</xsl:template>

<xsl:template name="min">
	<xsl:param name="nodes" select="/.." />
	<xsl:choose>
		<xsl:when test="not($nodes)">NaN</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="$nodes">
				<xsl:sort data-type="number" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="number(.)" />
				</xsl:if>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="max">
	<xsl:param name="nodes" select="/.." />
	<xsl:choose>
		<xsl:when test="not($nodes)">NaN</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="$nodes">
				<xsl:sort data-type="number" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="number(.)" />
				</xsl:if>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--第一个请求开始时间-->
<xsl:template name="sTime">
	<xsl:param name="nodes" select="/.." />
	<xsl:choose>
		<xsl:when test="not($nodes)">NaN</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="$nodes">
				<xsl:sort data-type="number" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="number(.)" />
				</xsl:if>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>
<!--最后一个请求开始时间-->
<xsl:template name="eTime">
	<xsl:param name="nodes" select="/.." />
	<xsl:choose>
		<xsl:when test="not($nodes)">NaN</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="$nodes">
				<!--倒序排序第一个 descending-->
				<xsl:sort data-type="number" order="descending" />
				<xsl:if test="position() = 1">
					<xsl:value-of select="number(.)" />
				</xsl:if>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>





<xsl:template name="display-percent">
	<xsl:param name="value" />
	<xsl:value-of select="format-number($value,'0.00%')" />
</xsl:template>

<xsl:template name="display-time">
	<xsl:param name="value" />
	<xsl:value-of select="format-number($value,'0 ms')" />
</xsl:template>
<!--新增一个新的display-tps-->
<xsl:template name="display-tps">
	<xsl:param name="value" />
	<xsl:value-of select="format-number($value,'0.0 /sec')" />
</xsl:template>

	
</xsl:stylesheet>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值