ant+jmeter+jenkins+git持續集成以及郵件報告展現

前序準備工做:php

  1. ant--下載地址:http://ant.apache.org/bindownload.cgi
  2. jmeter--下載地址:http://jmeter.apache.org/download_jmeter.cgi
  3. jenkins--下載地址:https://jenkins.io/
  4. git--下載地址:https://git-scm.com/downloads
  5. jdk--http://www.oracle.com/technetwork/java/javase/downloads/index.html

安裝工具:css

  經常使用的軟件安裝,環境變量配置,略……不會的能夠百度html

ant配置:java

  1. 將jmeter安裝目錄或者源碼目錄下\apache-jmeter-3.2\extras的ant-jmeter-1.1.1.jar複製到ant安裝目錄下apache-ant-1.10.1\lib
  2. 新建build.xml文件,按照下面的模版進行適當的修改。

 

<?xml version="1.0" encoding="UTF-8"?>

<project name="ant-jmeter-test" default="run" basedir=".">
    <tstamp>
        <format property="time" pattern="yyyyMMddhhmm" />
    </tstamp>

    <property environment="env"/>
    <property name="ReportName" value="TestReport" />
    <!-- 須要改爲本身本地的 Jmeter 目錄--> 
    <property name="jmeter.home" value="D:\work\soft\apache-jmeter-3.2" />
    <!-- jmeter生成jtl、html格式的結果報告的路徑--> 
    <property name="jmeter.result.dir" value="${env.WORKSPACE}/results/${env.BUILD_ID}" />
    <!-- 生成的報告的前綴-->  
    <property name="jmeter.result.jtlName" value="${jmeter.result.dir}/${ReportName}.jtl" />
    <property name="jmeter.result.htmlName" value="${jmeter.result.dir}/${ReportName}.html" />
    <!-- GIT拉取到本地目錄路徑 -->
    <property name="githome" value="${env.WORKSPACE}/autoscript/彙總/" />
    
    <target name="run">
        <echo message="start..."/>
        <antcall target="clean" />
        <antcall target="test" />
        <antcall target="report" />
    </target>
    
    <target name="clean">
        <mkdir dir="${env.WORKSPACE}/results/${env.BUILD_ID}" />
    </target>
        
    <target name="test">
        <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
        <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
            <!-- 聲明要運行的腳本"*.jmx"指包含此目錄下的全部jmeter腳本,includes裏的文件也能夠用逗號,隔開-->
             <testplans dir="${githome}\坐席基本功能" includes="坐席基本功能彙總.jmx" />
            
            <property name="jmeter.save.saveservice.output_format" value="xml"/>
        </jmeter>
    </target>
    
    <path id="xslt.classpath">
        <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/>
        <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/>
    </path>
    
    <target name="report">
     <tstamp> <format property="report.datestamp" pattern="yyyy/MM/dd HH:mm" /></tstamp>
        <xslt 
              classpathref="xslt.classpath"
              force="true"
              in="${jmeter.result.jtlName}"
              out="${jmeter.result.htmlName}"
              style="${jmeter.home}/extras/jmeter.results.shanhe.me.xsl">
              <param name="dateReport" expression="${report.datestamp}"/>
              </xslt>
        <!-- 由於上面生成報告的時候,不會將相關的圖片也一塊兒拷貝至目標目錄,因此,須要手動拷貝 --> 
        <copy todir="${jmeter.result.dir}">
            <fileset dir="${jmeter.home}/extras">
                <include name="collapse.png" />
                <include name="expand.png" />
            </fileset>
        </copy>
    </target>
</project>


    3.cmd進入build.xml文件目錄,使用ant命令測試是否生效git

jmeter配置:express

  打開jmeter安裝目錄apache-jmeter-3.2\bin中的jmeter.properties,把jmeter.save.saveservice.output_format=csv  值修改成xml,即:         jmeter.save.saveservice.output_format=xmlapache

git配置:oracle

因爲我本機裝了sourcetree和git,私鑰已經生成過,因此不知道是否有影響,能夠先跳過,若是後續的jenkins沒法集成git,能夠參考這篇文章,後續我也會更新在新環境下的配置流程http://www.cnblogs.com/xuange306/p/6403907.htmlapp

