JMeter API has an option to run JMeter script which made by Jmeter GUI using java. StandardJMeterEngine is the 'heart' of JMeter. If you must execute a JMeter test from Java code, this is the best option. The absolute minimal code to read the existing .jmx file.
Pre-prerequisites:
1. JMeter install somewhere.
2. Add JMeter jars from /lib and especially /lib/ext folders in your project or module class path.
Build Jmeter load testing script using below steps:
1. Start Jmeter
Click on Jmeter_Home/bin/ApacheJMeter.jar or jmeter.sh to open Jmeter window
2. Add 'Thread Group' under 'Test Plan'
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
3. Add a Sampler under 'Thread Group'
Right-click on the Thread Group > Add > Sampler > HTTP request
Write below values in corresponding field
Server Name or IP : hiromia.blogspot.com
Port NUmber : 80
Path : /
4. Add Listener under 'Thread Group'
Right-click on the Thread Group > Add > Listener > View Results Tree
5. Finally Save Test plan with desired name
Demo Java Code:
Out put:
Pre-prerequisites:
1. JMeter install somewhere.
2. Add JMeter jars from /lib and especially /lib/ext folders in your project or module class path.
Build Jmeter load testing script using below steps:
1. Start Jmeter
Click on Jmeter_Home/bin/ApacheJMeter.jar or jmeter.sh to open Jmeter window
2. Add 'Thread Group' under 'Test Plan'
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
3. Add a Sampler under 'Thread Group'
Right-click on the Thread Group > Add > Sampler > HTTP request
Write below values in corresponding field
Server Name or IP : hiromia.blogspot.com
Port NUmber : 80
Path : /
4. Add Listener under 'Thread Group'
Right-click on the Thread Group > Add > Listener > View Results Tree
5. Finally Save Test plan with desired name
Demo Java Code:
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.File;
import java.io.FileInputStream;
public class ProgramJmeterJMX {
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();
// Initialize Properties, logging, locale, etc.
JMeterUtils.loadJMeterProperties(new File(jmeterPropertieslocation).getPath());
JMeterUtils.setJMeterHome(new File(jmeterHomelocation).getPath());
// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLogging();
JMeterUtils.initLocale();
// Initialize JMeter SaveService
SaveService.loadProperties();
// Load existing .jmx Test Plan
FileInputStream in = new FileInputStream(new File(jmeterHomelocation + "bin\\webloadtesting.jmx"));
HashTree testPlanTree = SaveService.loadTree(in);
in.close();
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
ResultCollector logger = new ResultCollector(summer);
testPlanTree.add(testPlanTree.getArray()[0], logger);
// Run JMeter Test
jmeter.configure(testPlanTree);
jmeter.run();
}
}
Out put:
Useless article. This is not working.
ReplyDeletetestPlanTree.add(testPlanTree.getArray()[0], logger);
ReplyDeleteCan you please tell what iis this for ?
I'm getting a null pointer exception because of this.
did you get the answer??m facing the same issue
DeleteThanks for sharing this useful information. Load testing companies in UAE
ReplyDelete