首页 > 代码库 > jenkins / ant / jmeter 持续集成接口自动化
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=http://www.mamicode.com/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="http://www.mamicode.com/var/lib/jenkins/workspace/Jmeter_auto/jmxpath"/>
<property name="jmeter.home" value="http://www.mamicode.com/var/lib/apache-jmeter-3.0/"/>
<property name="report.title" value="http://www.mamicode.com/Load Test Results"/>
<property name="jmeter.classpath" value="http://www.mamicode.com/var/lib/apache-jmeter-3.0/"/>
<!-- Name of test (without .jmx) -->
<property name="test" value="http://www.mamicode.com/HTTPCOOK"/>
<!-- Should report include response data for failures? -->
<property name="show-data" value="http://www.mamicode.com/n"/>
<property name="format" value="http://www.mamicode.com/2.1"/>
<condition property="style_version" value="">
<equals arg1="${format}" arg2="2.0"/>
</condition>
<condition property="style_version" value="http://www.mamicode.com/_21">
<equals arg1="${format}" arg2="2.1"/>
</condition>
<condition property="funcMode">
<equals arg1="${show-data}" arg2="y"/>
</condition>
<condition property="funcMode" value="http://www.mamicode.com/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="http://www.mamicode.com/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="http://www.mamicode.com/-Xincgc"/>
<jvmarg value="http://www.mamicode.com/-Xmx128m"/>
<jvmarg value="http://www.mamicode.com/-Dproperty=value"/>
<jmeterarg value="http://www.mamicode.com/-qextra.properties"/>
-->
<!-- Force suitable defaults -->
<property name="jmeter.save.saveservice.output_format" value="http://www.mamicode.com/xml"/>
<property name="jmeter.save.saveservice.assertion_results" value="http://www.mamicode.com/all"/>
<property name="jmeter.save.saveservice.bytes" value="http://www.mamicode.com/true"/>
<property name="file_format.testlog" value="http://www.mamicode.com/${format}"/>
<property name="jmeter.save.saveservice.response_data.on_error" value="http://www.mamicode.com/${funcMode}"/>
<property name="jmeter.save.saveservice.response_data" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.samplerData" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.responseHeaders" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.requestHeaders" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.encoding" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.url" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.filename" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.hostname" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.thread_counts" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.sample_count" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.idle_time" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.output_format" value="http://www.mamicode.com/xml"/>
<property name="jmeter.save.saveservice.assertion_results" value="http://www.mamicode.com/all"/>
<property name="jmeter.save.saveservice.bytes" value="http://www.mamicode.com/true"/>
<property name="file_format.testlog" value="http://www.mamicode.com/${format}"/>
<property name="jmeter.save.saveservice.response_data.on_error" value="http://www.mamicode.com/${funcMode}"/>
</jmeter>
</target>
<property name="lib.dir" value="http://www.mamicode.com/${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="http://www.mamicode.com/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="http://www.mamicode.com/xslt.classpath" classname="org.apache.xalan.processor.TransformerFactoryImpl"/>
<available classpathref="http://www.mamicode.com/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=http://www.mamicode.com/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="http://www.mamicode.com/var/lib/jenkins/workspace/Jmeter_auto/jmxpath"/>
<property name="jmeter.home" value="http://www.mamicode.com/var/lib/apache-jmeter-3.0/"/>
<property name="report.title" value="http://www.mamicode.com/Load Test Results"/>
<property name="jmeter.classpath" value="http://www.mamicode.com/var/lib/apache-jmeter-3.0/"/>
<property name="results.jtl" value="http://www.mamicode.com/jtl"/>
<property name="results.html" value ="http://www.mamicode.com/html"/>
<property name="runremote" value="http://www.mamicode.com/false"/>
<property name="report.title" value="http://www.mamicode.com/WebLoad Test Report"/>
<tstamp><format property="report.datestamp" pattern="yyyy-MM-dd-HH-mm-ss"/></tstamp>
<property name="time" value="http://www.mamicode.com/${report.datestamp}"/>
<property name = "interval-time-in-seconds" value ="http://www.mamicode.com/10"/>
<!-- Name of test (without .jmx) -->
<!-- <property name="test" value="http://www.mamicode.com/HTTPCOOK"/> -->
<!-- Should report include response data for failures? -->
<property name="show-data" value="http://www.mamicode.com/n"/>
<property name="format" value="http://www.mamicode.com/2.1"/>
<condition property="style_version" value="">
<equals arg1="${format}" arg2="2.0"/>
</condition>
<condition property="style_version" value="http://www.mamicode.com/_21">
<equals arg1="${format}" arg2="2.1"/>
</condition>
<condition property="funcMode">
<equals arg1="${show-data}" arg2="y"/>
</condition>
<condition property="funcMode" value="http://www.mamicode.com/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="http://www.mamicode.com/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="http://www.mamicode.com/-Xincgc"/>
<jvmarg value="http://www.mamicode.com/-Xms1024m"/>
<jvmarg value="http://www.mamicode.com/-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="http://www.mamicode.com/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="http://www.mamicode.com/${testpath}/temp" dest="${testpath}/${report.datestamp}"/>
<delete dir="${testpath}/temp"/>
</target>
<target name="all" depends="all-test, copy-images" />
<!--
<jvmarg value="http://www.mamicode.com/-Xincgc"/>
<jvmarg value="http://www.mamicode.com/-Xmx128m"/>
<jvmarg value="http://www.mamicode.com/-Dproperty=value"/>
<jmeterarg value="http://www.mamicode.com/-qextra.properties"/>
-->
<!-- Force suitable defaults -->
<property name="jmeter.save.saveservice.output_format" value="http://www.mamicode.com/xml"/>
<property name="jmeter.save.saveservice.assertion_results" value="http://www.mamicode.com/all"/>
<property name="jmeter.save.saveservice.bytes" value="http://www.mamicode.com/true"/>
<property name="file_format.testlog" value="http://www.mamicode.com/${format}"/>
<property name="jmeter.save.saveservice.response_data.on_error" value="http://www.mamicode.com/${funcMode}"/>
<property name="jmeter.save.saveservice.response_data" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.samplerData" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.responseHeaders" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.requestHeaders" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.encoding" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.url" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.filename" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.hostname" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.thread_counts" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.sample_count" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.idle_time" value="http://www.mamicode.com/true"/>
<property name="jmeter.save.saveservice.output_format" value="http://www.mamicode.com/xml"/>
<property name="jmeter.save.saveservice.assertion_results" value="http://www.mamicode.com/all"/>
<property name="jmeter.save.saveservice.bytes" value="http://www.mamicode.com/true"/>
<property name="file_format.testlog" value="http://www.mamicode.com/${format}"/>
<property name="jmeter.save.saveservice.response_data.on_error" value="http://www.mamicode.com/${funcMode}"/>
<property name="lib.dir" value="http://www.mamicode.com/${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="http://www.mamicode.com/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="http://www.mamicode.com/xslt.classpath" classname="org.apache.xalan.processor.TransformerFactoryImpl"/>
<available classpathref="http://www.mamicode.com/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>
jenkins / ant / jmeter 持续集成接口自动化