性能测试工具 Jmeter Html 报告优化

南风 · 2016年09月12日 · 最后由 达峰的夏天 回复于 2020年04月08日 · 2187 次阅读

但是最近在查阅相关资料时,发现基本都是重复一篇文章Jmeter 使用笔记之 html 报告扩展(一),而且有很多看不明白的地方,于是根据自己需求,在报告中修改了一些,现在整理分享出来。

优化后效果图:

1. 邮件发送 html 报告有中文时,显示乱码:

修改 encoding 为 “GBK”

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

2. Summary 中的只标红 Failures 数:

  • 屏蔽 Summary 中 class 属性 <!-- <xsl:attribute name="class"> <xsl:choose> <xsl:when test="$allFailureCount &gt; 0">Failure</xsl:when> </xsl:choose> </xsl:attribute> -->
  • 修改 allFailureCount <td align="center"> <xsl:value-of select="$allSuccessCount" /> </td> <xsl:choose> <xsl:when test="$allFailureCount &gt; 0"> <td align="center" style="font-weight:bold"> <font color="red"> <xsl:value-of select="$allFailureCount" /> </font> </td> </xsl:when> <xsl:otherwise> <td align="center"> <xsl:value-of select="$allFailureCount" /> </td> </xsl:otherwise> </xsl:choose>

3. Pages 页面按 Average Time 倒序排序:

在 Pagelist 模板中 for-each 下添加

<xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]"   >
            <!-- 按平均时间排序 -->
            <xsl:sort select="sum(../*[@lb = current()/@lb]/@t) div count(../*[@lb = current()/@lb])" data-type="number" order="descending"/>

4. 接口 Average Time 超过 2s 标黄显示:

  • 添加 LongTime css
.Failure {
    font-weight:bold; color:red;
}
.LongTime {
    font-weight:bold; color:#ff9900;
}

  • Pagelist 模块中针对错误和超长时间接口标色显示
<tr valign="top">
    <xsl:choose>
        <!-- 失败用例标红显示 -->
        <xsl:when test="$failureCount &gt; 0">
            <xsl:attribute name="class">
                <xsl:choose>
                    <xsl:when test="$failureCount &gt; 0">Failure</xsl:when>
                </xsl:choose>
            </xsl:attribute>
        </xsl:when>
        <!-- 平均时间超过2s,标色显示 -->
        <xsl:when test="$averageTime &gt; 2000">
            <xsl:attribute name="class">
                <xsl:choose>
                    <xsl:when test="$averageTime &gt; 2000">LongTime</xsl:when>
                </xsl:choose>
            </xsl:attribute>
        </xsl:when>
    </xsl:choose>

5. 添加 90% Line 和 QPS:

  • 添加 90 %lineTime 模板
<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>

<!-- 90% line time -->
<xsl:template name="lineTime">
    <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" />
                <!-- last() 返回当前上下文中的最后一个节点位置数 -->
                <!-- ceiling(number) 返回大于number的最小整数 -->
                <!-- floor(number) 返回不大于number的最大整数 -->
                <!-- position() 返回当前节点位置的数字 -->
                <!-- number(object) 使对象转换成数字 -->
                <xsl:choose>
                    <!-- 当只有一个节点时,向上取整 -->
                    <xsl:when test="last() = 1">
                        <xsl:if test="position() = ceiling(last()*0.9)">
                            <xsl:value-of select="number(.)" />
                        </xsl:if>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:if test="position() = floor(last()*0.9)">
                            <xsl:value-of select="number(.)" />
                        </xsl:if>
                      </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

  • Sunmary 中添加标题
<tr valign="top">
    <th># Samples</th>
    <th>Success</th>
    <th>Failures</th>
    <th>Success Rate</th>
    <th>Average Time</th>
    <th>Min Time</th>
    <th>Max Time</th>
    <th>90% Line</th>
    <th>QPS</th>
</tr>
  • Summary 中添加 allLineTime 和 qps 变量
<xsl:variable name="allMaxTime">
    <xsl:call-template name="max">
        <xsl:with-param name="nodes" select="/testResults/*/@t" />
    </xsl:call-template>
</xsl:variable>
<!-- New add 90% line -->
<xsl:variable name="allLineTime">
    <xsl:call-template name="lineTime">
        <xsl:with-param name="nodes" select="/testResults/*/@t" />
    </xsl:call-template>
</xsl:variable>
<!-- 将毫秒转换成秒 -->
<xsl:variable name="qps" select="$allCount * 1000 div $allTotalTime"/>
  • Summary 中调用 allLineTime 和 qps 变量
<td align="center">
    <xsl:call-template name="display-time">
        <xsl:with-param name="value" select="$allMaxTime" />
    </xsl:call-template>
