一.ant安裝javascript
環境準備:css
1.安裝jdkhtml
http://www.oracle.com/technetwork/java/javase/downloads/index.htmljava
注意:最好與jmeter綠色版中自帶的jdk版本一致,能夠把jdk改爲jmeter中自帶的jdk(修改環境變量JAVA_HOME)node
2.下載antexpress
http://ant.apache.org/bindownload.cgiapache
配置ant環境變量,這裏不說了oracle
輸入:ant -v 出現如下,表示安裝成功app
二.安裝遇到的問題less
輸入 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>支持多人同時發郵件!!<br /><br /></message> 72 </mail> 73 </sequential> 74 </for> 75 </target--> 76 </project>
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 > 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 > 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報告以下