zoukankan      html  css  js  c++  java
  • jenkins / ant / jmeter 持续集成接口自动化

    1、 将 jmeter 脚本放在/var/lib/jenkins/workspace/Jmeter_auto/jmxpath路径下

    2、 点击http://jk.facebank.net.cn/job/Jmeter_auto/立即构建即可

    最终生成的报告会在 /var/lib/jenkins/workspace/Jmeter_auto/jmxpath/时间目录/html/文件夹中,里面包含整个脚本运行的结果

    jmeter 安装目录:

    /var/lib/apache-jmeter-3.0, ant中原始的build.xml是在jmeter就有的,/var/lib/apache-jmeter-3.0/extras下

    ant 安装目录:

    /var/lib/apache-ant-1.9.6  ,需要将jmeter中的多个配置都拷贝到ant目录下一份 

    build.xml, jmeter-results-detail-report_21.xsl,collapse.png,expand.png

    原理如下:

    1、jenkins 调用ant 的build.xml 进行构建

    2、ant的build.xml可调用jmeter的脚本(jmx) 生成jtl内容,

    3、将jtl依据jmeter-results-report_21.xsl 转变为html测试报告

    附件中 build.xml只能对一个jmx文件进行操作

    build2.xml可以对多个jmx文件进行操作

    ant-cortrib-1.0b3.jar 是对多个jmx文件进行操作必须的Jar包,使用foreach的包

    并必须在build.xml添加如下内容:

    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
    <pathelement location="/var/lib/apache-ant-1.9.6/lib/ant-contrib-1.0b3.jar"/>
    </classpath>
    </taskdef>

    http://ant-contrib.sourceforge.net/ 对ant-contrib.jar的说明

    jmeter-results-detail-report_21.xsl

    ant-contrib-1.0b3.jar

    build2.xml

    build.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">
    <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="/var/lib/jenkins/workspace/Jmeter_auto/jmxpath"/>
    <property name="jmeter.home" value="/var/lib/apache-jmeter-3.0/"/>
    <property name="report.title" value="Load Test Results"/>
    <property name="jmeter.classpath" value="/var/lib/apache-jmeter-3.0/"/>
    <!-- Name of test (without .jmx) -->
    <property name="test" value="HTTPCOOK"/>

    <!-- 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">
    <pathelement location="/var/lib/apache-ant-1.9.6/lib/ant-jmeter-1.1.1.jar" />
    <!-- <fileset dir="/var/lib/apache-ant-1.9.6">
    <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}"/>

    <property name="jmeter.save.saveservice.response_data" value="true"/>
    <property name="jmeter.save.saveservice.samplerData" value="true"/>
    <property name="jmeter.save.saveservice.responseHeaders" value="true"/>
    <property name="jmeter.save.saveservice.requestHeaders" value="true"/>
    <property name="jmeter.save.saveservice.encoding" value="true"/>
    <property name="jmeter.save.saveservice.url" value="true"/>
    <property name="jmeter.save.saveservice.filename" value="true"/>
    <property name="jmeter.save.saveservice.hostname" value="true"/>
    <property name="jmeter.save.saveservice.thread_counts" value="true"/>
    <property name="jmeter.save.saveservice.sample_count" value="true"/>
    <property name="jmeter.save.saveservice.idle_time" value="true"/>
    <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="/var/lib/apache-ant-1.9.6/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>

    <!-- Copy report images if needed -->
    <target name="copy-images" depends="verify-images" unless="samepath">
    <copy file="/var/lib/apache-ant-1.9.6/expand.png" tofile="${testpath}/expand.png"/>
    <copy file="/var/lib/apache-ant-1.9.6/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>

    build2.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')

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

    </description>

    <property name="testpath" value="/var/lib/jenkins/workspace/Jmeter_auto/jmxpath"/>
    <property name="jmeter.home" value="/var/lib/apache-jmeter-3.0/"/>
    <property name="report.title" value="Load Test Results"/>
    <property name="jmeter.classpath" value="/var/lib/apache-jmeter-3.0/"/>
    <property name="results.jtl" value="jtl"/>
    <property name="results.html" value ="html"/>
    <property name="runremote" value="false"/>
    <property name="report.title" value="WebLoad Test Report"/>
    <tstamp><format property="report.datestamp" pattern="yyyy-MM-dd-HH-mm-ss"/></tstamp>
    <property name="time" value="${report.datestamp}"/>
    <property name = "interval-time-in-seconds" value ="10"/>
    <!-- Name of test (without .jmx) -->
    <!-- <property name="test" value="HTTPCOOK"/> -->

    <!-- 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">
    <pathelement location="/var/lib/apache-ant-1.9.6/lib/ant-jmeter-1.1.1.jar" />
    <!-- <fileset dir="/var/lib/apache-ant-1.9.6">
    <include name="ant-jmeter*.jar"/>
    </fileset>
    -->
    </path>

    <taskdef
    name="jmeter"
    classpathref="jmeter.classpath"
    classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/>

    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
    <pathelement location="/var/lib/apache-ant-1.9.6/lib/ant-contrib-1.0b3.jar"/>
    </classpath>
    </taskdef>

    <!--运行之前首先创建临时结果文件夹-->
    <target name="create-folder">
    <delete dir="${testpath}/temp"/>
    <mkdir dir="${testpath}/temp/${results.jtl}" />
    <mkdir dir="${testpath}/temp/${results.html}" />
    </target>

    <target name="all-test" depends="create-folder">
    <foreach target="test1" param="jmxfile" >
    <fileset dir="/var/lib/jenkins/workspace/Jmeter_auto/jmxpath/">
    <include name="*.jmx" />
    </fileset>
    </foreach>
    </target>

    <target name="test1" >
    <basename property="jmx.filename" file="${jmxfile}" suffix=".jmx"/>
    <echo message="---------- Processing ${jmxfile} -----------"/>
    <echo message="resultlogdir===${testpath}/temp/${results.jtl}"/>
    <jmeter jmeterhome="${jmeter.home}" resultlogdir="${testpath}/temp/${results.jtl}" runremote="${runremote}" resultlog="${jmx.filename}.jtl"
    testplan="/var/lib/jenkins/workspace/Jmeter_auto/jmxpath/${jmx.filename}.jmx">
    <!-- <jvmarg value="-Xincgc"/>
    <jvmarg value="-Xms1024m"/>
    <jvmarg value="-Xm1024m"/>
    -->
    </jmeter>
    <sleep seconds="20"></sleep>
    <!--Generate html report-->

    <xslt in="${testpath}/temp/${results.jtl}/${jmx.filename}.jtl"
    out="${testpath}/temp/${results.html}/${jmx.filename}.html" classpathref="xslt.classpath"
    style="${jmeter.home}/extras/jmeter-results-report_21.xsl" >
    <param name="dateReport" expression="${report.datestamp}"/>
    <param name="showData" expression="n"/>
    <param name="titleReport" expression="${report.title}:[${jmx.filename}]"/>
    </xslt>

    <echo message="Sleep ${interval-time-in-seconds} Seconds, and then start next Test; Please waiting ......"/>
    <sleep seconds="${interval-time-in-seconds}"></sleep>
    </target>

    <target name="copy-images" depends="all-test">
    <copy file="${jmeter.home}/extras/expand.png" tofile="${results.html}/expand.png"/>
    <copy file="${jmeter.home}/extras/collapse.png" tofile="${results.html}/collapse.png"/>
    <copydir src="${testpath}/temp" dest="${testpath}/${report.datestamp}"/>
    <delete dir="${testpath}/temp"/>
    </target>

    <target name="all" depends="all-test, copy-images" />
    <!--
    <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}"/>

    <property name="jmeter.save.saveservice.response_data" value="true"/>
    <property name="jmeter.save.saveservice.samplerData" value="true"/>
    <property name="jmeter.save.saveservice.responseHeaders" value="true"/>
    <property name="jmeter.save.saveservice.requestHeaders" value="true"/>
    <property name="jmeter.save.saveservice.encoding" value="true"/>
    <property name="jmeter.save.saveservice.url" value="true"/>
    <property name="jmeter.save.saveservice.filename" value="true"/>
    <property name="jmeter.save.saveservice.hostname" value="true"/>
    <property name="jmeter.save.saveservice.thread_counts" value="true"/>
    <property name="jmeter.save.saveservice.sample_count" value="true"/>
    <property name="jmeter.save.saveservice.idle_time" value="true"/>
    <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}"/>


    <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="/var/lib/apache-ant-1.9.6/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>
    -->
    <!-- Copy report images if needed -->
    <!-- <target name="copy-images" depends="verify-images" unless="samepath">
    <copy file="/var/lib/apache-ant-1.9.6/expand.png" tofile="${testpath}/expand.png"/>
    <copy file="/var/lib/apache-ant-1.9.6/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>

  • 相关阅读:
    碰到了一个GDI+的问题,郁闷ing。。。
    (旧文重贴)对于以前遇到过的ws proxy实例化时间太长的问题,看JNDI受到了些启发
    (旧文重贴)下午又看了MBF的介绍,靠,心丧如死!
    (旧文重贴)前段时间关于SmartClient部署的两个问题,都搞定了。
    (旧文重贴)编程高手箴言读书笔记(1)
    SAP MDM 学习网址
    常用Oralce sql
    WE02 修改IDOC
    OBD/IBD 的拣配数量更新
    配置ALE仓库管理
  • 原文地址:https://www.cnblogs.com/yingchen/p/6142427.html
Copyright © 2011-2022 走看看