英文:
Jmeter is not running the HttpSamplerProxy
问题
StandardJMeterEngine jmeter = new StandardJMeterEngine();
// 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 structure using 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 using 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();
英文:
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();
答案1
得分: 5
首先,这段代码:
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);
需要被替换为:
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpSampler);
另外,我也没有看到你在将结果保存到 .jtl
文件中,你需要像这样添加 ResultCollector:
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);
在 jmeter.configure(testPlanTree);
行之前。
总的来说,使用 API 创建 JMeter 测试并不是得到很好支持的方式,也不能保证代码在未来的 JMeter 版本中能正常工作,因此我更建议使用第三方选项,比如 Taurus 或者 jmeter-java-dsl。
然而,如果出于某种原因你想继续,可以在 Five Ways To Launch a JMeter Test without Using the JMeter GUI 文章和 jmeter-from-code 仓库 中找到一些示例。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论