【问题标题】:Throughput Controller automation in JMeter test?JMeter测试中的吞吐量控制器自动化?
【发布时间】:2020-01-22 10:25:05
【问题描述】:

我正在开展一个项目,我必须将 JMeter 测试作为后端服务自动化。我的测试计划是这样的

Thread Group
 -Throughput Controller1
       --sampler1

 -Throughput controller2
       --sampler2

我的一个示例吞吐量实现是这样的。在这里,我正在做的是最初将线程数设置为 10,然后我想将其控制为 3 和其他 7 的线程数,用于另一个采样器。

TestPlan testPlan = new TestPlan();
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setRampUp(1);
threadGroup.setNumThreads(10);

ThroughputController throughputController = new ThroughputController();
throughputController.setComment("Through Put");
throughputController.setName("Through put");
throughputController.setMaxThroughput(3);
throughputController.setStyle(1);
   :
   :
   :
HTTPSamplerProxy examplecomSampler = loadTestSampler(urlDetails);

ListedHashTree testPlanTree = new ListedHashTree();
HashTree tpConfig = testPlanTree.add(testPlan);
HashTree tgConfig = tpConfig.add(threadGroup);
tgConfig.add(throughputController);
tgConfig.add(examplecomSampler)

当我运行这个时,所有 10 个线程都调用了 examplecomSampler。但我想将其限制为 3 和其他 7 到另一个采样器为什么会这样?

谢谢。

【问题讨论】:

    标签: controller jmeter throughput


    【解决方案1】:

    我不认为你初始化吞吐量控制器的方式是正确的,而且如果没有看到你的类的完整代码,很难说还有什么问题。

    请注意,您应该使用 JMeter GUI 创建 JMeter 测试计划,官方不支持其他方式,但是您可以尝试使用包装工具,例如 Taurus

    一般JMeter jmx scripts 基本上是 XML 文件,因此您可以将使用 JMeter API 生成的测试计划与来自 JMeter GUI 的测试计划进行比较,找出差异并修改您的代码以消除它们。


    如果您仍想进入 JMeter 脚本的编程创建方向,一个示例测试计划具有 2 个吞吐量控制器和 2 个采样器,分别配置为 30% 和 70% 的执行,如下所示:

    HashTree testPlanTree = new HashTree();
    
    HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
    examplecomSampler.setDomain("example.com");
    examplecomSampler.setPort(80);
    examplecomSampler.setPath("/");
    examplecomSampler.setMethod("GET");
    examplecomSampler.setName("Open example.com");
    examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
    examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
    
    HTTPSamplerProxy blazemetercomSampler = new HTTPSamplerProxy();
    blazemetercomSampler.setDomain("blazemeter.com");
    blazemetercomSampler.setPort(80);
    blazemetercomSampler.setPath("/");
    blazemetercomSampler.setMethod("GET");
    blazemetercomSampler.setName("Open blazemeter.com");
    blazemetercomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
    blazemetercomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
    
    LoopController loopController = new LoopController();
    loopController.setLoops(1);
    loopController.setFirst(true);
    loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
    loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
    loopController.initialize();
    
    ThreadGroup threadGroup = new ThreadGroup();
    threadGroup.setName("Example Thread Group");
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController(loopController);
    threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
    threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
    
    ThroughputController throughputController3 = new ThroughputController();
    throughputController3.setComment("Through Put 3");
    throughputController3.setName("Through put 3");
    FloatProperty throughput3 = new FloatProperty();
    throughput3.setName("ThroughputController.percentThroughput");
    throughput3.setValue(30.0f);
    throughputController3.setProperty(throughput3);
    throughputController3.setProperty("ThroughputController.style", 1);
    throughputController3.setProperty("ThroughputController.perThread", false);
    throughputController3.setProperty(TestElement.TEST_CLASS, ThroughputController.class.getName());
    throughputController3.setProperty(TestElement.GUI_CLASS, ThroughputControllerGui.class.getName());
    
    ThroughputController throughputController7 = new ThroughputController();
    throughputController7.setComment("Through Put 7");
    throughputController7.setName("Through put 7");
    FloatProperty throughput7 = new FloatProperty();
    throughput7.setName("ThroughputController.percentThroughput");
    throughput7.setValue(70.0f);
    throughputController7.setProperty(throughput7);
    throughputController7.setProperty("ThroughputController.style", 1);
    throughputController7.setProperty("ThroughputController.perThread", false);
    throughputController7.setProperty(TestElement.TEST_CLASS, ThroughputController.class.getName());
    throughputController7.setProperty(TestElement.GUI_CLASS, ThroughputControllerGui.class.getName());
    
    HashTree logicHashTree = new HashTree();
    
    HashTree throughputController3Tree = new HashTree();
    HashTree exampleComHashTree = new HashTree();
    
    exampleComHashTree.add(examplecomSampler);
    throughputController3Tree.add(throughputController3);
    throughputController3Tree.add(throughputController3, exampleComHashTree);
    
    HashTree throughputController7Tree = new HashTree();
    HashTree blazemeterComHashTree = new HashTree();
    
    blazemeterComHashTree.add(blazemetercomSampler);
    throughputController7Tree.add(throughputController7);
    throughputController7Tree.add(throughputController7, blazemeterComHashTree);
    
    
    logicHashTree.add(throughputController3Tree);
    logicHashTree.add(throughputController7Tree);
    
    TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
    testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
    testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
    testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
    
    testPlanTree.add(testPlan);
    HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
    threadGroupHashTree.add(logicHashTree);
    

    查看Five Ways To Launch a JMeter Test without Using the JMeter GUI 文章,了解有关启动 JMeter 测试的各种方法的更多信息,包括使用 Java 语言从头开始创建一个。

    【讨论】:

    • 根据您的代码 sn-p 我的 ThroughPut 控制器代码是错误的。让我纠正一下。您能否分享您引用的源链接?
    • 另一个问题是 FlotProperty 是一个抽象类。所以它不能像这样启动
    • doesn't look like an abstract class to me,而且它还有constructor。如果您不太熟悉 Java,我建议您使用 Taurus 使用简单的 YAML 语法构建 JMeter 脚本。
    • 我明白了。这里 float 属性与 JMeter 一起出现。所以它不是抽象的。感谢您的帮助,我也完成了任务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多