性能測試持續集成(Jenkins+Ant+Jmeter)

1、環境準備:php

一、JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.htmlhtml

二、Jmeter:http://jmeter.apache.org/download_jmeter.cgijava

三、Ant:http://ant.apache.org/bindownload.cgiweb

四、tomcat:http://tomcat.apache.org/express

五、Jenkins:http://jenkins-ci.org/apache

 

3、Jemter腳本準備:tomcat

 

 


一、腳本內容:訪問baidu首頁oracle

      

二、腳本路徑:app

D:\apache-jmeter-2.13\demoless

 

3、經過ant的 build.xml執行性能腳本(以jmx結尾)生成測試報告

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

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

二、編寫build.xml

<?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" basedir=".">
    <tstamp> 
        <format property="time" pattern="yyyyMMddhhmm" />   
    </tstamp>
    <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:
        -Dshow-data=y - include response data in Failure Details
        -Dtestpath=xyz - path to test file(s) (default user.dir).
                         N.B. Ant interprets relative paths against the build file
        -Djmeter.home=.. - path to JMeter home directory (defaults to parent of this build file)
        -Dreport.title="My Report" - title for html report (default is 'Load Test Results')

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

    </description>
    
    <property name="testpath" value="${user.dir}"/>
    <!-- 須要改爲本身本地的 Jmeter 目錄-->
    <property name="jmeter.home" value="D:\apache-jmeter-2.13" />
    <!-- 須要改爲本身本地的 Jmeter 中demo的目錄-->
    <property name="test.dir" value="D:\apache-jmeter-2.13\demo" />
    <!-- jmeter生成測試報告的title名稱--> 
    <property name="report.title" value="Jmeter Test Results"/>
    <!-- jmeter生成jtl、html格式的結果報告的路徑--> 
    <property name="jmeter.result.jtl.dir" value="D:\apache-jmeter-2.13\demo\report\jtl" />   
    <property name="jmeter.result.html.dir" value="D:\apache-jmeter-2.13\demo\report\html" />  
    <!-- 測試報告名稱--> 
    <property name="ReportName" value="Load Test Report" />       
    <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}.jtl" />   
    <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/index.html" />   
    <target name="all"> 
        <echo message="start..."/>    
        <!--<antcall target="clean" /> -->
        <antcall target="run" />   
        <antcall target="xslt-report" />      
       </target>   
    

    <!-- Name of test (without .jmx) -->
    <property name="test" value="Test"/>
    
    <!-- Should report include response data for failures? -->
    <property name="show-data" value="n"/>

    <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="run">
        <echo>funcMode = ${funcMode}</echo>
        <delete file="${testpath}/${test}.html"/>
        <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />   
        <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">  
 <!-- 執行這個目錄下邊全部以.jmx結尾文件 --> <testplans dir="${test.dir}" includes="*.jmx" /> <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> <target name="xslt-report" depends="_message_xalan"> <xslt classpathref="xslt.classpath" force="true" in="${jmeter.result.jtlName}" out="${jmeter.result.htmlName}" style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"> <param name="showData" expression="${show-data}"/> <param name="titleReport" expression="${ReportName}${time}"/> <param name="dateReport" expression="${ReportName}${time}"/> </xslt> <!-- 由於上面生成報告的時候,不會將相關的圖片也一塊兒拷貝至目標目錄,因此,須要手動拷貝 --> <copy todir="${jmeter.result.html.dir}"> <fileset dir="${jmeter.home}/extras"> <include name="collapse.png" /> <include name="expand.png" /> </fileset> </copy> </target> <target name="verify-images"> <condition property="samepath"> <equals arg1="${testpath}" arg2="${basedir}" /> </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 and serializer jars in ${lib.dir}</echo> </target> </project>

 

三、執行build.xml

   1)cmd進入腳本目錄:D:\apache-jmeter-2.13\demo

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

   

    3)測試報告目錄 D:\apache-jmeter-2.13\demo\report\html

        由於在D:\apache-jmeter-2.13\demo目錄下邊有兩個jmx文件,3個請求,全部結果以下3個url結果

    

 

3、Jenkins中集成ant,添加job在Jenkins中生成html樣式性能測試報告

     一、將Jenkins.war包放在tomcat目錄D:\apache-tomcat-7.0.54\webapps下,而且啓動D:\apache-tomcat-7.0.54\bin目錄下邊startup.bat腳本,以下表示啓動成功:

        

   二、訪問Jenkins ,Jenkins在tomcat中默認啓動端口8080  ,訪問地址: http://localhost:8080/jenkins/

   三、創建job 而且配置ant ,配置好信息進行保存;

        

        填寫build.xml路徑

       

    四、在Jenkins裏邊添加Performance pluginHTML Publisher plugin  、ant  插件

         在job配置 "增長構建後操做步驟"增長Publish HTML reports插件,而且添加報告目錄,進行保存 如圖:

    

         

       五、執行job

       

       六、查看控制檯,看到Finished: SUCCESS說明執行job成功已生成jtl和html報告;

     

       七、查看Jenkins生成html報告

         路徑:http://localhost:8080/jenkins/job/jmeterhttp/HTML_Report/

   

 

  最終報告和用ant的build.xml效果同樣 ,哈哈,大功搞成了;

     

    

     參考地址博客:    http://www.cnblogs.com/puresoul/p/4737716.html

相關文章
相關標籤/搜索