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

如何合并K8s环境下多Pod运行的Gatling负载测试报告?

合并K8s中多个Gatling负载测试Pod报告的方法

核心逻辑

Gatling报告合并的本质是把多个测试实例生成的simulation.log及统计数据整合后,重新生成统一的HTML报告。虽然官方移除了gatling.sh的合并命令,但可以直接调用Gatling的Java API实现,或者封装自定义脚本完成这个流程。

具体步骤

1. 收集所有Pod的Gatling结果文件

每个Gatling Pod运行结束后,默认会在target/gatling/<simulation-name>-<timestamp>目录生成关键文件:

  • simulation.log:包含所有请求细节的核心测试日志
  • js/stats.json:预计算的统计数据(可选,能加快合并速度)
  • 其他如index.html这类报告文件不需要保留,合并后会重新生成

在K8s环境中,可通过以下方式收集这些文件:

  • 配置Pod完成后将结果目录上传到共享存储(比如NFS、S3或PVC)
  • 用kubectl cp批量从已完成的Pod中复制结果目录到本地或统一存储节点

2. 用Gatling Java API编写合并工具

写个简单的Java程序,调用Gatling的报告生成API来合并多个结果集。

Maven依赖配置

确保pom.xml里包含Gatling图表依赖:

<dependency>
    <groupId>io.gatling.highcharts</groupId>
    <artifactId>gatling-charts-highcharts</artifactId>
    <version>3.12.0</version>
    <scope>compile</scope>
</dependency>

合并代码示例

import io.gatling.charts.report.ReportsGenerator;
import io.gatling.core.config.GatlingConfiguration;
import io.gatling.core.stats.writer.LogFileReader;
import io.gatling.core.stats.writer.RunMessage;
import io.gatling.core.stats.writer.UserMessage;
import io.gatling.core.stats.writer.RequestMessage;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class GatlingReportMerger {
    public static void main(String[] args) {
        // 加载Gatling配置
        GatlingConfiguration config = GatlingConfiguration.load();
        
        // 收集所有待合并的结果目录
        List<Path> resultDirs = new ArrayList<>();
        resultDirs.add(Paths.get("/path/to/pod1-results"));
        resultDirs.add(Paths.get("/path/to/pod2-results"));
        // 添加更多结果目录...
        
        // 指定合并报告的输出路径
        Path outputDir = Paths.get("/path/to/merged-report");
        
        // 初始化报告生成器
        ReportsGenerator reportGenerator = new ReportsGenerator(config, outputDir);
        
        // 遍历所有结果目录,读取并合并simulation.log
        for (Path dir : resultDirs) {
            Path logFile = dir.resolve("simulation.log");
            LogFileReader reader = new LogFileReader(logFile, config.core().charset());
            
            reader.read(message -> {
                if (message instanceof RunMessage) {
                    reportGenerator.onRunMessage((RunMessage) message);
                } else if (message instanceof UserMessage) {
                    reportGenerator.onUserMessage((UserMessage) message);
                } else if (message instanceof RequestMessage) {
                    reportGenerator.onRequestMessage((RequestMessage) message);
                }
            });
        }
        
        // 生成合并后的HTML报告
        reportGenerator.generateFor(null);
        System.out.println("合并报告已生成至: " + outputDir);
    }
}

3. 封装成可执行工具或K8s Job

把上面的Java程序打包成可执行JAR后:

  • 本地运行JAR,传入结果目录参数生成合并报告
  • 或者创建专门的K8s Job,挂载共享存储中的所有结果目录,运行JAR生成报告后保存到指定位置

4. 替代方案:扩展Maven插件自定义目标

如果用Maven,可以扩展gatling-maven-plugin添加自定义合并目标。比如在pom.xml里配置:

<plugin>
    <groupId>io.gatling</groupId>
    <artifactId>gatling-maven-plugin</artifactId>
    <version>4.9.6</version>
    <executions>
        <execution>
            <id>merge-reports</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <runMultipleSimulations>true</runMultipleSimulations>
                <reportsOnly>${gatling.results.directory}</reportsOnly>
            </configuration>
        </execution>
    </executions>
</plugin>

注意:runMultipleSimulations参数允许处理多个模拟结果,需要确保所有结果目录都在reportsOnly指定的路径下。

注意事项

  • 所有待合并的测试必须使用相同的模拟类名称,否则合并报告可能出现数据不一致
  • 合并前检查所有simulation.log的完整性,避免文件损坏导致报告生成失败
  • K8s环境中建议用持久化存储统一管理测试结果,减少手动复制文件的麻烦

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 13:27:09