-
Notifications
You must be signed in to change notification settings - Fork 0
/
KatalonJMeterRunner.groovy
92 lines (74 loc) · 3.25 KB
/
KatalonJMeterRunner.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.katalon.jmeter
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.report.dashboard.ReportGenerator;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jorphan.collections.HashTree;
import com.kms.katalon.core.configuration.RunConfiguration;
import com.kms.katalon.core.util.KeywordUtil;
public class KatalonJMeterRunner {
private static NUM_LOOPS = 1;
private static NUM_THREADS = 2;
private static RAMP_UP = 1;
private static DURATION = 10000;
private static REPORT_DIR = "${RunConfiguration.getReportFolder()}${File.separator}jmeter-report";
private static JTL_FILE = "${RunConfiguration.getReportFolder()}${File.separator}jmeter-result.csv";
public void run(KatalonSampler sampler) throws IOException {
// JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.loadJMeterProperties('Include/jmeter-properties/bin/jmeter.properties');
JMeterUtils.setJMeterHome('Include/jmeter-properties/');
JMeterUtils.initLocale();
JMeterUtils.setProperty("jmeter.reportgenerator.exporter.html.property.output_dir", REPORT_DIR);
// JMeter Test Plan
HashTree testPlanTree = new HashTree();
// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(NUM_LOOPS);
loopController.setFirst(true);
loopController.initialize();
// Thread Group
org.apache.jmeter.threads.ThreadGroup threadGroup = new org.apache.jmeter.threads.ThreadGroup();
threadGroup.setNumThreads(NUM_THREADS);
threadGroup.setRampUp(RAMP_UP);
threadGroup.setDuration(DURATION);
threadGroup.setScheduler(true);
threadGroup.setName("Main Thread Group");
threadGroup.setSamplerController(loopController);
// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(sampler);
// Add Summarizer output to get test progress in stdout like:
// summary = 2 in 1.3s = 1.5/s Avg: 631 Min: 290 Max: 973 Err: 0 (0.00%)
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
// Store execution results into a .jtl file
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(JTL_FILE);
testPlanTree.add(testPlan, logger);
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
try {
ReportGenerator reportGen = new ReportGenerator(JTL_FILE, null);
reportGen.generate();
} catch (Exception e) {
e.printStackTrace();
}
KeywordUtil.logInfo("Test completed. See ${JTL_FILE} file for results");
KeywordUtil.logInfo("JMeter report directory is ${REPORT_DIR}");
System.exit(0);
}
}