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

JMeter中Concurrency Thread Group如何获取初始测试预计时长或结束时间

JMeter Concurrency Thread Group场景下末尾特定异常过滤方案

1. JSR223元件选择说明

  • 优先选择JSR223断言:可以直接操作SampleResult对象,支持修改采样结果状态、控制是否写入结果文件,完全匹配过滤错误日志的需求
  • 若仅需输出自定义日志不修改结果状态,JSR223后置处理器也可使用,两者核心逻辑无差异

2. Concurrency Thread Group获取预计结束时间方案

默认getDuration()方法仅支持JMeter原生线程组,Concurrency Thread Group作为第三方插件没有实现该接口。结合你已搭配Throughput Shaping Timer(TST)做动态吞吐量的场景,可直接从TST配置中提取测试总时长,无需硬编码:

2.1 测试启动时计算阈值时间

新增setUp线程组,添加1个JSR223采样器,填入以下代码,测试启动时仅执行一次:

import com.blazemeter.jmeter.timer.ThroughputShapingTimer

// 替换为你自己的Throughput Shaping Timer元件名称
def tstName = "吞吐量整形定时器"
def tst = ctx.getTestPlan().getToplevelNodes().find { 
    it instanceof ThroughputShapingTimer && it.getName() == tstName
}

// 计算TST配置的所有阶段总时长,单位:秒
def totalTestDuration = tst.getSchedule().sum { it.getDuration() }
// 测试实际开始时间戳,单位:毫秒
def testStartTime = ctx.getTestPlan().getStartTime()
// 预计结束时间 = 开始时间 + 总时长转换为毫秒
def expectedEndTime = testStartTime + totalTestDuration * 1000
// 过滤阈值:结束时间前推2分钟
def filterThreshold = expectedEndTime - 2 * 60 * 1000

// 存入全局属性,所有线程可访问
props.put("log_filter_threshold", filterThreshold)

若你的Concurrency Thread Group单独配置了额外的收尾等待时间,可在totalTestDuration中累加该时间即可

3. 异常过滤核心逻辑

在需要过滤异常的采样器下添加JSR223断言,填入以下代码:

// 仅处理失败的采样
if (!SampleResult.isSuccessful()) {
    def errorInfo = SampleResult.getResponseMessage() + SampleResult.getResponseDataAsString()
    // 匹配需要过滤的SocketException异常
    if (errorInfo.contains("java.net.SocketException")) {
        long currentTime = System.currentTimeMillis()
        long threshold = props.get("log_filter_threshold") as long
        // 当前时间已进入最后2分钟,执行过滤
        if (currentTime > threshold) {
            // 方案1:将结果改为成功,不会记录错误日志
            SampleResult.setSuccessful(true)
            // 方案2:保留错误状态但不写入结果文件,二选一即可
            // SampleResult.setIgnore()
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 10:15:03