ANT批量執行Jmeter指令碼

五柳-先生發表於2016-02-20

一、環境準備:

  1、Jdk1.6或以上:http://www.oracle.com/technetwork/java/javase/downloads/index.html

    命令列輸入:java -version,出現如下提示說明安裝成功

  

  2、ANT下載:http://ant.apache.org/bindownload.cgi

    命令列輸入:ant -v,出現如下提示說明安裝成功

  

  3、Jmeter下載:http://jmeter.apache.org/download_jmeter.cgi

  4、將 jmeter的extras目錄中ant-jmeter-1.1.1.jar包拷貝至ant安裝目錄下的lib目錄中

  5、修改Jmeter的bin目錄下jmeter.properties檔案的配置:jmeter.save.saveservice.output_format=xml  

  

二、Jmeter指令碼:

  1、如何編寫指令碼請參見:http://www.cnblogs.com/puresoul/p/4740436.html

   2、指令碼目錄:D:\apache-jmeter-2.13\demo

  

  3、指令碼內容:

  測試百度.jmx                    Google1.jmx

    

 

三、build.xml程式碼: 

複製程式碼
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <project name="ant-jmeter-test" default="run" basedir=".">
 4     <tstamp>
 5         <format property="time" pattern="yyyyMMddhhmm" />
 6     </tstamp>
 7     <!-- 需要改成自己本地的 Jmeter 目錄-->  
 8     <property name="jmeter.home" value="D:\apache-jmeter-2.13" />
 9     <!-- jmeter生成jtl格式的結果報告的路徑--> 
10     <property name="jmeter.result.jtl.dir" value="D:\apache-jmeter-2.13\demo\report\jtl" />
11     <!-- jmeter生成html格式的結果報告的路徑-->
12     <property name="jmeter.result.html.dir" value="D:\apache-jmeter-2.13\demo\report\html" />
13     <!-- 生成的報告的字首-->  
14     <property name="ReportName" value="TestReport" />
15     <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}${time}.jtl" />
16     <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}${time}.html" />
17     
18     <target name="run">
19         <antcall target="test" />
20         <antcall target="report" />
21     </target>
22     
23     <target name="test">
24         <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
25         <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
26              <!-- 宣告要執行的指令碼。"*.jmx"指包含此目錄下的所有jmeter指令碼-->
27             <testplans dir="D:\apache-jmeter-2.13\demo" includes="*.jmx" />
28 
29        <property name="jmeter.save.saveservice.output_format" value="xml"/>
30 
31         </jmeter>
32     </target>
33         
34     <target name="report">
35         <xslt in="${jmeter.result.jtlName}"
36               out="${jmeter.result.htmlName}"
37               style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl" />
38                 <!-- 因為上面生成報告的時候,不會將相關的圖片也一起拷貝至目標目錄,所以,需要手動拷貝 --> 
39         <copy todir="${jmeter.result.html.dir}">
40             <fileset dir="${jmeter.home}/extras">
41                 <include name="collapse.png" />
42                 <include name="expand.png" />
43             </fileset>
44         </copy>
45     </target>
46 </project>
複製程式碼

 

四、執行指令碼:

 1、cmd進入指令碼目錄:D:\apache-jmeter-2.13\demo

 2、輸入:ant  或 ant run(run為build.xml中的task名),執行結果:

複製程式碼
D:\apache-jmeter-2.13\demo>ant
Buildfile: build.xml

all:

