jmeter jmx还是调用java_尝试使用JMeter API生成JMeter测试计划(jmx):从代码创建的jmeter jmx文件与JMeter创建的jmeter jmx文件不匹配...

小编典典

最后,在研究了jmeter源代码之后,我发现除了我在做什么之外,我还需要显式设置guiClass和testClass参数。

testPlan.setProperty(TestElement.TEST_CLASS,TestPlan.class.getName());

testPlan.setProperty(TestElement.GUI_CLASS,TestPlanGui.class.getName());

类似地,对于其他测试元素,例如ThreadGroup,JavaSampler等。

完整的代码如下,

package com.test;

import java.io.FileOutputStream;

import org.apache.jmeter.control.LoopController;

import org.apache.jmeter.control.gui.LoopControlPanel;

import org.apache.jmeter.control.gui.TestPlanGui;

import org.apache.jmeter.protocol.java.control.gui.JavaTestSamplerGui;

import org.apache.jmeter.protocol.java.sampler.JavaSampler;

import org.apache.jmeter.save.SaveService;

import org.apache.jmeter.testelement.TestElement;

import org.apache.jmeter.testelement.TestPlan;

import org.apache.jmeter.threads.ThreadGroup;

import org.apache.jmeter.threads.gui.ThreadGroupGui;

import org.apache.jmeter.util.JMeterUtils;

import org.apache.jorphan.collections.HashTree;

public class JMXCreator {

public static void main(String[] argv) throws Exception {

// Initialize the configuration variables

String jmeterHome = "D:\\apache-jmeter-2.11";

JMeterUtils.setJMeterHome(jmeterHome);

JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir()

+ "\\jmeter.properties");

JMeterUtils.initLogging();

JMeterUtils.initLocale();

// TestPlan

TestPlan testPlan = new TestPlan();

testPlan.setName("Test Plan");

testPlan.setEnabled(true);

testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());

testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());

// ThreadGroup controller

LoopController loopController = new LoopController();

loopController.setEnabled(true);

loopController.setLoops(5);

loopController.setProperty(TestElement.TEST_CLASS,

LoopController.class.getName());

loopController.setProperty(TestElement.GUI_CLASS,

LoopControlPanel.class.getName());

// ThreadGroup

ThreadGroup threadGroup = new ThreadGroup();

threadGroup.setName("Thread Group");

threadGroup.setEnabled(true);

threadGroup.setSamplerController(loopController);

threadGroup.setNumThreads(5);

threadGroup.setRampUp(10);

threadGroup.setProperty(TestElement.TEST_CLASS,

ThreadGroup.class.getName());

threadGroup.setProperty(TestElement.GUI_CLASS,

ThreadGroupGui.class.getName());

// JavaSampler

JavaSampler javaSampler = new JavaSampler();

javaSampler.setClassname("my.example.sampler");

javaSampler.setEnabled(true);

javaSampler.setProperty(TestElement.TEST_CLASS,

JavaSampler.class.getName());

javaSampler.setProperty(TestElement.GUI_CLASS,

JavaTestSamplerGui.class.getName());

// Create TestPlan hash tree

HashTree testPlanHashTree = new HashTree();

testPlanHashTree.add(testPlan);

// Add ThreadGroup to TestPlan hash tree

HashTree threadGroupHashTree = new HashTree();

threadGroupHashTree = testPlanHashTree.add(testPlan, threadGroup);

// Add Java Sampler to ThreadGroup hash tree

HashTree javaSamplerHashTree = new HashTree();

javaSamplerHashTree = threadGroupHashTree.add(javaSampler);

// Save to jmx file

SaveService.saveTree(testPlanHashTree, new FileOutputStream(

"d:\\test.jmx"));

}

}

2020-07-24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值