Jmeter is excellent tool used to do load testing on the application. Jmeter has graphical(GUI) and Non graphical(Non GUI) options. Non graphical option use Jmeter API to write a complete test plan for loading testing in java.
Pre-prerequisites:
1. JMeter install somewhere.
2. Add JMeter jars from /lib and especially /lib/ext folders in your project or module class path.
Load the jmeter properties:
Create "Test Plan" Object and JOrphan HashTree:
Graphical: Thread Group
Right-click on the Test Plan > Add > Threads (Users) > Thread Group
Type below values in corresponding field
Number of Threads (users) : 1
Ramp-Up Period (in seconds) : 1
Loop Count : 1
Write below values in corresponding field
Server Name or IP : hiromia.blogspot.com
Port NUmber : 80
Path : /
Non Graphical: Sampler
Graphical: Summary Report
Right-click on the Thread Group > Add > Listener > Summary Report
Non Graphical: Summary Report
Run Test Plan:
Completed Jmeter Test Plan java source code:
Output:
Pre-prerequisites:
1. JMeter install somewhere.
2. Add JMeter jars from /lib and especially /lib/ext folders in your project or module class path.
Load the jmeter properties:
//Set jmeter home for the jmeter utils to load
String jmeterHomelocation = "F:\\apache-jmeter-2.13\\";
String jmeterPropertieslocation = jmeterHomelocation + "bin\\jmeter.properties";
//JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome(new File(jmeterHomelocation).getPath());
JMeterUtils.loadJMeterProperties(new File(jmeterPropertieslocation).getPath());
// see extra log messages of i.e. DEBUG level
JMeterUtils.initLogging();
JMeterUtils.initLocale();
Create "Test Plan" Object and JOrphan HashTree:
// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();
// Test Plan
TestPlan testPlan = new TestPlan("Java code Test Plan");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
Graphical: Thread Group
Right-click on the Test Plan > Add > Threads (Users) > Thread Group
Type below values in corresponding field
Number of Threads (users) : 1
Ramp-Up Period (in seconds) : 1
Loop Count : 1
Non Graphical: Thread Group
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Test Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopCtrl);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
Graphical: Sampler
Right-click on the Thread Group > Add > Sampler > HTTP requestWrite below values in corresponding field
Server Name or IP : hiromia.blogspot.com
Port NUmber : 80
Path : /
Non Graphical: Sampler
HTTPSampler examplecomSampler = new HTTPSampler();
examplecomSampler.setDomain("www.google.com");
examplecomSampler.setPort(80);
examplecomSampler.setPath("/");
examplecomSampler.setMethod("GET");
examplecomSampler.setName("google");
examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSampler.class.getName());
examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
Loop Controller: // Loop Controller
LoopController loopCtrl = new LoopController();
loopCtrl.setLoops(10);
loopCtrl.setFirst(true);
loopCtrl.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopCtrl.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopCtrl.initialize();
Graphical: Summary Report
Right-click on the Thread Group > Add > Listener > Summary Report
Non Graphical: Summary Report
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, we can save file as csv also
String reportFile = "report\\report.jtl";
String csvFile = "report\\report.csv";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(reportFile);
ResultCollector csvlogger = new ResultCollector(summer);
csvlogger.setFilename(csvFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
testPlanTree.add(testPlanTree.getArray()[0], csvlogger);
Run Test Plan:
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run()
Completed Jmeter Test Plan java source code:
import java.io.File;
import java.io.FileOutputStream;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
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.HTTPSampler;
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.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
public class CompleteNongraphicaljplan {
public static void main(String[] argv) throws Exception {
//Set jmeter home for the jmeter utils to load
String jmeterHomelocation = "F:\\apache-jmeter-2.13\\";
String jmeterPropertieslocation = jmeterHomelocation + "bin\\jmeter.properties";
//JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome(new File(jmeterHomelocation).getPath());
JMeterUtils.loadJMeterProperties(new File(jmeterPropertieslocation).getPath());
// see extra log messages of i.e. DEBUG level
JMeterUtils.initLogging();
JMeterUtils.initLocale();
// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();
// First HTTP Sampler - open google.com
HTTPSampler examplecomSampler = new HTTPSampler();
examplecomSampler.setDomain("www.google.com");
examplecomSampler.setPort(80);
examplecomSampler.setPath("/");
examplecomSampler.setMethod("GET");
examplecomSampler.setName("google");
examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSampler.class.getName());
examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
// Loop Controller
LoopController loopCtrl = new LoopController();
loopCtrl.setLoops(10);
loopCtrl.setFirst(true);
loopCtrl.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopCtrl.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopCtrl.initialize();
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Test Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopCtrl);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
// Test Plan
TestPlan testPlan = new TestPlan("Java code Test Plan");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(examplecomSampler);
// save generated test plan to JMeter's .jmx file format
SaveService.saveTree(testPlanTree, new FileOutputStream("report\\jmeter_api_sample.jmx"));
//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, we can save file as csv also
String reportFile = "summaryreport.jtl";
String csvFile = "summaryreport.csv";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(reportFile);
ResultCollector csvlogger = new ResultCollector(summer);
csvlogger.setFilename(csvFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
testPlanTree.add(testPlanTree.getArray()[0], csvlogger);
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
System.exit(0);
}
}
Output:
Such a great articles in my carrier, It's wonderful commands like easiest understand words of knowledge in information's.
ReplyDeleteccna training in chennai guindy
Wow amazing i saw the article with execution models you had posted. It was such informative.By explaining this type we can identify the concepts easily. So thank you for this sharing.
ReplyDeleteSEO Training in Chennai
I just want to say I’m new to weblog and certainly savored this page. Almost certainly I’m likely to bookmark your website . You actually have outstanding well written articles. Cheers for sharing with us your website.
ReplyDeleteSeo Training
This comment has been removed by the author.
ReplyDeleteHow can i get Summarizer information in my java code.... like
ReplyDeletesummary = 2 in 1.3s = 1.5/s Avg: 631 Min: 290 Max: 973 Err: 0 in java object