test:
   [jmeter] Executing test plan: D:\apache-jmeter-2.13\demo\Google1.jmx ==> D:\apache-jmeter-2.13\demo\report\jtl\TestReport201509141114.jtl
   [jmeter] Creating summariser <summary>
   [jmeter] Created the tree successfully using D:\apache-jmeter-2.13\demo\Google1.jmx
   [jmeter] Starting the test @ Mon Sep 14 23:14:32 CST 2015 (1442243672984)
   [jmeter] Waiting for possible shutdown message on port 4445
   [jmeter] summary +      1 in     1s =    1.9/s Avg:   248 Min:   248 Max:   248 Err:     1 (100.00%) Active: 1 Started: 1 Finished: 0
   [jmeter] summary +      5 in     1s =    7.8/s Avg:   119 Min:   107 Max:   137 Err:     0 (0.00%) Active: 0 Started: 1 Finished: 1
   [jmeter] summary =      6 in   1.2s =    5.2/s Avg:   140 Min:   107 Max:   248 Err:     1 (16.67%)
   [jmeter] Tidying up ...    @ Mon Sep 14 23:14:34 CST 2015 (1442243674232)
   [jmeter] ... end of run
   [jmeter] Executing test plan: D:\apache-jmeter-2.13\demo\測試百度.jmx ==> D:\apache-jmeter-2.13\demo\report\jtl\TestReport201509141114.jtl
   [jmeter] Creating summariser <summary>
   [jmeter] Created the tree successfully using D:\apache-jmeter-2.13\demo\測試百度.jmx
   [jmeter] Starting the test @ Mon Sep 14 23:14:35 CST 2015 (1442243675376)
   [jmeter] Waiting for possible shutdown message on port 4445
   [jmeter] summary +      1 in   0.5s =    2.1/s Avg:   196 Min:   196 Max:   196 Err:     0 (0.00%) Active: 1 Started: 1 Finished: 0
   [jmeter] summary +      5 in     1s =    8.4/s Avg:   113 Min:   107 Max:   133 Err:     0 (0.00%) Active: 0 Started: 1 Finished: 1
   [jmeter] summary =      6 in   1.1s =    5.6/s Avg:   126 Min:   107 Max:   196 Err:     0 (0.00%)
   [jmeter] Tidying up ...    @ Mon Sep 14 23:14:36 CST 2015 (1442243676535)
   [jmeter] ... end of run

report:
     [xslt] Processing D:\apache-jmeter-2.13\demo\report\jtl\TestReport201509141114.jtl to D:\apache-jmeter-2.13\demo\report\html\TestReport201509141114.html
     [xslt] Loading stylesheet D:\apache-jmeter-2.13\extras\jmeter-results-detail-report_21.xsl
     [copy] Copying 2 files to D:\apache-jmeter-2.13\demo\report\html

BUILD SUCCESSFUL
Total time: 5 seconds
複製程式碼

 3、測試報告目錄:D:\apache-jmeter-2.13\demo\report\html,其中有一個紅色失敗的案例是我故意設定失敗,以便檢視的。 

 

 

四:最後,說一個我在使用過程中碰到一個問題,上面build.xml檔案第29行有一句必須要加上:<property name="jmeter.save.saveservice.output_format" value="xml"/>,否則會報如下的錯誤:   

report:
     [xslt] Processing D:\Tools\apache-jmeter-2.13\demo\report\jtl\TestReport201509210923.jtl to D:\Tools\apache-jmeter-2.13\demo\report\html\TestReport201509210923.html
     [xslt] Loading stylesheet D:\Tools\apache-jmeter-2.13\extras\jmeter-results-detail-report_21.xsl
     [xslt] : Error! 前言中不允許有內容。
     [xslt] : Error! com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: 前言中不允許有內容。
     [xslt] Failed to process D:\Tools\apache-jmeter-2.13\demo\report\jtl\TestReport201509210923.jtl

 因為不加上這一句,生成的.jtl檔案是文字檔案不是xml檔案,使用xsl去轉換.jtl檔案時就會報錯。

 

更新補充:

  上面生成的報告有如下兩個問題,解決辦法參見Jmeter自帶報告優化(一)

  1、Date report這裡的時間沒有正確顯示出來

  2、Summary裡的欄位Min Time和Max Time顯示的是NaN,沒有顯示正確的時間。

 

 

 

 

 

 

參考:

1、http://rainbow702.iteye.com/blog/2159244

2、http://www.programmerplanet.org/projects/jmeter-ant-task/

 


作者:Glen.He 
出處:http://www.cnblogs.com/puresoul/ 
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。

相關文章