简体   繁体   中英

How to convert json file to jmx file (jmeter) using java code?

Tried to convert json to xml and from xml to jmx file but couldn't able to convert to jmx file. Someone please help on this to do conversion using plain java code.

  1. You can use Jackson library in order to convert JSON to 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. You can use JMeter API to build a JMeter Test Plan from Java code, check out Five Ways To Launch a JMeter Test without Using the JMeter GUI article for detailed explanation and example code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM