Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加载由Java代码生成的.jmx文件时发生ConversionException_Java_Jmeter - Fatal编程技术网

加载由Java代码生成的.jmx文件时发生ConversionException

加载由Java代码生成的.jmx文件时发生ConversionException,java,jmeter,Java,Jmeter,我有一个Java代码来创建JMeter测试计划。但每次我尝试加载那些创建的.jmx文件时,都会出现以下错误: Caused by: com.thoughtworks.xstream.converters.ConversionException: ---- Debugging information ---- cause-exception : java.lang.NullPointerException cause-message : null class

我有一个Java代码来创建JMeter测试计划。但每次我尝试加载那些创建的
.jmx
文件时,都会出现以下错误:

Caused by: com.thoughtworks.xstream.converters.ConversionException: 

---- Debugging information ----
cause-exception     : java.lang.NullPointerException
cause-message       : null
class               : org.apache.jmeter.reporters.ResultCollector
required-type       : org.apache.jmeter.reporters.ResultCollector
converter-type      : org.apache.jmeter.save.converters.TestElementConverter
path                : /jmeterTestPlan/org.apache.jorphan.collections.HashTree/org.apache.jorphan.collections.HashTree/ResultCollector
line number         : 28
class[1]            : org.apache.jorphan.collections.HashTree
converter-type[1]   : org.apache.jmeter.save.converters.HashTreeConverter
-------------------------------
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:73) ~[xstream-1.4.9.jar:1.4.9]
    at org.apache.jmeter.save.converters.HashTreeConverter.unmarshal(HashTreeConverter.java:67) ~[ApacheJMeter_core.jar:3.2 r1790748]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:73) ~[xstream-1.4.9.jar:1.4.9]
    at org.apache.jmeter.save.converters.HashTreeConverter.unmarshal(HashTreeConverter.java:67) ~[ApacheJMeter_core.jar:3.2 r1790748]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) ~[xstream-1.4.9.jar:1.4.9]
    at org.apache.jmeter.save.ScriptWrapperConverter.unmarshal(ScriptWrapperConverter.java:95) ~[ApacheJMeter_core.jar:3.2 r1790748]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1230) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1214) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1085) ~[xstream-1.4.9.jar:1.4.9]
    at org.apache.jmeter.save.SaveService.readTree(SaveService.java:442) ~[ApacheJMeter_core.jar:3.2 r1790748]
    ... 21 more
Caused by: java.lang.NullPointerException
    at java.util.Hashtable.get(Hashtable.java:363) ~[?:1.8.0_121]
    at java.util.Properties.getProperty(Properties.java:969) ~[?:1.8.0_121]
    at org.apache.jmeter.save.SaveService.aliasToClass(SaveService.java:285) ~[ApacheJMeter_core.jar:3.2 r1790748]
    at org.apache.jmeter.save.converters.TestElementConverter.unmarshal(TestElementConverter.java:93) ~[ApacheJMeter_core.jar:3.2 r1790748]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:73) ~[xstream-1.4.9.jar:1.4.9]
    at org.apache.jmeter.save.converters.HashTreeConverter.unmarshal(HashTreeConverter.java:67) ~[ApacheJMeter_core.jar:3.2 r1790748]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.readItem(AbstractCollectionConverter.java:73) ~[xstream-1.4.9.jar:1.4.9]
    at org.apache.jmeter.save.converters.HashTreeConverter.unmarshal(HashTreeConverter.java:67) ~[ApacheJMeter_core.jar:3.2 r1790748]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) ~[xstream-1.4.9.jar:1.4.9]
    at org.apache.jmeter.save.ScriptWrapperConverter.unmarshal(ScriptWrapperConverter.java:95) ~[ApacheJMeter_core.jar:3.2 r1790748]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:70) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1230) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1214) ~[xstream-1.4.9.jar:1.4.9]
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1085) ~[xstream-1.4.9.jar:1.4.9]
    at org.apache.jmeter.save.SaveService.readTree(SaveService.java:442) ~[ApacheJMeter_core.jar:3.2 r1790748]
    ... 21 more
以下是Java代码:

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.java.sampler.JSR223Sampler;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.SummaryReport;
import org.apache.jorphan.collections.HashTree;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Locale;
import java.util.UUID;

class JedisSampler {

    private static String DEFAULT_SCRIPT_LANGUAGE = "groovy";
    private static String JMETER_HOME = "src/test/jmeter/";
    private static String JMETER_PROPERTIES_FILE = JMETER_HOME + "bin/jmeter.properties";
    private static String JMETER_REPORTS_HOME = "target/jmeter/";

