Run jmx file using java

六眼飞鱼酱① 提交于 2019-12-13 05:25:17

问题


I created a jmx file using java code. But when i tried to execute the jmx file using java, it throws the exception. Pls help me.. I have added all the jars. (Error in NonGUIDriver java.lang.IllegalArgumentException: Problem loading XML from:'/home/ksahu/MyScreenshots/k.jmx', conversion error com.thoughtworks.xstream.converters.ConversionException: null : null)

import org.apache.jmeter.engine.StandardJMeterEngine;
    import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
    import org.apache.jmeter.save.SaveService;
    import org.apache.jmeter.util.JMeterUtils;
    import org.apache.jorphan.collections.HashTree;

    import java.io.FileInputStream;

    public class RunJMXfile {

        public static void main(String[] argv) throws Exception {
            // JMeter Engine
            StandardJMeterEngine jmeter = new StandardJMeterEngine();


            // Initialize Properties, logging, locale, etc.
            JMeterUtils.loadJMeterProperties("/home/ksahu/apache-jmeter-2.13/bin/jmeter.properties");
            JMeterUtils.setJMeterHome("/home/ksahu/apache-jmeter-2.13");
            JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
            JMeterUtils.initLocale();

            // Initialize JMeter SaveService
            SaveService.loadProperties();



            // Load existing .jmx Test Plan
            FileInputStream in = new FileInputStream("/home/ksahu/MyScreenshots/k.jmx");
            HashTree testPlanTree = SaveService.loadTree(in);
            in.close();

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

This is the code that i have used to generate the jmx file

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.SetupThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;


public class jmeterTesting {

    public static void main(String[] args) throws FileNotFoundException, IOException{
        // Engine
        StandardJMeterEngine jm = new StandardJMeterEngine();
        JMeterUtils.setJMeterHome("/home/ksahu/apache-jmeter-2.13");

        // jmeter.properties
        JMeterUtils.loadJMeterProperties("/home/ksahu/apache-jmeter-2.13/bin/jmeter.properties");

        HashTree hashTree = new HashTree();     

        // HTTP Sampler
        HTTPSampler httpSampler = new HTTPSampler();
        httpSampler.setDomain("www.google.com");
        httpSampler.setPort(80);
        httpSampler.setPath("/");
        httpSampler.setMethod("GET");

        // Loop Controller
        TestElement loopCtrl = new LoopController();
        ((LoopController)loopCtrl).setLoops(1);
        ((LoopController)loopCtrl).addTestElement(httpSampler);
        ((LoopController)loopCtrl).setFirst(true);

        // Thread Group
        SetupThreadGroup threadGroup = new SetupThreadGroup();
        threadGroup.setNumThreads(1);
        threadGroup.setRampUp(1);
        threadGroup.setSamplerController((LoopController)loopCtrl);

        // Test plan
        TestPlan testPlan = new TestPlan("MY TEST PLAN");

        hashTree.add("testPlan", testPlan);
        hashTree.add("loopCtrl", loopCtrl);
        hashTree.add("threadGroup", threadGroup);
        hashTree.add("httpSampler", httpSampler);       
        jm.configure(hashTree);



        jm.run();
        System.out.println(hashTree);
        SaveService.saveTree(hashTree,new FileOutputStream("/home/ksahu/MyScreenshots/k.jmx"));
    }
}

回答1:


Try to open your /home/ksahu/MyScreenshots/k.jmx in JMeter GUI. If it does not open - there is a problem with the code, you generated the JMX file with. In that case update your question with the code, you used to create the k.jmx file.

See Chapter 4. RUN A JMETER TEST THROUGH A PROGRAM (FROM JAVA CODE) of the Five Ways To Launch a JMeter Test without Using the JMeter GUI for details.

Also there is a sample project which you can use as a reference: https://bitbucket.org/blazemeter/jmeter-from-code/




回答2:


You need to change the text "org.apache.jorphan.collections.HashTree" in the JMX generated using java to "hashTree". Open the JMX in any text editor and do the replace as mentioned. If that doesn't suffice, include the below step.

You need to set TestElement.ENABLED, TestElement.TEST_CLASS and TestElement.GUI_CLASS explicitly for each element. For example a sampler can be defined as below.

 HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
        httpSampler.setDomain(DOMAIN);
        httpSampler.setPort(PORT);
        httpSampler.setPath(PATH);
        httpSampler.setMethod(METHOD);
        httpSampler.addArgument("", "${domain}");
        httpSampler.setProperty(TestElement.ENABLED, true);
        httpSampler.setResponseTimeout("20000");
        httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
        httpSampler .setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


来源:https://stackoverflow.com/questions/35546843/run-jmx-file-using-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!