You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于JMeter Aggregate Report统计信息生成自定义报告?

JMeter自定义简易HTML汇总报告实现方案

问题描述

JMeter自带的仪表盘报告过于详细,我希望生成一个包含执行参数和少量采样器统计表格的简易HTML汇总报告,要求在测试计划执行结束时自动生成一次。受《JMeter JSR223 Listener for ExtentReports needs improvement》启发,考虑用JSR223 Listener实现,但不清楚如何获取或重新计算Aggregate Report中的统计数据(如count、average、min、max、百分位数),同时也欢迎其他实现思路。


一、JSR223 Listener实现方案

1. 获取Aggregate Report统计数据

JMeter的聚合统计数据可以通过内置的AggregateConsumer类直接获取,无需重复计算,步骤如下:

  • 在JSR223 Listener中选择Groovy作为脚本语言(性能最优)
  • 通过JMeter上下文获取全局聚合统计实例,遍历采样器的统计结果:
import org.apache.jmeter.report.processor.AggregateConsumer
import org.apache.jmeter.report.processor.AggregatedSampleResult

// 获取Aggregate Report背后的统计处理器实例
def aggregateConsumer = ctx.getProperties().get("jmeter.report.processor.aggregate")

// 遍历所有采样器的聚合统计数据
aggregateConsumer.getResultMap().each { samplerLabel, aggregatedResult ->
    def count = aggregatedResult.getCount()       // 请求总数
    def average = aggregatedResult.getMean()      // 平均响应时间
    def min = aggregatedResult.getMin()           // 最小响应时间
    def max = aggregatedResult.getMax()           // 最大响应时间
    def p90 = aggregatedResult.getPercentile90()  // 90%分位响应时间
    def p95 = aggregatedResult.getPercentile95()  // 95%分位响应时间
    
    // 可将数据存入变量,供后续生成HTML使用
    vars.put("${samplerLabel}_count", count.toString())
    // 其他指标同理存储
}

2. 生成HTML报告

在测试执行结束时(通过样本结果判断测试收尾事件),用Groovy拼接HTML内容并写入本地文件:

// 判断是否为测试结束的最后一个样本事件
if (sampleResult.isTestStarted() && sampleResult.isTestEnded()) {
    // 组装HTML结构与样式
    def htmlContent = """
    <html>
        <head>
            <title>简易测试汇总报告</title>
            <style>
                table {border-collapse: collapse; width: 80%; margin: 20px auto;}
                th, td {border: 1px solid #ddd; padding: 8px; text-align: center;}
                th {background-color: #f2f2f2;}
                .params {margin: 20px; padding: 15px; border-radius: 5px; border: 1px solid #eee;}
                h1 {text-align: center; color: #333;}
            </style>
        </head>
        <body>
            <h1>测试汇总报告</h1>
            <div class="params">
                <h3>执行参数</h3>
                <p>线程数: ${ctx.getThreadGroup().getNumThreads()}</p>
                <p>循环次数: ${ctx.getThreadGroup().getLoopCount()}</p>
                <p>开始时间: ${new Date(ctx.getStartTime())}</p>
                <p>结束时间: ${new Date()}</p>
            </div>
            <table>
                <tr>
                    <th>采样器名称</th>
                    <th>请求数</th>
                    <th>平均响应时间(ms)</th>
                    <th>最小响应时间(ms)</th>
                    <th>最大响应时间(ms)</th>
                    <th>90%响应时间(ms)</th>
                </tr>
    """
    
    // 遍历采样器统计数据,拼接表格行
    aggregateConsumer.getResultMap().each { samplerLabel, aggregatedResult ->
        htmlContent += """
                <tr>
                    <td>${samplerLabel}</td>
                    <td>${aggregatedResult.getCount()}</td>
                    <td>${Math.round(aggregatedResult.getMean())}</td>
                    <td>${aggregatedResult.getMin()}</td>
                    <td>${aggregatedResult.getMax()}</td>
                    <td>${Math.round(aggregatedResult.getPercentile90())}</td>
                </tr>
        """
    }
    
    // 补全HTML结尾并写入文件
    htmlContent += """
            </table>
        </body>
    </html>
    """
    def reportPath = "${System.getProperty('user.dir')}/custom_summary_report.html"
    new File(reportPath).write(htmlContent, "UTF-8")
}

注意:需将JSR223 Listener的作用域(Scope)设置为Test Plan,确保能捕获测试结束事件。


二、其他实现方法

1. 自定义Java监听器

若熟悉Java,可编写继承AbstractListenerElement的自定义监听器,重写testEnded方法,直接调用JMeter统计API获取数据,生成HTML。编译打包为JAR后放入JMeter的lib/ext目录即可使用。

2. CSV结果文件+脚本生成报告

  • 先用JMeter的Simple Data Writer输出详细的CSV测试结果
  • 测试结束后,用Python/Shell脚本读取CSV文件,计算所需统计指标,通过字符串拼接或模板生成HTML
  • 可通过JMeter的OS Process Sampler在测试最后一步自动调用脚本

3. 持续集成场景:JMeter API+模板引擎

在Maven/Ant构建脚本中调用JMeter API执行测试,获取统计数据后,用FreeMarker/Velocity等模板引擎生成标准化HTML报告,适合持续集成流水线自动生成报告。


内容的提问来源于stack exchange,提问作者Yves Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 12:35:37