如何使用Java代码将json文件转换为jmx文件(jmeter)?

时间:2019-02-20 09:00:07

标签: java json jmeter jmx

试图将json转换为xml,并从xml转换为jmx文件,但无法转换为jmx文件。有人请帮助使用纯Java代码进行转换。

1 个答案:

答案 0 :(得分:0)

  1. 您可以使用Jackson library来将JSON转换为XML

    String jsonString = "" 
            + "{\n \"_links\": {\n" 
            + "    \"individual\": {\n" 
            + "      \"href\": \"/individuals/matching\",\n" 
            + "      \"name\": \"GET\",\n" 
            + "      \"title\": \"Individual Details\"\n" 
            + "    },\n" 
            + "    \"self\": {\n" 
            + "      \"href\": \"/individuals/matching/\"\n" 
            + "    }\n" //
            + "  }\n" //
            + "}";
    
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectMapper xmlMapper = new XmlMapper();
    JsonNode tree = objectMapper.readTree(jsonString);
    String jsonAsXml = xmlMapper.writer().withRootName("RootTagName").writeValueAsString(tree);
    
  2. 您可以使用JMeter API从Java代码构建JMeter测试计划,请查看Five Ways To Launch a JMeter Test without Using the JMeter GUI文章以获取详细说明和示例代码。

    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.http.control.gui.HttpTestSampleGui;
    import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
    import org.apache.jmeter.reporters.ResultCollector;
    import org.apache.jmeter.reporters.Summariser;
    import org.apache.jmeter.save.SaveService;
    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.jorphan.collections.HashTree;
    
    import java.io.FileOutputStream;
    
    public class JMeterFromScratch {
    
        public static void main(String[] args) throws Exception {
    
            String jmeterHome = "/path/to/your/jmeter/installation";
            StandardJMeterEngine jmeter = new StandardJMeterEngine();
    
            //JMeter initialization (properties, log levels, locale, etc)
            JMeterUtils.setJMeterHome(jmeterHome);
            JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties");
            JMeterUtils.initLocale();
    
            // JMeter Test Plan, basically JOrphan HashTree
            HashTree testPlanTree = new HashTree();
    
            // JMeter Test Plan, basically JOrphan HashTree
    
            // HTTP Sampler - open example.com
            HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
            examplecomSampler.setDomain("example.com.com");
            examplecomSampler.setPort(80);
            examplecomSampler.setPath("/");
            examplecomSampler.setMethod("POST");
            examplecomSampler.setName("Open example.com");
            Arguments arguments = new Arguments();
            arguments.addArgument("", jsonAsXml, "");
            examplecomSampler.setArguments(arguments);
            examplecomSampler.addNonEncodedArgument("", jsonAsXml, "");
            examplecomSampler.setPostBodyRaw(true);
            examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
            examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
    
    
            // Loop Controller
            LoopController loopController = new LoopController();
            loopController.setLoops(1);
            loopController.setFirst(true);
            loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
            loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
            loopController.initialize();
    
            // Thread Group
            ThreadGroup threadGroup = new ThreadGroup();
            threadGroup.setName("Example Thread Group");
            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());
    
            // Test Plan
            TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
            testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
            testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
            testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
    
            // HTTP Request Sampler and JSR223 PostProcessor
            HashTree httpRequestTree = new HashTree();
            httpRequestTree.add(examplecomSampler);
    
            // Construct Test Plan from previously initialized elements
            testPlanTree.add(testPlan);
            HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
            threadGroupHashTree.add(httpRequestTree);
    
            SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + "/bin/test.jmx"));
    
            //Add Summarizer output
            Summariser summer = null;
            String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
            if (summariserName.length() > 0) {
                summer = new Summariser(summariserName);
            }
    
            // Store Execution Results into a .csv file
            String csvFile = jmeterHome + "/bin/result.csv";
            ResultCollector logger = new ResultCollector(summer);
            logger.setFilename(csvFile);
            testPlanTree.add(testPlanTree.getArray()[0], logger);
    
            //Run Test Plan
            jmeter.configure(testPlanTree);
            jmeter.run();
    
            System.exit(0);
    
        }
    }