jenkins配置:less

  1. 打開jenkins,進入插件管理,安裝插件:Performance PluginHTML Publisher pluginEmail Extension PluginAnt Plugin……附上個人插件安裝列表

  2.進入系統管理的Global Tool Configuration,配置ant、jdk、git的安裝路徑

 

  3.進入系統管理的系統設置,進行全局的配置。注意:系統管理員郵件地址必定要跟郵件配置的發件人地址一致

  4. 回到jenkins主頁,新建一個job,選擇自由風格 

  • 源碼管理
    •   選擇git,Repository URL輸入git地址:http://192.168.***.10/**r/**ter.git
    •   Credentials點擊ADD,增長本身的git賬號。如:
    •   Credentials選擇上步增長的賬號
    • Branch Specifier (blank for 'any')輸入本身項目的分支,個人項目在master下,就輸入*/master
  • 構建觸發器
    •   選擇Build periodically。輸入30 22 * * *     --表明天天的22:30執行
  • 構建
    • 增長構建步驟--Invoke ant:Ant Version選擇上面步驟配置ant的變量名稱ANT HOME;Targets輸入run;Build File輸入上述步驟的ant build.xml的文件路徑:D:\work\antxml\build-通訊平臺.xml
    • 增長構建步驟--Execute Windows batch command:輸入
      echo %WORKSPACE%
      echo %BUILD_ID%
      cd results
      cd %BUILD_ID%

       

  • 構建後操做
    • 增長Publish HTML reports:HTML directory to archive輸入results\$BUILD_ID;Index page[s]輸入TestReport.html;Report title輸入HTML Report
    • 增長Publish Performance test result report:性能監控的,能夠先忽略配置
    • 增長Editable Email Notification:基本配置介紹了,我以前的jenins文章有過介紹,額外增長一個附件的配置:Attachments輸入results\$BUILD_ID\TestReport.html

 

附上個人配置圖:

到此爲止,jenkins的基本配置就算完成,保存後,點擊當即構建,查看效果:

 

能夠看到有NAN的出現,下面咱們開始對jenkins的報告進行優化。。。


 

首先解決調NAN的問題

 1. 打開jmeter安裝目錄apache-jmeter-3.2\extras的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">
    <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')
    </description>
    
    <property name="testpath" value="${user.dir}"/>
    <property name="jmeter.home" value="${basedir}/.."/>
    <property name="report.title" value="**接口自動化測試報告"/>
    
    <!-- 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="_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="all" depends="run,report"/>

    <target name="run">
        <echo>funcMode = ${funcMode}</echo>
        <delete file="${testpath}/${test}.html"/>
        <jmeter
            jmeterhome="${jmeter.home}"
            testplan ="${testpath}/${test}.jmx"
            resultlog="${testpath}/${test}.jtl">
        <!--
            <jvmarg value="-Xincgc"/>
            <jvmarg value="-Xmx128m"/>
            <jvmarg value="-Dproperty=value"/>
            <jmeterarg value="-qextra.properties"/>
        -->
            <!-- Force suitable defaults -->
            <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="report" depends="xslt-report,copy-images">
        <echo>Report generated at ${report.datestamp}</echo>
    </target>

    <target name="xslt-report" depends="_message_xalan">
        <tstamp><format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/></tstamp>
        <xslt
            classpathref="xslt.classpath"
            force="true"
            in="${testpath}/${test}.jtl"
            out="${testpath}/${test}.html"
            
            style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl">
            <param name="showData" expression="${show-data}"/>
            <param name="titleReport" expression="${report.title}"/>
            <param name="dateReport" expression="${report.datestamp}"/>
        </xslt>
    </target>
<!--style="${basedir}/jmeter-results-detail-report${style_version}.xsl">-->
    <!-- Copy report images if needed -->
    <target name="copy-images" depends="verify-images" unless="samepath">
        <copy file="${basedir}/expand.png" tofile="${testpath}/expand.png"/>
        <copy file="${basedir}/collapse.png" tofile="${testpath}/collapse.png"/>
    </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>

2. 運行後的效果圖以下:

3. 顯示沒問題了,可是又碰到另外一問題,這樣的報告會把同一個接口自動歸類的一個標籤下面,以下圖,正常簽入下面有好多個簽入接口,沒有按照我腳本中的分組去顯示


接着優化

