我正在尝试使用JMeter版本2.12 API将我的JMeter测试计划转换为纯Java实现,但我没有运气成功执行测试。我的实现基于GUI测试中的测试计划.jmx文件,该文件在通过GUI运行时成功执行。我试图转换的测试是JMS Publisher向我的localhost上的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();
}
答案 0 :(得分:2)
根据上面列出的示例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();
}
答案 1 :(得分:1)
如果您有相关的.jmx文件,则可以按照5 Ways To Launch a JMeter Test without Using the JMeter GUI指南的4.2 Running an existing JMeter Test from Java code
章节以编程方式启动它。
如果您正在寻找一种纯粹用Java设计负载测试的方法,那么您的代码会遗漏一些重要的位,例如TestElement.TEST_CLASS
属性。 ThreadGroup也应该表示为HashTree。
请参阅JMeterFromScratch.java源代码以供参考。
希望这会有所帮助。