ANT+JMETER集成

一.ant安装

环境准备:

1.安装jdk

http://www.oracle.com/technetwork/java/javase/downloads/index.html

注意:最好与jmeter绿色版中自带的jdk版本一致,可以把jdk改成jmeter中自带的jdk(修改环境变量JAVA_HOME)

2.下载ant

http://ant.apache.org/bindownload.cgi

配置ant环境变量,这里不说了

 

输入:ant -v  出现以下,表示安装成功

二.安装遇到的问题

输入 ant -v 后一直报错,如下

Exception in thread “main" java.lang.UnsupportedClassVersionError

后来发现是ant版本太新了,本机不支持1.10.1版本,换了一个1.9.7的版本就好了

三.ant+jmeter实现批量执行jmter脚本

准备:

1.jmeter脚本准备(不说了)

2.新建一个demo,把jmx脚本放进去

3.在demo下新建目录resultLog,log下新建两个子目录html,jtl

4.将 jmeter的extras目录中ant-jmeter-1.1.1.jar包拷贝至ant安装目录下的lib目录中

5.修改Jmeter的bin目录下jmeter.properties文件的配置:jmeter.save.saveservice.output_format=xml 

html:存放生成的html报告

jtl:存放生成的jtl报告

 

build.xml配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project name="ant-jmeter-test" default="run" basedir=".">
 3  <taskdef resource="net/sf/antcontrib/antlib.xml" />
 4  <!-- (1)需要改成自己本地的 Jmeter 目录-->
 5 <property name="jmeter.home" value="C:\Users\Administrator\Desktop\apache-jmeter-3.0"/>
 6 <property name="report.title" value="report"/>
 7 <!-- (2)jmeter生成jtl格式的结果报告的路径-->
 8 <property name="jmeter.result.jtl.dir" value="C:\Users\Administrator\Desktop\apache-jmeter-3.0\demo\resultLog\jtl"/>
 9 <!-- (3)jmeter生成html格式的结果报告的路径-->
10 <property name="jmeter.result.html.dir" value="C:\Users\Administrator\Desktop\apache-jmeter-3.0\demo\resultLog\html"/>
11 <!-- 生成的报告的前缀 -->
12 <property name="ReportName" value="TestReport"/>
13 <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}.jtl"/>
14 <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}.html"/>
15 
16 <target name="run"> 
17   <antcall target="test"/>  
18   <antcall target="report"/> 
19   <!--antcall  target="mail"/-->    
20 </target>
21 <target name="test"> 
22   <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>  
23   <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}"> 
24   <!-- (4)build.xml的存放的路径-->
25     <testplans dir="C:\Users\Administrator\Desktop\apache-jmeter-3.0\demo" includes="*.jmx"/>  
26     <property name="jmeter.save.saveservice.output_format" value="xml"/> 
27   </jmeter> 
28 </target>
29 <path id="xslt.classpath"> 
30   <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>  
31   <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/> 
32 </path>
33 <target name="report"> 
34   <tstamp> 
35     <format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/>
36   </tstamp>  
37   <xslt 
38   classpathref="xslt.classpath" 
39   force="true" 
40   in="${jmeter.result.jtlName}"
41   out="${jmeter.result.htmlName}"
42   style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"> 
43     <param name="dateReport" expression="${report.datestamp}"/> 
44   </xslt>  
45 
46   <copy todir="${jmeter.result.html.dir}"> 
47     <fileset dir="${jmeter.home}/extras"> 
48       <include name="collapse.png"/>  
49       <include name="expand.png"/> 
50     </fileset> 
51   </copy> 
52 </target>
53  <path id="lib_classpath">
54         <fileset dir="${basedir}/">
55             <include name="mail*.jar" />
56             <include name="activation*.jar" />
57             <include name="commons-email*.jar" />
58             <include name="ant-contrib*.jar" />
59         </fileset>
60  </path>
61     <!--target name="mail">
62         <for list="hanxm@thunisoft.com" param="tmp" >
63             <sequential>
64               <echo>message @{tmp}</echo> 
65                 <mail mailhost="smtp.thunisoft.com" mailport="25" ssl="false" subject="ant mail" messagemimetype="text/html" user="zhangjn" password="*******"  tolist="@{tmp}">
66                     <from address="zhangjn@thunisoft.com" />
67                     <fileset dir="${jmeter.result.html.dir}">
68                        <include name="*.html" />
69                        <include name="*.png" />
70                       </fileset>
71                     <message>支持多人同时发邮件!!&lt;br /&gt;&lt;br /&gt;</message>
72                 </mail>
73             </sequential>
74         </for>
75     </target-->
76 </project>
build.xml

