[BAEL-3845] Add JMeter test script (#16276)

* Add JMeter test script

* Address review comments

* Address review comments

* Refactor large method into smaller parts

---------

Co-authored-by: rajatgarg <rajatgarg@adobe.com>
This commit is contained in:
Rajat Garg 2024-04-06 05:13:09 +05:30 committed by GitHub
parent 7edeb43ed1
commit e5f9a53f7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 128 additions and 0 deletions

View File

@ -37,6 +37,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>5.4.1</version>
</dependency>
<!-- Apache JMeter HTTP -->
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_http</artifactId>
<version>5.4.1</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1 @@
jmeter.save.saveservice.output_format=xml

View File

@ -0,0 +1,116 @@
package com.baeldung.jmeterscript;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;
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.control.LoopController;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.junit.jupiter.api.Test;
/**
* This is a Live Test so that JMETER_HOME Environment variable will be available to run this test.
*/
public class JMeterLiveTest {
@Test
void givenJMeterScript_whenUsingCode_thenExecuteViaJavaProgram() throws IOException {
String jmeterHome = System.getenv("JMETER_HOME");
if (jmeterHome == null) {
throw new RuntimeException("JMETER_HOME environment variable is not set.");
}
String file = Objects.requireNonNull(JMeterLiveTest.class.getClassLoader().getResource("jmeter.properties")).getFile();
JMeterUtils.setJMeterHome(jmeterHome);
JMeterUtils.loadJMeterProperties(file);
JMeterUtils.initLocale();
StandardJMeterEngine jmeter = new StandardJMeterEngine();
HTTPSamplerProxy httpSampler = getHttpSamplerProxy();
LoopController loopController = getLoopController();
ThreadGroup threadGroup = getThreadGroup(loopController);
TestPlan testPlan = getTestPlan(threadGroup);
HashTree testPlanTree = new HashTree();
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpSampler);
SaveService.saveTree(testPlanTree, Files.newOutputStream(Paths.get("script.jmx")));
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
String logFile = "output-logs.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
jmeter.configure(testPlanTree);
jmeter.run();
System.out.println("Test completed. See output-logs.jtl file for results");
System.out.println("JMeter .jmx script is available at script.jmx");
}
private static TestPlan getTestPlan(ThreadGroup threadGroup) {
TestPlan testPlan = new TestPlan("Sample Test Plan");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.addThreadGroup(threadGroup);
return testPlan;
}
private static ThreadGroup getThreadGroup(LoopController loopController) {
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Sample Thread Group");
threadGroup.setNumThreads(10);
threadGroup.setRampUp(5);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
return threadGroup;
}
private static LoopController getLoopController() {
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();
return loopController;
}
private static HTTPSamplerProxy getHttpSamplerProxy() {
HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
httpSampler.setDomain("www.google.com");
httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("GET");
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
return httpSampler;
}
}