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

如何提取JMeter日志中指定INFO级耗时数据并输出到监听器/CSV/HTML报告

解决方案

方法1:修改Selenium JUnit代码直接关联JMeter上下文(优先推荐,稳定性最高)

  • 首先在你的Selenium项目中引入JMeter对应版本的依赖包ApacheJMeter_core.jar,该包可以在JMeter 5.4.1安装目录的lib/ext路径下获取,只需要编译时引入即可,运行时JMeter会自动加载相关类
  • 修改你原有的耗时统计代码,调用JMeter内置的上下文工具将耗时、limsId等自定义值存入JMeter变量,修改后代码参考如下:
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
// 你原有代码的其他导入

log.info(threadName + "::  limsId value is: " + limsId);
long endTime = System.currentTimeMillis();
long timeElapsed = endTime - startTime;
log.info(threadName + "::  Execution time in milliseconds: " + timeElapsed);

// 新增部分:将自定义值写入JMeter上下文
JMeterVariables vars = JMeterContextService.getContext().getVariables();
vars.put("search_elapsed_time", String.valueOf(timeElapsed));
vars.put("lims_id", limsId);

driver.close();
  • 将修改后的Selenium项目重新导出为JAR包,放入JMeter安装目录的lib/junit路径下,重启JMeter
  • 打开JMeter安装目录下bin/jmeter.properties文件,找到sample_variables配置项,修改为sample_variables=search_elapsed_time,lims_id,保存后重启JMeter
  • 后续运行测试时,这两个自定义值会自动写入JTL/CSV结果文件,也可以直接在「查看结果树」「汇总报告」等监听器中展示

方法2:不修改Selenium源码,通过后置处理器提取日志值

如果不方便修改Selenium的JAR包可以使用该方案,注意高并发场景下可能存在日志写入延迟导致提取失败的问题:

  • 给JUnit Request Sampler添加一个「JSR223后置处理器」
  • 在处理器中写入如下Groovy代码,匹配当前线程对应的日志值并存为JMeter变量:
// 获取当前线程名称
def threadName = ctx.getThread().getThreadName()
// 定义匹配规则
def timeMatch = (threadName + /::  Execution time in milliseconds: (\d+)/) as Pattern
def limsMatch = (threadName + /::  limsId value is: (\w+)/) as Pattern

// 读取JMeter日志文件匹配对应值
def logFile = new File(JMeterUtils.getJMeterHome() + "/bin/jmeter.log")
logFile.eachLine { line ->
    def timeRes = line =~ timeMatch
    def limsRes = line =~ limsMatch
    if(timeRes.find()) vars.put("search_elapsed_time", timeRes[0][1])
    if(limsRes.find()) vars.put("lims_id", limsRes[0][1])
}
  • 同样按照方法1的步骤配置sample_variables即可实现自定义值写入结果文件

自定义HTML报告配置

生成HTML报告时,自定义变量会自动包含在报告的自定义字段区域,你也可以直接通过如下命令生成包含该中间结果的报告:

jmeter -g 测试结果文件.jtl -o 报告输出目录

如果需要将耗时字段加入报告的统计维度,可以修改bin/user.properties中的报告相关配置即可实现。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 08:54:03