gpt4 book ai didi

java - 使用纯Java用JMeter进行JMS压力测试

转载 作者:行者123 更新时间:2023-11-30 08:17:18 25 4
gpt4 key购买 nike

我正在尝试使用 JMeter 2.12 版 API 将我拥有的 JMeter 测试计划转换为纯 Java 实现,但我没有运气让测试成功执行。我的实现基于 GUI 测试中的测试计划 .jmx 文件,该文件在通过 GUI 运行时成功执行。我试图转换的测试是一个 JMS 发布者向我本地主机上的 ActiveMQ 代理发送一条文本消息。我已经确认通过 JMeter GUI 执行时会收到消息,但我无法通过 Java 实现取得同样的成功。代码编译并运行,但我不确定为什么 JMS Publisher 没有成功发送消息:

public static void main(String[] args) {
String jmeterLocation = "C:\\Users\\Andrew2\\Desktop\\apache-jmeter-2.12\\";

StandardJMeterEngine jmeter = new StandardJMeterEngine();

//Load JMeter properties
JMeterUtils.loadJMeterProperties(jmeterLocation + "bin\\jmeter.properties");
JMeterUtils.setJMeterHome(jmeterLocation);
JMeterUtils.initLocale();

HashTree testPlanTree = new HashTree();

//Build Sampler
PublisherSampler jmsPublisher = new PublisherSampler();
jmsPublisher.setProperty("jms.jndi_properties", "false");
jmsPublisher.setProperty("jms.initial_context_factory","org.apache.activemq.jndi.ActiveMQInitialContextFactory");
jmsPublisher.setProperty("jms.provider_url","tcp://127.0.0.1:61616");
jmsPublisher.setProperty("jms.connection_factory","ConnectionFactory");
jmsPublisher.setProperty("jms.topic","dynamicQueues/NewQueue");
jmsPublisher.setProperty("jms.expiration","100");
jmsPublisher.setProperty("jms.priority","6");
jmsPublisher.setProperty("jms.security_principle","");
jmsPublisher.setProperty("jms.security_credentials","");
jmsPublisher.setProperty("jms.text_message","test..test..");
jmsPublisher.setProperty("jms.input_file","");
jmsPublisher.setProperty("jms.random_path","");
jmsPublisher.setProperty("jms.config_choice","jms_use_text");
jmsPublisher.setProperty("jms.config_msg_type","jms_text_message");
jmsPublisher.setProperty("jms.iterations","1");
jmsPublisher.setProperty("jms.authenticate",false);
JMSProperties jmsProperties = new JMSProperties();//set header property
jmsProperties.addJmsProperty(new JMSProperty("TestID","123456","java.lang.String"));


//Build Result Collector so that results can be inspected after test
ResultCollector rc = new ResultCollector();
rc.setEnabled(true);
rc.setErrorLogging(false);
rc.isSampleWanted(true);
SampleSaveConfiguration ssc = new SampleSaveConfiguration();
ssc.setTime(false);
ssc.setLatency(false);
ssc.setTimestamp(true);
ssc.setSuccess(true);
ssc.setLabel(false);
ssc.setCode(false);
ssc.setMessage(false);
ssc.setThreadName(false);
ssc.setDataType(false);
ssc.setEncoding(false);
ssc.setAssertions(false);
ssc.setSubresults(false);
ssc.setResponseData(false);
ssc.setSamplerData(false);
ssc.setAsXml(false);
ssc.setFieldNames(false);
ssc.setResponseHeaders(false);
ssc.setRequestHeaders(false);
ssc.setAssertionResultsFailureMessage(false);
ssc.setThreadCounts(false);
rc.setSaveConfig(ssc);
rc.setFilename("C:\\Users\\Andrew2\\Desktop\\constantthroughput-singleserver.csv");

//Create Loop Controller
LoopController loopController = new LoopController();
loopController.setEnabled(true);
loopController.setLoops(3);
loopController.addTestElement(jmsPublisher);
loopController.setFirst(true);
loopController.initialize();

//Create Thread Group
SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setEnabled(true);
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);