    void createAndRunJmeterTestPlan(int numberOfThreads, int rampUpPeriodInSeconds, int loopCount, String testName, String script) throws Exception {

        setUpJMeterUtils();

        TestPlan testPlan = setUpTestPlan(testName);

        HashTree testPlanTree = new HashTree();
        testPlanTree.add(testPlan);

        Summariser summer = setSummariser();
        testPlanTree.add(testPlanTree.getArray()[0], setLogger(testName, summer));
        testPlanTree.add(testPlanTree.getArray()[0], setLoggerCSV(testName, summer));

        ThreadGroup threadGroup = setUpThreadGroup(numberOfThreads, rampUpPeriodInSeconds, setUpLoopController(loopCount));

        HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
        threadGroupHashTree.add(setUpJSR223Sampler(script));

        SaveService.saveTree(testPlanTree, new FileOutputStream(JMETER_REPORTS_HOME + testName + ".jmx"));

        StandardJMeterEngine jmeter = new StandardJMeterEngine();
        jmeter.configure(testPlanTree);
        jmeter.run();

        System.out.println("Test completed. See " + JMETER_REPORTS_HOME + testName + ".jtl file for results");
        System.out.println("JMeter .jmx script is available at " + JMETER_REPORTS_HOME + testName + "-summary.jmx");

        jmeter.exit();
    }

    JSR223Sampler setUpJSR223Sampler(String script) {
        JSR223Sampler jedisSampler = new JSR223Sampler();
        jedisSampler.setName("Jedis Sampler");
        jedisSampler.setProperty("script", script);

        jedisSampler.setProperty("scriptLanguage", DEFAULT_SCRIPT_LANGUAGE);
        jedisSampler.setProperty("cacheKey", UUID.randomUUID().toString());
        jedisSampler.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
        jedisSampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
        return jedisSampler;
    }

    // to set loop running forever, loopCount should be equal to -1
    LoopController setUpLoopController(int loopCount) {
        LoopController loopController = new LoopController();
        loopController.setLoops(loopCount);
        loopController.setFirst(true);
        loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
        loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
        loopController.initialize();
        return loopController;
    }

    ThreadGroup setUpThreadGroup(int numberOfThreads, int rampUpPeriodInSeconds, LoopController loopController) {
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setName("Jedis Thread Group");
        threadGroup.setNumThreads(numberOfThreads);
        threadGroup.setRampUp(rampUpPeriodInSeconds);
        threadGroup.setSamplerController(loopController);
        threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
        threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
        return threadGroup;
    }

    Summariser setSummariser() {
        //add Summarizer output to get test progress in stdout like:
        // summary =      2 in   1.3s =    1.5/s Avg:   631 Min:   290 Max:   973 Err:     0 (0.00%)
        Summariser summer = null;
        String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
        if (summariserName.length() > 0) {
            summer = new Summariser(summariserName);
        }
        return summer;
    }

    ResultCollector setLogger(String testName, Summariser summer) {
        String logFile = JMETER_REPORTS_HOME + testName + "-summary.jtl";
        ResultCollector logger = new ResultCollector(summer);
        logger.setFilename(logFile);
        return  logger;
    }

    ResultCollector setLoggerCSV(String testName, Summariser summer) {
        String csvLogFile = JMETER_REPORTS_HOME + testName + "-summary.csv";
        ResultCollector csvlogger = new ResultCollector(summer);
        csvlogger.setFilename(csvLogFile);
        return  csvlogger;
    }

    TestPlan setUpTestPlan(String testName) {
        TestPlan testPlan = new TestPlan(testName + " Plan");
        testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
        testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
        testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
        return testPlan;
    }

    void setUpJMeterUtils() throws IOException {
        if (!Files.exists(Paths.get(JMETER_REPORTS_HOME))) {
            Files.createDirectory(Paths.get(JMETER_REPORTS_HOME));
        }
        JMeterUtils.loadJMeterProperties(JMETER_PROPERTIES_FILE);
        JMeterUtils.setLocale(Locale.ENGLISH);
        JMeterUtils.setJMeterHome(JMETER_HOME);
    }
}
请告知如何修复此问题

谢谢

这一行:

SaveService.saveTree(testPlanTree, new FileOutputStream(JMETER_REPORTS_HOME + testName + ".jmx"));
应在这些行之前

testPlanTree.add(testPlanTree.getArray()[0], setLogger(testName, summer));
testPlanTree.add(testPlanTree.getArray()[0], setLoggerCSV(testName, summer));
更多信息:

  • -关于在非GUI模式下运行JMeter的说明,包括通过
  • -示范简单测试计划创建、保存和执行的示例项目
谢谢你,德米特里!