1. 在網上找到一個模版,下載地址:http://shanhe.me/2011/06/07/new-xsl-stylesheet-for-jmeter-command-line-results-report

2. 將下載好的模版存放在jmeter安裝目錄apache-jmeter-3.2\extras中。

3. 再次打開jmeter安裝目錄apache-jmeter-3.2\extras的build.xml,將<!--style="${basedir}/jmeter-results-detail-report${style_version}.xsl">-->替換爲下載的模版文件style="${basedir}/jmeter.results.shanhe.me.xsl"> 

<?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">
    <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')
    </description>
    
    <property name="testpath" value="${user.dir}"/>
    <property name="jmeter.home" value="${basedir}/.."/>
    <property name="report.title" value="**接口自動化測試報告"/>
    
    <!-- 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="_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="all" depends="run,report"/>

    <target name="run">
        <echo>funcMode = ${funcMode}</echo>
        <delete file="${testpath}/${test}.html"/>
        <jmeter
            jmeterhome="${jmeter.home}"
            testplan ="${testpath}/${test}.jmx"
            resultlog="${testpath}/${test}.jtl">
        <!--
            <jvmarg value="-Xincgc"/>
            <jvmarg value="-Xmx128m"/>
            <jvmarg value="-Dproperty=value"/>
            <jmeterarg value="-qextra.properties"/>
        -->
            <!-- Force suitable defaults -->
            <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="report" depends="xslt-report,copy-images">
        <echo>Report generated at ${report.datestamp}</echo>
    </target>

    <target name="xslt-report" depends="_message_xalan">
        <tstamp><format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/></tstamp>
        <xslt
            classpathref="xslt.classpath"
            force="true"
            in="${testpath}/${test}.jtl"
            out="${testpath}/${test}.html"
            
            style="${basedir}/jmeter.results.shanhe.me.xsl">
            <param name="showData" expression="${show-data}"/>
            <param name="titleReport" expression="${report.title}"/>
            <param name="dateReport" expression="${report.datestamp}"/>
        </xslt>
    </target>
<!--style="${basedir}/jmeter-results-detail-report${style_version}.xsl">-->
    <!-- Copy report images if needed -->
    <target name="copy-images" depends="verify-images" unless="samepath">
        <copy file="${basedir}/expand.png" tofile="${testpath}/expand.png"/>
        <copy file="${basedir}/collapse.png" tofile="${testpath}/collapse.png"/>
    </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>

4. 打開jmeter安裝目錄apache-jmeter-3.2\bin中的jmeter.properties,增長以下配置

jmeter.save.saveservice.data_type=true
jmeter.save.saveservice.label=true
jmeter.save.saveservice.response_code=true
# response_data is not currently supported for CSV output
jmeter.save.saveservice.response_data=true
# Save ResponseData for failed samples
jmeter.save.saveservice.response_data.on_error=false
jmeter.save.saveservice.response_message=true
jmeter.save.saveservice.successful=true
jmeter.save.saveservice.thread_name=true
jmeter.save.saveservice.time=true
jmeter.save.saveservice.subresults=true
jmeter.save.saveservice.assertions=true
jmeter.save.saveservice.latency=true
jmeter.save.saveservice.connect_time=true
jmeter.save.saveservice.samplerData=true
jmeter.save.saveservice.responseHeaders=true
jmeter.save.saveservice.requestHeaders=true
jmeter.save.saveservice.encoding=false
jmeter.save.saveservice.bytes=true
jmeter.save.saveservice.url=true
jmeter.save.saveservice.filename=true
jmeter.save.saveservice.hostname=true
jmeter.save.saveservice.thread_counts=true
jmeter.save.saveservice.sample_count=true
jmeter.save.saveservice.idle_time=true

5. 進入jenkins,再次構建,查看報告:

到這裏,已經初步知足了我此次項目的需求,爲了達到效果,我再報告中又增長了一個測試結果概要的描述。以下圖:

具體步驟是把jmeter-results-detail-report_21.xsl中的概要複製到jmeter.results.shanhe.me.xsl中,並修改css樣式。詳細報告可進個人網盤下載:連接: https://pan.baidu.com/s/1jIcb1ci 


 

貼一個最終發郵件的圖吧:

附件效果圖:

相關文章
相關標籤/搜索