How to generate html report using ant+jmeter

1. Install ant

Download apache-ant, put it in the corresponding path, and configure the environment variables. Add E:\Installation Pack\eclipse\apache-ant-1.9.6\lib to the CLASSPATH of the system variable; add E:\Installation Pack\eclipse\apache-ant-1.9.6\bin to the PATH of the user variable. Open cmd, execute ant, and prompt "Buildfile: build.xml does not exist! Build failed" means the configuration is successful and the ant command is available. As shown below.

2. Write a test plan:

See jmeter's http request. Such as baidu.jmx.

3. Write the build.xml file 

<?xml version="1.0" encoding="UTF-8"?>
<project name="ant-jmeter-test" default="all" basedir=".">
  <!--为生成的jtl和html文件加时间戳-->
    <tstamp>
        <format property="time" pattern="yyyyMMddhhmm" />
    </tstamp>
   <!--更改为自己的jmeter路径-->
    <property name="jmeter.home" value="E:\Installation Pack\jmeter" />
   <!--更改为自己的jtl文件结果存放路径-->
    <property name="jmeter.result.jtl.dir" value="E:\Installation Pack\JmeterTest\resultLog\jtl" />
   <!--更改为自己的html文件结果存放路径-->
    <property name="jmeter.result.html.dir" value="E:\Installation Pack\JmeterTest\resultLog\html" />
    <!--生成的html报告的前缀-->
    <property name="ReportName" value="TestReport" />
   <!--jlt和html文件名称-->
    <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${ReportName}${time}.jtl" />
    <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${ReportName}${time}.html" />
    
    <target name="all">
        <antcall target="test" />
        <antcall target="report" />
    </target>
    
    <target name="test">
        <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
        <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
       <!--改为自己的jmx文件所在路径-->
            <testplans dir="E:\Installation Pack\jmeter\bin\examples" includes="*.jmx" />
        </jmeter>
    </target>
    
    <target name="report">
     <!--使用jmeter自己的转化文件:将jtl转化为xsl文件,改为自己的xsl文件所在路径-->
        <xslt in="${jmeter.result.jtlName}" out="${jmeter.result.htmlName}" style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl" />
        <copy todir="${jmeter.result.html.dir}">
            <fileset dir="${jmeter.home}/extras">
                <include name="collapse.png" />
                <include name="expand.png" />
            </fileset>
        </copy>
    </target>
</project>

 

4. Results

The jtl and html files are generated under the corresponding path, such as TestReport201511091118.html. Open the html file in the browser to get the following.

Five, the use of ant+jmeter under linux

1 Installation: There are two types of offline and online installation. I chose offline installation.

Download the tar.gz version of ant from http://ant.apache.org and copy it to the usr path

Decompress: tar -xzvf apache-ant-1.9.6.tar.gz 

vi /etc/profile edit ANT_HOME path:

export ANT_HOME=/usr/apache-ant-1.9.2

export PATH=$PATH:$ANT_HOME/bin

source /etc/profile to make the configuration take effect, and execute ant -version to test whether ant takes effect.

2 jmeter Copy the jmeter folder to the linux machine, I put it in the /opt/soft/jmeter path, chmod 777 /opt/soft/jmeter/bin/jmeter.sh. Test whether jmeter is available in linux:

 

[root@bjdhj-120-215 bin]# sh jmeter.sh -n -t examples/AdTest.jmx -l result.jtl
Created the tree successfully using examples/AdTest.jmx
Starting the test @ Fri Nov 27 17:42:13 CST 2015 (1448617333572)
Waiting for possible shutdown message on port 4445
Tidying up ...    @ Fri Nov 27 17:42:14 CST 2015 (1448617334581)
... end of run

3 su jenkins user, execute ant, test whether it can be executed correctly and generate corresponding files. Attention to permissions

Six, the problem

Every time I do something, I will encounter a lot of problems, and this time is no exception. To sum up, the problems are as follows

1 In the generated html report, Min Time and Max Time have no value, and NaN is displayed. Baidu needs to add two jar packages of xalan. Yes, so I only put the xalan.jar package in the ant lib, it can run correctly and get the Max and Min values

报错信息如下:E:\Installation Pack\JmeterTest\resultLog\jtl\build.xml:16: The following error occurred while executing this line: E:\Installation Pack\JmeterTest\resultLog\jtl\build.xml:27: java.lang.IllegalAccessError: tried to access class org.apache.xml.serializer.ExtendedContentHandler from class org.apache.xalan.transformer.TransformerImpl at org.apache.xalan.transformer.TransformerImpl.createSerializationHandler(TransformerImpl.java:1152)

2 The source document says that the parameter of 90% Line can be increased and the code is posted, but I failed to experiment and feel that the writing is wrong. as follows

<!--.......................其他内容略....................................-->
<!-- New add 90% line 这部分可不写,直接在<xsl:template name="summary">中把position参数传过去,position参数即count(/testResults/*)的值(总行数)-->
<xsl:variable name="allLineTime">
    <xsl:call-template name="line">
        <xsl:with-param name="nodes" select="/testResults/*/@t" />
        <xsl:with-param name="postion" select="$allPostion" /> 
    </xsl:call-template>
</xsl:variable>
<!--.......................其他内容略....................................-->
<!-- New add 90% line -->
<xsl:template name="line">
    <xsl:param name="nodes" select="/.." />
    <xsl:param name="postion" select="/.." />
    <xsl:choose>
        <xsl:when test="not($nodes)">LNaN</xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$nodes">
          <!--nodes得到的是响应时间列表,descending即按倒序排列,$position即得到的倒序后的最后一条,也就是第一条,所以并不是90%Line Time,我不大会写xml,未完成这个问题的解决,试图用公式没有成功。-->
                <xsl:sort data-type="number" order="descending" />
                <xsl:if test="position() =$postion">
            <!--number(.)即得到对应position的值。也就是指定记录的响应时间-->
                    <xsl:value-of select="number(.)" />
                </xsl:if>
            </xsl:for-each>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
<!--.......................其他内容略....................................-->

3 If 90% can be added, how is qps calculated? Do they all need to write formula statistics by themselves? Ask colleagues who are good at xml to solve problem 2 tomorrow, and then expand the function later

ps: It’s so sad. I asked a colleague a question. He saw that I was using ant to compile, and he despised me greatly. He said that now it’s all maven, and no one uses ant. cry

4 问题:taskdef class org.programmerplanet.ant.taskdefs.jmeter.JMeterTask cannot be found

Solution: Put the jar package: ant-jmeter-1.0.9.jar in the lib path of ant

5 Ant cannot be executed correctly under linux:

For permission issues, just change all directories involved in ant to jenkins permissions.

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [ software testing ] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive

Guess you like

Origin blog.csdn.net/okcross0/article/details/132605161