使用纯 Java 使用 JMeter 执行 JMS 压力测试
Performing JMS stress test with JMeter using pure Java
我正在尝试使用 JMeter 版本 2.12 API 将我拥有的 JMeter 测试计划转换为纯 Java 实现,但我没有运气让测试成功执行。我的实现基于来自 GUI 测试的测试计划 .jmx 文件,当 运行 通过 GUI 时它确实执行成功。我试图转换的测试是一个 JMS 发布者向我本地主机上的 ActiveMQ 代理发送一条文本消息。我已经确认通过 JMeter GUI 执行时会收到消息,但我无法通过 Java 实现取得同样的成功。代码编译并 运行s,但我不确定为什么 JMS 发布器没有成功发送消息:
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();
}
如果您有相关的 .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 源代码以供参考。
希望这对您有所帮助。
基于上面列出的示例 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();
}
我正在尝试使用 JMeter 版本 2.12 API 将我拥有的 JMeter 测试计划转换为纯 Java 实现,但我没有运气让测试成功执行。我的实现基于来自 GUI 测试的测试计划 .jmx 文件,当 运行 通过 GUI 时它确实执行成功。我试图转换的测试是一个 JMS 发布者向我本地主机上的 ActiveMQ 代理发送一条文本消息。我已经确认通过 JMeter GUI 执行时会收到消息,但我无法通过 Java 实现取得同样的成功。代码编译并 运行s,但我不确定为什么 JMS 发布器没有成功发送消息:
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();
}
如果您有相关的 .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 源代码以供参考。
希望这对您有所帮助。
基于上面列出的示例 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();
}