java - 使用纯Java通过JMeter执行JMS压力测试

标签 java jms jmeter

我正在尝试使用 JMeter 版本 2.12 API 将我拥有的 JMeter 测试计划转换为纯 Java 实现,但我没有成功执行测试。我的实现基于 GUI 测试中的测试计划 .jmx 文件,该文件在通过 GUI 运行时确实成功执行。我尝试转换的测试是 JMS 发布者向我的本地主机上的 ActiveMQ 代理发送一条文本消息。我已经确认通过 JMeter GUI 执行时会收到该消息,但我无法通过 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();
}

最佳答案

基于上面列出的 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/

相关文章:

jakarta-ee - 我可以使用哪些 maven 依赖项为 Glassfish 创建独立的 JMS 客户端?

regex - 响应变量 JMETER

rest - JMeter : How to handle asynchronous http post request

java - JPA RollbackException 但不在单元测试中

java - 给我一个 Oracle Streams 的简单示例?

java - 私有(private)构造函数对象初始化

java - JMS Bridge 现有队列消息

java - JMeter 以警告消息启动

java - 如何在 REST Web 服务的上下文中调用 spark 作业?

java - JOptionPane 中的 Inluce 方法