/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
testing-modules/jmeter/src/test/java/com/baeldung/jmeterscript/JMeterLiveTest.java
116 строк
5 KB
Rajat Garg
[BAEL-3845] Add JMeter test script (#16276)
06 апр 2024, 02:43
Не верифицирован
06 апр 2024, 02:43
e5f9a53
Код
Авторство
О чём код?
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; } }