CentOS:Jmeter5.2.1+Ant1.10.12+jmeter.results.shanhe.me.xsl(亲测可用)

jmeter+ant

安装ant

1、下载地址:https://ant.apache.org/bindownload.cgi
2、解压并剪切到固定目录

tar -zxvf apache-ant-1.10.12-bin.tar.gz
mv apache-ant-1.10.12 /usr/local/

3、配置环境变量vim /etc/profile

#配置ant环境
export ANT_HOME=/usr/local/apache-ant-1.10.12
export PATH=$PATH:$ANT_HOME/bin:$ANT_HOME/lib

4、环境变量生效

source /etc/profile

5、验证

ant -v

6、jmeter目录下创建entry_name文件夹(测试项目存放路径)

cd /usr/local/apache-jmeter-5.2.1
mkdir entry_name

7、在entry_name目录下面,创建build.xml文件,文件编码格式必须是UTF-8
jmeter.results.shanhe.me.xsl模板用下面这个build.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project name="接口测试监控" default="run" basedir=".">
    <property name="jmeterPath" value="/usr/local/apache-jmeter-5.2.1"/>
    <!-- jmeter脚本路径 -->
    <property name="scriptsPath" value="${jmeterPath}/entry_name/jmeter_scripts"/>
    <!-- jmeter测试报告路径 -->
    <property name="reportPath" value="${jmeterPath}/entry_name/report"/>
    <!-- email配置信息 -->
    <property name="mail_host" value="smtp.126.com"/>
    <property name="mail_port" value="465"/>
    <!-- email发送账户密码 -->
    <property name="username" value="wuyxxxxxx8@126.com"/>
    <property name="password" value="KWHCXXXXXXXMTQCZ"/>
    <property name="mail_from" value="wuyxxxxxx8@126.com"/>
    <!-- mail_to:接收邮件列表,多个用逗号隔开 -->
    <property name="mail_to" value="3967xxx96@qq.com"/>
    <property name="mailsubject" value="Jmeter接口自动化测试报告"/>
    <property name="message" value="今日接口测试任务已执行完毕,详情请查看附件!!!"/>
    
    <tstamp><format property="time" pattern="yyyyMMddHHmm" /></tstamp>
    
    <!-- jmeter环境配置 -->
    <property name="jmeter.home" value="${jmeterPath}"/>
    <!-- jtl测试结果存放路径 -->
    <property name="jmeter.result.jtl.dir" value="${reportPath}/resultLog/jtl"/>
    <!-- html测试结果存放路径 -->
    <property name="jmeter.result.html.dir" value="${reportPath}/resultLog/html"/>
    <property name="htmlReportNameSummary" value="TestReport"/>
    <property name="jmeter.result.jtlName" value="${jmeter.result.jtl.dir}/${htmlReportNameSummary}${time}.jtl"/>
    <property name="jmeter.result.htmlName" value="${jmeter.result.html.dir}/${htmlReportNameSummary}${time}.html"/>

    <target name="run">
        <antcall target="test"/>
        <antcall target="report"/>
        <antcall target="sendEmail"/>
    </target>
       
    <!-- 执行接口测试 -->
    <target name="test">
        <echo>执行接口自动化测试用例</echo>
        <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
        <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
            <!-- 声明要运行的脚本“*.jmx”指包含此目录下的所有jmeter脚本 -->
            <testplans dir="${scriptsPath}" includes="*.jmx" />
            <property name="jmeter.save.saveservice.output_format" value="xml"/>
        </jmeter>
    </target>
    
    <!-- 解决报告中NAN字段显示问题-->
    <path id="xslt.classpath">
        <fileset dir="${jmeter.home}/lib" includes="xalan*.jar" />
        <fileset dir="${jmeter.home}/lib" includes="serializer*.jar" />    
    </path>
    
    <target name="report">
        <echo>生成接口自动化测试报告</echo>
            <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="titleReport" expression="${mailsubject}${time}"/> 
                  <param name="dateReport" expression="${report.datestamp}"/>
            </xslt>   
                  
            <!-- 因为上面生成报告的时候,不会将相关的图片也一起拷贝至目标目录,所以,需要手动拷贝 -->  
            <copy file="${jmeter.home}/extras/expand.png" tofile="${jmeter.result.html.dir}/expand.png" />
            <copy file="${jmeter.home}/extras/collapse.png" tofile="${jmeter.result.html.dir}/collapse.png" />
            
    </target>
    
    <target name="sendEmail">
        <echo>发送测试报告</echo>
            <mail mailhost="${mail_host}" 
                  ssl="true"
                  user="${username}"
                  password="${password}"
                  mailport="${mail_port}"
                  subject="${mailsubject}"
                  messagemimetype="text/html"
                  tolist="${mail_to}">
            <from address="${mail_from}" />
            
                <attachments>
                    <fileset dir="${jmeter.result.html.dir}">
                        <include name="${htmlReportNameSummary}${time}.html" />
                        <include name="collapse.png" />
                        <include name="expand.png" />
                    </fileset>
                </attachments>
                
                <message>
                ${message}
                </message>
            </mail>
    </target>
</project>

8、测试项目目录下创建jmeter_scripts文件夹(脚本存放路径)

cd /usr/local/apache-jmeter-5.2.1/entry_name
mkdir jmeter_scripts

将jmeter脚本放在这个目录下面,没有脚本会报错

9、测试项目目录下创建report文件夹(测试报告存放路径)

cd /usr/local/apache-jmeter-5.2.1/entry_name
mkdir report

10、将jmeter的extras目录下ant-jmeter-1.1.1.jar复制到ant的lib目录下

cd /usr/local/apache-jmeter-5.2.1/extras/
cp ant-jmeter-1.1.1.jar /usr/local/apache-ant-1.10.12/lib/

11、修改jmeter的bin目录下jmeter.properties文件
vim jmeter.properties

jmeter.save.saveservice.output_format=xml

12、下载email相关jar包,并将这三个jar包复制到ant的lib目录下
点击下载,commons-email-1.5.jar
点击下载,activation-1.1.1.jar
点击下载,javax.mail.jar

三个jar包一起下载

13、下载jmeter.results.shanhe.me.xsl模板文件,并将其复制到jmeter的extras目录下
jmeter.results.shanhe.me.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 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.
-->

<!-- 
    Stylesheet for processing 2.1 output format test result files 
    To uses this directly in a browser, add the following to the JTL file as line 2:
    <?xml-stylesheet type="text/xsl" href="../extras/jmeter-results-detail-report_21.xsl"?>
    and you can then view the JTL in a browser
-->

<xsl:output method="html" indent="yes" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />

<!-- Defined parameters (overrideable) -->
<xsl:param    name="showData" select="'n'"/>
<xsl:param    name="titleReport" select="'Load Test Results'"/>
<xsl:param    name="dateReport" select="'date not defined'"/>

<xsl:template match="testResults">
    <html>
        <head>
            <title><xsl:value-of select="$titleReport" /></title>
            <style type="text/css">
                body {
                    font:normal 68% verdana,arial,helvetica;
                    color:#000000;
                }
                table tr td, table tr th {
                    font-size: 68%;
                }
                table.details tr th{
                    color: #ffffff;
                    font-weight: bold;
                    text-align:center;
                    background:#2674a6;
                    white-space: nowrap;
                }
                table.details tr td{
                    background:#eeeee0;
                    white-space: nowrap;
                }
                h1 {
                    margin: 0px 0px 5px; font: 165% verdana,arial,helvetica
                }
                h2 {
                    margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica
                }
                h3 {
                    margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica
                }
                .Failure {
                    font-weight:bold; color:red;
                }
                
    
                img
                {
                  border-width: 0px;
                }
                
                .expand_link
                {
                   position=absolute;
                   right: 0px;
                   width: 27px;
                   top: 1px;
                   height: 27px;
                }
                
                .page_details
                {
                   display: none;
                }
                                
                                .page_details_expanded
                                {
                                    display: block;
                                    display/* hide this definition from  IE5/6 */: table-row;
                                }


            </style>
            <script language="JavaScript"><![CDATA[
                           function expand(details_id)
               {
                  
                  document.getElementById(details_id).className = "page_details_expanded";
               }
               
               function collapse(details_id)
               {
                  
                  document.getElementById(details_id).className = "page_details";
               }
               
               function change(details_id)
               {
                  if(document.getElementById(details_id+"_image").src.match("expand"))
                  {
                     document.getElementById(details_id+"_image").src = "collapse.png";
                     expand(details_id);
                  }
                  else
                  {
                     document.getElementById(details_id+"_image").src = "expand.png";
                     collapse(details_id);
                  } 
                           }
            ]]></script>
        </head>
        <body>
        
            <xsl:call-template name="pageHeader" />
            
            <xsl:call-template name="summary" />
            <hr size="1" width="95%" align="center" />
            
            <xsl:call-template name="pagelist" />
            <hr size="1" width="95%" align="center" />
            
            <xsl:call-template name="detail" />

        </body>
    </html>
</xsl:template>

