diff --git a/testing-modules/jmeter/pom.xml b/testing-modules/jmeter/pom.xml
index 868023e762..f9521f9dd1 100644
--- a/testing-modules/jmeter/pom.xml
+++ b/testing-modules/jmeter/pom.xml
@@ -37,6 +37,17 @@
org.springframework.boot
spring-boot-starter-security
+
+ org.apache.jmeter
+ ApacheJMeter_core
+ 5.4.1
+
+
+
+ org.apache.jmeter
+ ApacheJMeter_http
+ 5.4.1
+
diff --git a/testing-modules/jmeter/src/main/resources/jmeter.properties b/testing-modules/jmeter/src/main/resources/jmeter.properties
new file mode 100644
index 0000000000..5321c72d7c
--- /dev/null
+++ b/testing-modules/jmeter/src/main/resources/jmeter.properties
@@ -0,0 +1 @@
+jmeter.save.saveservice.output_format=xml
\ No newline at end of file
diff --git a/testing-modules/jmeter/src/test/java/com/baeldung/jmeterscript/JMeterLiveTest.java b/testing-modules/jmeter/src/test/java/com/baeldung/jmeterscript/JMeterLiveTest.java
new file mode 100644
index 0000000000..e2a5eb59cf
--- /dev/null
+++ b/testing-modules/jmeter/src/test/java/com/baeldung/jmeterscript/JMeterLiveTest.java
@@ -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;
+ }
+}