I am constructing a .jmx file for jmeter GUI from Java. The .jmx only compose by http proxy samplers and threads groups. Both thread groups and proxy samplers are constructed based on a list, and ,thus, they are being arranged with a certain order. However, my problem is that the constructed .jmx has completely wrong order when comparing with the list.
I have read and try "Five Ways To Launch a JMeter Test without Using the JMeter GUI". What I did is trying to generalize the "Creating a New JMeter Test Purely in Java" to multiple thread groups and http proxy samplers. Here is the minimum code that I have worked on:
public static void createJmx(ArrayList<ArrayList<String>> calls, ArrayList<HashMap<String, String>> httpSamplerList) throws Exception {
// get log file length
int numberOfCalls = calls.size();
int numberOfhttpSampler = httpSamplerList.size();
// Engine
StandardJMeterEngine jm = new StandardJMeterEngine();
String jmeterHome = "D:\\Jenkins-Global-Workspace\\apache-jmeter-5.1.1";
// jmeter.properties
JMeterUtils.setJMeterHome(jmeterHome);
JMeterUtils.loadJMeterProperties("D:\\Jenkins-Global-Workspace\\apache-jmeter-5.1.1\\bin\\jmeter.properties");
JMeterUtils.initLogging();
JMeterUtils.initLocale();
// Create HashTree structure
HashTree testPlanTree = new HashTree();
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlanTree.add(testPlan);
HTTPSamplerProxy examplecomSampler = null;
LoopController loopController;
ThreadGroup threadGroup = null;
HashTree threadGroupHashTree = null;
int j=0;
for (int i = 0; i < numberOfCalls; i++) {
if (calls.get(i).size() == 3) {
System.out.println(calls.get(i));
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();
threadGroup = new ThreadGroup();
threadGroup.setName(calls.get(i).get(1));
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());
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
}
else {
System.out.println(calls.get(i));
examplecomSampler = new HTTPSamplerProxy();
examplecomSampler.setDomain("example.com");
examplecomSampler.setPort(80);
examplecomSampler.setPath("/");
examplecomSampler.setMethod("GET");
examplecomSampler.setName(Integer.toString(j)); // currently use a integer number naming as test
examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
threadGroupHashTree.add(examplecomSampler);
j++;
}
}
// // save generated test plan to JMeter's .jmx file format
SaveService.saveTree(testPlanTree, new FileOutputStream(".\\test.jmx"));
//
}
I am using a variable j to keep track the order of http sampler , but some http samplers are complete in wrong wrong order.
Does some one know why and how to fix it?
cheers
for (int i = 0; i < numberOfCalls; i++)you could usefor(ArrayList<String> elementList : calls)to get rid of all thosecalls.get(i). Additionally, you're only (re)initializingthreadGroupHashTreeifcalls.get(i).size() == 3- what if the sizes don't match that, e.g. if the first nested list has more or less elements? - Thomas