<xsl:template name="pageHeader">
    <h1><xsl:value-of select="$titleReport" /></h1>
    <table width="100%">
        <tr>
            <td align="left">Date report: <xsl:value-of select="$dateReport" /></td>
            <td align="right">Designed for use with <a href="http://jmeter.apache.org/">JMeter</a> and <a href="http://ant.apache.org">Ant</a>.</td>
        </tr>
    </table>
    <hr size="1" />
</xsl:template>

<xsl:template name="summary">
    <h2>Summary</h2>
    <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
        <tr valign="top">
            <th># Samples</th>
            <th>Failures</th>
            <th>Success Rate</th>
            <th>Average Time</th>
            <th>Min Time</th>
            <th>Max Time</th>
        </tr>
        <tr valign="top">
            <xsl:variable name="allCount" select="count(/testResults/*)" />
            <xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />
            <xsl:variable name="allSuccessCount" select="count(/testResults/*[attribute::s='true'])" />
            <xsl:variable name="allSuccessPercent" select="$allSuccessCount div $allCount" />
            <xsl:variable name="allTotalTime" select="sum(/testResults/*/@t)" />
            <xsl:variable name="allAverageTime" select="$allTotalTime div $allCount" />
            <xsl:variable name="allMinTime">
                <xsl:call-template name="min">
                    <xsl:with-param name="nodes" select="/testResults/*/@t" />
                </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="allMaxTime">
                <xsl:call-template name="max">
                    <xsl:with-param name="nodes" select="/testResults/*/@t" />
                </xsl:call-template>
            </xsl:variable>
            <xsl:attribute name="class">
                <xsl:choose>
                    <xsl:when test="$allFailureCount &gt; 0">Failure</xsl:when>
                </xsl:choose>
            </xsl:attribute>
            <td align="center">
                <xsl:value-of select="$allCount" />
            </td>
            <td align="center">
                <xsl:value-of select="$allFailureCount" />
            </td>
            <td align="center">
                <xsl:call-template name="display-percent">
                    <xsl:with-param name="value" select="$allSuccessPercent" />
                </xsl:call-template>
            </td>
            <td align="center">
                <xsl:call-template name="display-time">
                    <xsl:with-param name="value" select="$allAverageTime" />
                </xsl:call-template>
            </td>
            <td align="center">
                <xsl:call-template name="display-time">
                    <xsl:with-param name="value" select="$allMinTime" />
                </xsl:call-template>
            </td>
            <td align="center">
                <xsl:call-template name="display-time">
                    <xsl:with-param name="value" select="$allMaxTime" />
                </xsl:call-template>
            </td>
        </tr>
    </table>
</xsl:template>

