使用纯Java使用JMeter执行JMS压力测试

使用纯Java使用JMeter执行JMS压力测试,java,jms,jmeter,Java,Jms,Jmeter,我试图使用JMeter 2.12版API将我拥有的JMeter测试计划转换为纯Java实现,但我没有成功执行测试的机会。我的实现基于GUI测试中的test plan.jmx文件,该文件在通过GUI运行时会成功执行。我试图转换的测试是JMS发布者向本地主机上的ActiveMQ代理发送一条文本消息。我已经确认,当通过JMeterGUI执行时会收到消息,但我无法通过Java实现获得同样的成功。代码可以编译并运行,但我不确定JMS发布者为什么没有成功发送消息: public static void ma

我试图使用JMeter 2.12版API将我拥有的JMeter测试计划转换为纯Java实现,但我没有成功执行测试的机会。我的实现基于GUI测试中的test plan.jmx文件,该文件在通过GUI运行时会成功执行。我试图转换的测试是JMS发布者向本地主机上的ActiveMQ代理发送一条文本消息。我已经确认,当通过JMeterGUI执行时会收到消息,但我无法通过Java实现获得同样的成功。代码可以编译并运行,但我不确定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文件,您可以按照指南的
4.2运行Java代码中的现有JMeter测试以编程方式启动它

如果您正在寻找一种纯用Java设计负载测试的方法,那么您的代码就会缺少一些重要的内容,如
TestElement.test\u CLASS
property。此外,ThreadGroup应表示为HashTree

参考源代码


希望这有帮助

基于上面列出的示例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();
}

指向JMeterFromScratch.java的链接帮助很大。谢谢Dmitri,我可以让代码按我需要的方式工作。