使用Java运行jmx文件

使用Java运行jmx文件

本文介绍了使用Java运行jmx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

尝试在JMeter GUI中打开/home/ksahu/MyScreenshots/k.jmx.如果未打开-代码存在问题,则使用生成了JMX文件.在这种情况下,请使用代码更新您的问题,您曾经使用该代码创建了k.jmx文件.

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.

请参见4. RUN A JMETER TEST THROUGH A PROGRAM (FROM JAVA CODE)章rel ="nofollow"> 5无需使用JMeter GUI即可启动JMeter测试的方法以获取详细信息.

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.

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

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

这篇关于使用Java运行jmx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:56