1.修改build.xml中的(1)(2)(3)(4)(5)的路径为自己的响应的路径

2.将build.xml中的第42行

style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl">

拷贝以下内容将这个路径下的jmeter-results-detail-report_21.xsl文件覆盖,这个文件说明了xsl文件装换为html文件的一些规则

  1 <?xml version="1.0"?>
  2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  3 
  4 <!--
  5    Licensed to the Apache Software Foundation (ASF) under one or more
  6    contributor license agreements.  See the NOTICE file distributed with
  7    this work for additional information regarding copyright ownership.
  8    The ASF licenses this file to You under the Apache License, Version 2.0
  9    (the "License"); you may not use this file except in compliance with
 10    the License.  You may obtain a copy of the License at
 11  
 12        http://www.apache.org/licenses/LICENSE-2.0
 13  
 14    Unless required by applicable law or agreed to in writing, software
 15    distributed under the License is distributed on an "AS IS" BASIS,
 16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 17    See the License for the specific language governing permissions and
 18    limitations under the License.
 19 -->
 20 
 21 <!-- 
 22     Stylesheet for processing 2.1 output format test result files 
 23     To uses this directly in a browser, add the following to the JTL file as line 2:
 24     <?xml-stylesheet type="text/xsl" href="../extras/jmeter-results-detail-report_21.xsl"?>
 25     and you can then view the JTL in a browser
 26 -->
 27 
 28 <xsl:output method="html" indent="yes" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />
 29 
 30 <!-- Defined parameters (overrideable) -->
 31 <xsl:param    name="showData" select="'n'"/>
 32 <xsl:param    name="titleReport" select="'Load Test Results'"/>
 33 <xsl:param    name="dateReport" select="'date not defined'"/>
 34 
 35 <xsl:template match="testResults">
 36     <html>
 37         <head>
 38             <title><xsl:value-of select="$titleReport" /></title>
 39             <style type="text/css">
 40                 body {
 41                     font:normal 68% verdana,arial,helvetica;
 42                     color:#000000;
 43                 }
 44                 table tr td, table tr th {
 45                     font-size: 68%;
 46                 }
 47                 table.details tr th{
 48                     color: #ffffff;
 49                     font-weight: bold;
 50                     text-align:center;
 51                     background:#2674a6;
 52                     white-space: nowrap;
 53                 }
 54                 table.details tr td{
 55                     background:#eeeee0;
 56                     white-space: nowrap;
 57                 }
 58                 h1 {
 59                     margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
 60                 }
 61                 h2 {
 62                     margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
 63                 }
 64                 h3 {
 65                     margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
 66                 }
 67                 .Failure {
 68                     font-weight:bold; color:red;
 69                 }
 70                 
 71     
 72                 img
 73                 {
 74                   border-width: 0px;
 75                 }
 76                 
 77                 .expand_link
 78                 {
 79                    position=absolute;
 80                    right: 0px;
 81                    width: 27px;
 82                    top: 1px;
 83                    height: 27px;
 84                 }
 85                 
 86                 .page_details
 87                 {
 88                    display: none;
 89                 }
 90                                 
 91                                 .page_details_expanded
 92                                 {
 93                                     display: block;
 94                                     display/* hide this definition from  IE5/6 */: table-row;
 95                                 }
 96 
 97 
 98             </style>
 99             <script language="JavaScript"><![CDATA[
100                            function expand(details_id)
101                {
102                   
103                   document.getElementById(details_id).className = "page_details_expanded";
104                }
105                
106                function collapse(details_id)
107                {
108                   
109                   document.getElementById(details_id).className = "page_details";
110                }
111                
112                function change(details_id)
113                {
114                   if(document.getElementById(details_id+"_image").src.match("expand"))
115                   {
116                      document.getElementById(details_id+"_image").src = "collapse.png";
117                      expand(details_id);
118                   }
119                   else
120                   {
121                      document.getElementById(details_id+"_image").src = "expand.png";
122                      collapse(details_id);
123                   } 
124                            }
125             ]]></script>
126         </head>
127         <body>
128         
129             <xsl:call-template name="pageHeader" />
130             
131             <xsl:call-template name="summary" />
132             <hr size="1" width="95%" align="center" />
133             
134             <xsl:call-template name="pagelist" />
135             <hr size="1" width="95%" align="center" />
136             
137             <xsl:call-template name="detail" />
138 
139         </body>
140     </html>
141 </xsl:template>
142 
143 <xsl:template name="pageHeader">
144     <h1><xsl:value-of select="$titleReport" /></h1>
145     <table width="100%">
146         <tr>
147             <td align="left">Date report: <xsl:value-of select="$dateReport" /></td>
148             <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>
149         </tr>
150     </table>
151     <hr size="1" />
152 </xsl:template>
153 
154 <xsl:template name="summary">
155     <h2>Summary</h2>
156     <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
157         <tr valign="top">
158             <th># Samples</th>
159             <th>Failures</th>
160             <th>Success Rate</th>
161             <th>Average Time</th>
162             <th>Min Time</th>
163             <th>Max Time</th>
164         </tr>
165         <tr valign="top">
166             <xsl:variable name="allCount" select="count(/testResults/*)" />
167             <xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />
168             <xsl:variable name="allSuccessCount" select="count(/testResults/*[attribute::s='true'])" />
169             <xsl:variable name="allSuccessPercent" select="$allSuccessCount div $allCount" />
170             <xsl:variable name="allTotalTime" select="sum(/testResults/*/@t)" />
171             <xsl:variable name="allAverageTime" select="$allTotalTime div $allCount" />
172             <xsl:variable name="allMinTime">
173                 <xsl:call-template name="min">
174                     <xsl:with-param name="nodes" select="/testResults/*/@t" />
175                 </xsl:call-template>
176             </xsl:variable>
177             <xsl:variable name="allMaxTime">
178                 <xsl:call-template name="max">
179                     <xsl:with-param name="nodes" select="/testResults/*/@t" />
180                 </xsl:call-template>
181             </xsl:variable>
182             <xsl:attribute name="class">
183                 <xsl:choose>
184                     <xsl:when test="$allFailureCount &gt; 0">Failure</xsl:when>
185                 </xsl:choose>
186             </xsl:attribute>
187             <td align="center">
188                 <xsl:value-of select="$allCount" />
189             </td>
190             <td align="center">
191                 <xsl:value-of select="$allFailureCount" />
192             </td>
193             <td align="center">
194                 <xsl:call-template name="display-percent">
195                     <xsl:with-param name="value" select="$allSuccessPercent" />
196                 </xsl:call-template>
197             </td>
198             <td align="center">
199                 <xsl:call-template name="display-time">
200                     <xsl:with-param name="value" select="$allAverageTime" />
201                 </xsl:call-template>
202             </td>
203             <td align="center">
204                 <xsl:call-template name="display-time">
205                     <xsl:with-param name="value" select="$allMinTime" />
206                 </xsl:call-template>
207             </td>
208             <td align="center">
209                 <xsl:call-template name="display-time">
210                     <xsl:with-param name="value" select="$allMaxTime" />
211                 </xsl:call-template>
212             </td>
213         </tr>
214     </table>
215 </xsl:template>
216 
217 <xsl:template name="pagelist">
218     <h2>Pages</h2>
219     <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
220         <tr valign="top">
221             <th>URL</th>
222             <th># Samples</th>
223             <th>Failures</th>
224             <th>Success Rate</th>
225             <th>Average Time</th>
226             <th>Min Time</th>
227             <th>Max Time</th>
228             <th></th>
229         </tr>
230         <xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]">
231             <xsl:variable name="label" select="@lb" />
232             <xsl:variable name="count" select="count(../*[@lb = current()/@lb])" />
233             <xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" />
234             <xsl:variable name="successCount" select="count(../*[@lb = current()/@lb][attribute::s='true'])" />
235             <xsl:variable name="successPercent" select="$successCount div $count" />
236             <xsl:variable name="totalTime" select="sum(../*[@lb = current()/@lb]/@t)" />
237             <xsl:variable name="averageTime" select="$totalTime div $count" />
238             <xsl:variable name="minTime">
239                 <xsl:call-template name="min">
240                     <xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" />
241                 </xsl:call-template>
242             </xsl:variable>
243             <xsl:variable name="maxTime">
244                 <xsl:call-template name="max">
245                     <xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" />
246                 </xsl:call-template>
247             </xsl:variable>
248             <tr valign="top">
249                 <xsl:attribute name="class">
250                     <xsl:choose>
251                         <xsl:when test="$failureCount &gt; 0">Failure</xsl:when>
252                     </xsl:choose>
253                 </xsl:attribute>
254                 <td>
255                 <xsl:if test="$failureCount > 0">
256                   <a><xsl:attribute name="href">#<xsl:value-of select="$label" /></xsl:attribute>
257                   <xsl:value-of select="$label" />
258                   </a>
259                 </xsl:if>
260                 <xsl:if test="0 >= $failureCount">
261                   <xsl:value-of select="$label" />
262                 </xsl:if>
263                 </td>
264                 <td align="center">
265                     <xsl:value-of select="$count" />
266                 </td>
267                 <td align="center">
268                     <xsl:value-of select="$failureCount" />
269                 </td>
270                 <td align="right">
271                     <xsl:call-template name="display-percent">
272                         <xsl:with-param name="value" select="$successPercent" />
273                     </xsl:call-template>
274                 </td>
275                 <td align="right">
276                     <xsl:call-template name="display-time">
277                         <xsl:with-param name="value" select="$averageTime" />
278                     </xsl:call-template>
279                 </td>
280                 <td align="right">
281                     <xsl:call-template name="display-time">
282                         <xsl:with-param name="value" select="$minTime" />
283                     </xsl:call-template>
284                 </td>
285                 <td align="right">
286                     <xsl:call-template name="display-time">
287                         <xsl:with-param name="value" select="$maxTime" />
288                     </xsl:call-template>
289                 </td>
290                 <td align="center">
291                    <a href="">
292                       <xsl:attribute name="href"><xsl:text/>javascript:change('page_details_<xsl:value-of select="position()" />')</xsl:attribute>
293                       <img src="expand.png" alt="expand/collapse"><xsl:attribute name="id"><xsl:text/>page_details_<xsl:value-of select="position()" />_image</xsl:attribute></img>                      
294                    </a>
295                 </td>
296             </tr>
297             
298                         <tr class="page_details">
299                            <xsl:attribute name="id"><xsl:text/>page_details_<xsl:value-of select="position()" /></xsl:attribute>
300                            <td colspan="8" bgcolor="#FF0000">
301                               <div align="center">
302                      <b>Details for Page "<xsl:value-of select="$label" />"</b>
303                      <table bordercolor="#000000" bgcolor="#2674A6" border="0"  cellpadding="1" cellspacing="1" width="95%">
304                      <tr>
305                         <th>Thread</th>
306                         <th>Iteration</th>
307                         <th>Time (milliseconds)</th>
308                         <th>Bytes</th>
309                         <th>Success</th>
310                      </tr>
311                                       
312                      <xsl:for-each select="../*[@lb = $label and @tn != $label]">                                             
313                         <tr>
314                            <td><xsl:value-of select="@tn" /></td>
315                            <td align="center"><xsl:value-of select="position()" /></td>
316                            <td align="right"><xsl:value-of select="@t" /></td>
317                            <!--  TODO allow for missing bytes field -->
318                            <td align="right"><xsl:value-of select="@by" /></td>
319                            <td align="center"><xsl:value-of select="@s" /></td>
320                         </tr>
321                      </xsl:for-each>
322                      
323                      </table>
324                   </div>
325                            </td>
326                         </tr>
327             
328         </xsl:for-each>
329     </table>
330 </xsl:template>
331 
332 <xsl:template name="detail">
333     <xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />
334 
335     <xsl:if test="$allFailureCount > 0">
336         <h2>Failure Detail</h2>
337 
338         <xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]">
339 
340             <xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" />
341 
342             <xsl:if test="$failureCount > 0">
343                 <h3><xsl:value-of select="@lb" /><a><xsl:attribute name="name"><xsl:value-of select="@lb" /></xsl:attribute></a></h3>
344 
345                 <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
346                 <tr valign="top">
347                     <th>Response</th>
348                     <th>Failure Message</th>
349                     <xsl:if test="$showData = 'y'">
350                        <th>Response Data</th>
351                     </xsl:if>
352                 </tr>
353             
354                 <xsl:for-each select="/testResults/*[@lb = current()/@lb][attribute::s='false']">
355                     <tr>
356                         <td><xsl:value-of select="@rc | @rs" /> - <xsl:value-of select="@rm" /></td>
357                         <td><xsl:value-of select="assertionResult/failureMessage" /></td>
358                         <xsl:if test="$showData = 'y'">
359                             <td><xsl:value-of select="./binary" /></td>
360                         </xsl:if>
361                     </tr>
362                 </xsl:for-each>
363                 
364                 </table>
365             </xsl:if>
366 
367         </xsl:for-each>
368     </xsl:if>
369 </xsl:template>
370 
371 <xsl:template name="min">
372     <xsl:param name="nodes" select="/.." />
373     <xsl:choose>
374         <xsl:when test="not($nodes)">NaN</xsl:when>
375         <xsl:otherwise>
376             <xsl:for-each select="$nodes">
377                 <xsl:sort data-type="number" />
378                 <xsl:if test="position() = 1">
379                     <xsl:value-of select="number(.)" />
380                 </xsl:if>
381             </xsl:for-each>
382         </xsl:otherwise>
383     </xsl:choose>
384 </xsl:template>
385 
386 <xsl:template name="max">
387     <xsl:param name="nodes" select="/.." />
388     <xsl:choose>
389         <xsl:when test="not($nodes)">NaN</xsl:when>
390         <xsl:otherwise>
391             <xsl:for-each select="$nodes">
392                 <xsl:sort data-type="number" order="descending" />
393                 <xsl:if test="position() = 1">
394                     <xsl:value-of select="number(.)" />
395                 </xsl:if>
396             </xsl:for-each>
397         </xsl:otherwise>
398     </xsl:choose>
399 </xsl:template>
400 
401 <xsl:template name="display-percent">
402     <xsl:param name="value" />
403     <xsl:value-of select="format-number($value,'0.00%')" />
404 </xsl:template>
405 
406 <xsl:template name="display-time">
407     <xsl:param name="value" />
408     <xsl:value-of select="format-number($value,'0 ms')" />
409 </xsl:template>
410     
411 </xsl:stylesheet>

执行:

切换到build.xml路径,执行ant命令,当出现如下字样表示执行成功

查看报告:

1.在\demo\resultLog\html路径下查看html报告如下

 

posted @ 2017-04-19 18:59  菜鸟虫师  阅读(11549)  评论(1编辑  收藏  举报