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

如何通过Java代码调用JMeter API使用吞吐量整形定时器

Java调用JMeter API实现Throughput Shaping Timer配置方法

依赖准备

首先确保项目引入所需组件包,包含JMeter核心库、JMeter插件管理器下的Concurrency Thread Group(并发线程组)、Throughput Shaping Timer(吞吐量整形计时器)两个插件包,核心对应类如下:

  • 并发线程组:kg.apc.jmeter.threads.ConcurrencyThreadGroup
  • 吞吐量整形计时器:kg.apc.jmeter.timers.VariableThroughputTimer
  • 线程数动态调整用的__tstFeedback为插件内置函数,无需额外引入。

核心实现代码

import kg.apc.jmeter.timers.VariableThroughputTimer;
import kg.apc.jmeter.threads.ConcurrencyThreadGroup;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.util.ArrayList;
import java.util.List;

public class JmeterTstRunner {
    public static void main(String[] args) throws Exception {
        // 初始化JMeter运行环境,替换为本地JMeter安装路径
        String jmeterHome = "/your/local/jmeter/path";
        JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties");
        JMeterUtils.setJMeterHome(jmeterHome);
        JMeterUtils.initLocale();

        HashTree testPlanTree = new HashTree();

        // 1. 配置并发线程组,绑定tstFeedback函数
        ConcurrencyThreadGroup threadGroup = new ConcurrencyThreadGroup();
        threadGroup.setName("custom_concurrent_group");
        // 核心配置:参数依次为绑定的计时器名称、最小线程数、最大线程数、调度精度
        threadGroup.setNumThreads("${__tstFeedback(custom_tst_timer,5,200,10)}");
        threadGroup.setRampUp(5);
        threadGroup.setHold(300);
        // 配置循环控制器为永久循环,由计时器控制执行节奏
        LoopController loopController = new LoopController();
        loopController.setLoops(-1);
        loopController.setFirst(true);
        threadGroup.setSamplerController(loopController);

        // 2. 配置Throughput Shaping Timer负载规则
        VariableThroughputTimer tstTimer = new VariableThroughputTimer();
        tstTimer.setName("custom_tst_timer");
        // 负载规则每行三个参数:起始RPS、结束RPS、持续时长(秒)
        // 示例规则:5秒内从0升到100RPS,保持100RPS运行280秒,最后15秒降到0
        List<CollectionProperty> loadProfiles = new ArrayList<>();
        CollectionProperty profile1 = new CollectionProperty();
        profile1.addItem(0);
        profile1.addItem(100);
        profile1.addItem(5);
        CollectionProperty profile2 = new CollectionProperty();
        profile2.addItem(100);
        profile2.addItem(100);
        profile2.addItem(280);
        CollectionProperty profile3 = new CollectionProperty();
        profile3.addItem(100);
        profile3.addItem(0);
        profile3.addItem(15);
        loadProfiles.add(profile1);
        loadProfiles.add(profile2);
        loadProfiles.add(profile3);
        tstTimer.setData(new CollectionProperty("load_profile", loadProfiles));

        // 3. 组装测试计划结构
        TestPlan testPlan = new TestPlan("tst_test_plan");
        testPlan.setUserDefinedVariables(new Arguments());
        testPlanTree.add(testPlan);
        HashTree groupNode = testPlanTree.add(testPlan, threadGroup);
        // 将计时器挂载到线程组下,通过名称和__tstFeedback函数自动关联
        groupNode.add(tstTimer);
        // 可在此处继续添加自定义采样器、后置处理器、断言等业务组件

        // 4. 启动执行
        StandardJMeterEngine engine = new StandardJMeterEngine();
        engine.configure(testPlanTree);
        engine.run();
    }
}

关键配置说明

  • __tstFeedback函数的第一个参数必须和吞吐量整形计时器的name属性完全一致,否则无法完成线程数动态调整,吞吐量控制失效。
  • 若需要接收外部多行输入动态生成负载规则,只需将外部输入解析为[起始RPS, 结束RPS, 持续时长]格式的集合,替换代码中硬编码的loadProfiles内容即可。
  • 线程组不要设置固定线程数,必须通过__tstFeedback函数动态生成,否则无法根据实际吞吐量自动增减线程。
  • 循环控制器需设置为永久循环,否则固定循环次数会导致计时器调度逻辑提前终止。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.01 00:01:37