Reputation: 13
I am running this with jmeter core and jmeter http both at version 5.3. I am confused to why the sampler will not fire off. I am new to using jmeter in this fashion and am not quite sure how this all works. When the code is ran i end up with no request data in the jtl file.
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.loadJMeterProperties(System.getenv("JMETER_HOME") + "/bin/jmeter.properties");
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
// JMeter Test Plan, basic all u JOrphan HashTree
HashTree testPlanTree = new HashTree();
// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("example.com");
httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("GET");
// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.addTestElement(httpSampler);
loopController.setFirst(true);
loopController.initialize();
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
// Construct Test Plan from previously initialized elements
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
Upvotes: 1
Views: 431
Reputation: 168002
First of all, this code:
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);
needs to be replaced by
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpSampler);
I also fail to see where you're saving the results into the .jtl, you need to add ResultCollector like this:
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
String logFile = "/path/to/result.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
before jmeter.configure(testPlanTree);
line
In general creating JMeter tests using API is not very supported and there is no any guarantee that the code will work after next JMeter release so I would rather rely on 3rd-party options like Taurus or jmeter-java-dsl
However if for some reason you want to continue you can get some examples in Five Ways To Launch a JMeter Test without Using the JMeter GUI article and in jmeter-from-code repo
Upvotes: 5