使用java运行jmx文件

时间:2016-02-22 06:09:51

标签: jmeter jmx

我使用java代码创建了一个jmx文件。但是当我尝试使用java执行jmx文件时,它会抛出异常。请帮助我..我已经添加了所有的罐子。 (NonGUIDriver中的错误java.lang.IllegalArgumentException:从以下问题加载XML时出现问题:&#39; /home/ksahu/MyScreenshots/k.jmx' ;,转换错误com.thoughtworks.xstream.converters.ConversionException:null:null)< / p>

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();
        }
    }

这是我用来生成jmx文件的代码

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"));
    }
}

2 个答案:

答案 0 :(得分:1)

尝试在JMeter GUI中打开/home/ksahu/MyScreenshots/k.jmx。如果它没有打开 - 代码有问题,你生成了JMX文件。在这种情况下,使用代码更新您的问题,您曾用于创建k.jmx文件。

有关详细信息,请参阅Five Ways To Launch a JMeter Test without Using the JMeter GUI的第4. RUN A JMETER TEST THROUGH A PROGRAM (FROM JAVA CODE)章。

还有一个示例项目,您可以将其用作参考:https://bitbucket.org/blazemeter/jmeter-from-code/

答案 1 :(得分:0)

您需要将使用Java生成的JMX中的文本“ org.apache.jorphan.collections.HashTree”更改为“ hashTree”。在任何文本编辑器中打开JMX并按所述进行替换。如果这样还不够,请执行以下步骤。

您需要为每个元素显式设置TestElement.ENABLED,TestElement.TEST_CLASS和TestElement.GUI_CLASS。例如,可以如下定义采样器。

 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());