<xsl:template name="pagelist">
    <h2>Pages</h2>
    <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
        <tr valign="top">
            <th>URL</th>
            <th># Samples</th>
            <th>Failures</th>
            <th>Success Rate</th>
            <th>Average Time</th>
            <th>Min Time</th>
            <th>Max Time</th>
            <th></th>
        </tr>
        <xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]">
            <xsl:variable name="label" select="@lb" />
            <xsl:variable name="count" select="count(../*[@lb = current()/@lb])" />
            <xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" />
            <xsl:variable name="successCount" select="count(../*[@lb = current()/@lb][attribute::s='true'])" />
            <xsl:variable name="successPercent" select="$successCount div $count" />
            <xsl:variable name="totalTime" select="sum(../*[@lb = current()/@lb]/@t)" />
            <xsl:variable name="averageTime" select="$totalTime div $count" />
            <xsl:variable name="minTime">
                <xsl:call-template name="min">
                    <xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" />
                </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="maxTime">
                <xsl:call-template name="max">
                    <xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" />
                </xsl:call-template>
            </xsl:variable>
            <tr valign="top">
                <xsl:attribute name="class">
                    <xsl:choose>
                        <xsl:when test="$failureCount &gt; 0">Failure</xsl:when>
                    </xsl:choose>
                </xsl:attribute>
                <td>
                <xsl:if test="$failureCount > 0">
                  <a><xsl:attribute name="href">#<xsl:value-of select="$label" /></xsl:attribute>
                  <xsl:value-of select="$label" />
                  </a>
                </xsl:if>
                <xsl:if test="0 >= $failureCount">
                  <xsl:value-of select="$label" />
                </xsl:if>
                </td>
                <td align="center">
                    <xsl:value-of select="$count" />
                </td>
                <td align="center">
                    <xsl:value-of select="$failureCount" />
                </td>
                <td align="right">
                    <xsl:call-template name="display-percent">
                        <xsl:with-param name="value" select="$successPercent" />
                    </xsl:call-template>
                </td>
                <td align="right">
                    <xsl:call-template name="display-time">
                        <xsl:with-param name="value" select="$averageTime" />
                    </xsl:call-template>
                </td>
                <td align="right">
                    <xsl:call-template name="display-time">
                        <xsl:with-param name="value" select="$minTime" />
                    </xsl:call-template>
                </td>
                <td align="right">
                    <xsl:call-template name="display-time">
                        <xsl:with-param name="value" select="$maxTime" />
                    </xsl:call-template>
                </td>
                <td align="center">
                   <a href="">
                      <xsl:attribute name="href"><xsl:text/>javascript:change('page_details_<xsl:value-of select="position()" />')</xsl:attribute>
                      <img src="expand.png" alt="expand/collapse"><xsl:attribute name="id"><xsl:text/>page_details_<xsl:value-of select="position()" />_image</xsl:attribute></img>                      
                   </a>
                </td>
            </tr>
            
                        <tr class="page_details">
                           <xsl:attribute name="id"><xsl:text/>page_details_<xsl:value-of select="position()" /></xsl:attribute>
                           <td colspan="8" bgcolor="#FF0000">
                              <div align="center">
                     <b>Details for Page "<xsl:value-of select="$label" />"</b>
                     <table bordercolor="#000000" bgcolor="#2674A6" border="0"  cellpadding="1" cellspacing="1" width="95%">
                     <tr>
                        <th>Thread</th>
                        <th>Iteration</th>
                        <th>Time (milliseconds)</th>
                        <th>Bytes</th>
                        <th>Success</th>
                     </tr>
                                      
                     <xsl:for-each select="../*[@lb = $label and @tn != $label]">                                             
                        <tr>
                           <td><xsl:value-of select="@tn" /></td>
                           <td align="center"><xsl:value-of select="position()" /></td>
                           <td align="right"><xsl:value-of select="@t" /></td>
                           <!--  TODO allow for missing bytes field -->
                           <td align="right"><xsl:value-of select="@by" /></td>
                           <td align="center"><xsl:value-of select="@s" /></td>
                        </tr>
                     </xsl:for-each>
                     
                     </table>
                  </div>
                           </td>
                        </tr>
            
        </xsl:for-each>
    </table>
</xsl:template>

<xsl:template name="detail">
    <xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" />

    <xsl:if test="$allFailureCount > 0">
        <h2>Failure Detail</h2>

        <xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]">

            <xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" />

            <xsl:if test="$failureCount > 0">
                <h3><xsl:value-of select="@lb" /><a><xsl:attribute name="name"><xsl:value-of select="@lb" /></xsl:attribute></a></h3>

                <table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
                <tr valign="top">
                    <th>Response</th>
                    <th>Failure Message</th>
                    <xsl:if test="$showData = 'y'">
                       <th>Response Data</th>
                    </xsl:if>
                </tr>
            
                <xsl:for-each select="/testResults/*[@lb = current()/@lb][attribute::s='false']">
                    <tr>
                        <td><xsl:value-of select="@rc | @rs" /> - <xsl:value-of select="@rm" /></td>
                        <td><xsl:value-of select="assertionResult/failureMessage" /></td>
                        <xsl:if test="$showData = 'y'">
                            <td><xsl:value-of select="./binary" /></td>
                        </xsl:if>
                    </tr>
                </xsl:for-each>
                
                </table>
            </xsl:if>

        </xsl:for-each>
    </xsl:if>
</xsl:template>

<xsl:template name="min">
    <xsl:param name="nodes" select="/.." />
    <xsl:choose>
        <xsl:when test="not($nodes)">NaN</xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$nodes">
                <xsl:sort data-type="number" />
                <xsl:if test="position() = 1">
                    <xsl:value-of select="number(.)" />
                </xsl:if>
            </xsl:for-each>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="max">
    <xsl:param name="nodes" select="/.." />
    <xsl:choose>
        <xsl:when test="not($nodes)">NaN</xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$nodes">
                <xsl:sort data-type="number" order="descending" />
                <xsl:if test="position() = 1">
                    <xsl:value-of select="number(.)" />
                </xsl:if>
            </xsl:for-each>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="display-percent">
    <xsl:param name="value" />
    <xsl:value-of select="format-number($value,'0.00%')" />
</xsl:template>

<xsl:template name="display-time">
    <xsl:param name="value" />
    <xsl:value-of select="format-number($value,'0 ms')" />
</xsl:template>
    
</xsl:stylesheet>

14、在build.xml目录,执行

cd /usr/local/apache-jmeter-5.2.1/entry_name
ant
或者
ant -buildfile build.xml

15、查看执行结果及邮件

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值