//Create Test Plan
testPlanTree.add("testPlan",new TestPlan("JMeter JMS test"));
testPlanTree.add("loopController",loopController);
testPlanTree.add("JMS Publisher",jmsPublisher);
testPlanTree.add("Logger",rc);
testPlanTree.add("ThreadGroup",threadGroup);

//Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
}

最佳答案

基于上面列出的 JMeterFromScratch.java Dmitri 示例,我修改了我的代码,现在它可以工作了,工作代码是:

public static void main(String[] args) throws FileNotFoundException, IOException {
String jmeterLocation = "C:\\Users\\Andrew2\\Desktop\\apache-jmeter-2.12\\";

StandardJMeterEngine jmeter = new StandardJMeterEngine();

//Load JMeter properties
JMeterUtils.loadJMeterProperties(jmeterLocation + "bin/jmeter.properties");
JMeterUtils.setJMeterHome(jmeterLocation);
JMeterUtils.initLocale();

HashTree testPlanTree = new HashTree();

//Build Sampler
PublisherSampler jmsPublisher = new PublisherSampler();
jmsPublisher.setProperty("jms.jndi_properties", "false");
jmsPublisher.setProperty("jms.initial_context_factory","org.apache.activemq.jndi.ActiveMQInitialContextFactory");
jmsPublisher.setProperty("jms.provider_url","tcp://127.0.0.1:61616");
jmsPublisher.setProperty("jms.connection_factory","ConnectionFactory");
jmsPublisher.setProperty("jms.topic","dynamicQueues/NewQueue");
jmsPublisher.setProperty("jms.expiration","100");
jmsPublisher.setProperty("jms.priority","6");
jmsPublisher.setProperty("jms.security_principle","");
jmsPublisher.setProperty("jms.security_credentials","");
jmsPublisher.setProperty("jms.text_message","test..test..");
jmsPublisher.setProperty("jms.input_file","");
jmsPublisher.setProperty("jms.random_path","");
jmsPublisher.setProperty("jms.config_choice","jms_use_text");
jmsPublisher.setProperty("jms.config_msg_type","jms_text_message");
jmsPublisher.setProperty("jms.iterations","1");
jmsPublisher.setProperty("jms.authenticate",false);
JMSProperties jmsProperties = new JMSProperties();//set header property
jmsProperties.addJmsProperty(new JMSProperty("TestID","123456","java.lang.String"));


//Build Result Collector so that results can be inspected after test
ResultCollector rc = new ResultCollector();
rc.setEnabled(true);
rc.setErrorLogging(false);
rc.isSampleWanted(true);
SampleSaveConfiguration ssc = new SampleSaveConfiguration();
ssc.setTime(false);
ssc.setLatency(false);
ssc.setTimestamp(true);
ssc.setSuccess(true);
ssc.setLabel(false);
ssc.setCode(false);
ssc.setMessage(false);
ssc.setThreadName(false);
ssc.setDataType(false);
ssc.setEncoding(false);
ssc.setAssertions(false);
ssc.setSubresults(false);
ssc.setResponseData(false);
ssc.setSamplerData(false);
ssc.setAsXml(false);
ssc.setFieldNames(false);
ssc.setResponseHeaders(false);
ssc.setRequestHeaders(false);
ssc.setAssertionResultsFailureMessage(false);
ssc.setThreadCounts(false);
rc.setSaveConfig(ssc);
rc.setFilename("C:\\Users\\Andrew2\\Desktop\\constantthroughput-singleserver.csv");

//Create Loop Controller
LoopController loopController = new LoopController();
loopController.setEnabled(true);
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
loopController.initialize();

//Create Thread Group
SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setEnabled(true);
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());

//Create Test Plan
TestPlan testPlan = new TestPlan("New Test Plan");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

//Load elements into test plan
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(jmsPublisher);
threadGroupHashTree.add(rc);

SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterLocation + "bin/testjms.jmx"));

Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}

//Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
}

关于java - 使用纯Java用JMeter进行JMS压力测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27935394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com