</td>
<td align="center">
    <xsl:call-template name="display-time">
        <xsl:with-param name="value" select="$allLineTime" />
    </xsl:call-template>
</td>
<td align="center">
    <xsl:call-template name="display-qps">
        <xsl:with-param name="value" select="$qps" />
    </xsl:call-template>
  • pagelist 中添加标题
<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>Success</th>
            <th>Failures</th>
            <th>Success Rate</th>
            <th>Average Time</th>
            <th>Min Time</th>
            <th>Max Time</th>
            <th>90% Line</th>
            <th>QPS</th>
            <th></th>
        </tr>
  • pagelist 中添加 allLineTime 和 qps 变量
<xsl:variable name="maxTime">
    <xsl:call-template name="max">
        <xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" />
    </xsl:call-template>
</xsl:variable>
<!-- new add 90% line time -->
<xsl:variable name="nintyTime">
    <xsl:call-template name="lineTime">
        <xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" />
    </xsl:call-template>
</xsl:variable>
<xsl:variable name="qpsTime" select="$count * 1000 div $totalTime"/>
  • pagelist 中调用 allLineTime 和 qps 变量
<td align="center">
    <xsl:call-template name="display-time">
        <xsl:with-param name="value" select="$maxTime" />
    </xsl:call-template>
</td>
<!-- Page页面添加90% LineTime -->
<td align="center">
    <xsl:call-template name="display-time">
        <xsl:with-param name="value" select="$nintyTime" />
    </xsl:call-template>
</td>
<td align="center">
    <xsl:call-template name="display-qps">
        <xsl:with-param name="value" select="$qpsTime" />
    </xsl:call-template>
</td>

6.Failure Detail 模块显示 Response Data:

  • 设置 showData 为 ‘y’
<!-- Defined parameters (overrideable) -->
<xsl:param    name="showData" select="'y'"/>
<xsl:param    name="titleReport" select="'Interface Test Results'"/>
<xsl:param    name="dateReport" select="'date not defined'"/>

  • 替换内容
<table class="details" border="0" cellpadding="5" cellspacing="2" width="95%">
<tr valign="top">
    <th align="center">Response</th>
    <th align="center">Failure Message</th>
    <xsl:if test="$showData = 'y'">
        <th align="left">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="responseData" /></td>
        </xsl:if>
    </tr>
</xsl:for-each>

7.添加 Host

<xsl:template name="pageHeader">
    <h1><xsl:value-of select="$titleReport" /></h1>
    <table width="100%">
        <tr>
            <!-- 获取requestHeader数据 -->
            <xsl:variable name="URL" select="/testResults/httpSample/requestHeader" />
            <!-- 从获取的URL中截取数据,第二代表起始位置,第三位代表长度 -->
            <xsl:variable name="newURL" select="substring($URL, 94, 30)" /> 
            <!-- 已换行符来截取,before代表换行符之前的数据 -->
            <xsl:variable name="remaining" select="substring-before($newURL, '&#xA;')" /> 
            <td align="left">Date report: <xsl:value-of select="$dateReport" /></td>
            <td align="center">Host: <xsl:value-of select="$remaining" /></td>
            <td align="right"><a href="./TestLog.html">测试日志</a></td>
        </tr>
    </table>

8.文件下载:jmeter-results-detail-report_30.xsl

共收到 14 条回复 时间 点赞

效果是什么样子的

Monkey 内容不符合版规屏蔽此话题 09月12日 18:18

请使用 markdown,不要用那么多图片

南风难得啊

5楼 已删除
南风 #11 · 2016年09月22日 Author

@Lihuazhang @seveniruby @monkey 格式有问题么?

匿名 #10 · 2016年09月28日

感谢分享,请问这个能加上 TPS 吗?

虽然没看懂,还是手动点赞一下💯

#7 楼 @h470789634 加的 QPS,跟 TPS 一个意思

#8 楼 @xiaozheng_QM 晓征,没看懂多看看

在 centos 服务器上执行 ant 的时候,为啥老是报错呢?
有大神给指点一下吗,在 mac 上没问题了。

BUILD FAILED
/data/www/jmeter/banner/build.xml:19: The following error occurred while executing this line:
/data/www/jmeter/banner/build.xml:24: taskdef class org.programmerplanet.ant.taskdefs.jmeter.JMeterTask cannot be found

贤若 回复

试试这个:在 jmeter 中找到 ant-jmeter-1.1.1.jar(在 extras 目录下),并将这些文件 copy 到 Ant 安装路径中 lib 文件夹中

@jaychang1989 版主,
Interface Test Results

Date report: date not defined 怎么配置取执行时 current-date() 时间呢?

基于 java-junit5 的 Macaca 测试报告。 https://testerhome.com/topics/22986

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册