2
所以我用我的Maven项目这个例子:在Java代码运行的JMeter - JMeterThread不运行采样
public class JMeterFromScratch {
public static void main(String[] argv) throws Exception {
//JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.loadJMeterProperties("C:/git/jbehave-snbtracker/src/main/resources/config/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
HashTree testPlanTree = new HashTree();
// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("example.com");
httpSampler.setPort(80);
httpSampler.setPath("/");
httpSampler.setMethod("GET");
// Loop Controller
TestElement loopController = new LoopController();
((LoopController)loopController).setLoops(1);
loopController.addTestElement(httpSampler);
((LoopController)loopController).setFirst(true);
((LoopController)loopController).initialize();
// Thread Group
SetupThreadGroup threadGroup = new SetupThreadGroup();
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(((LoopController)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();
}
}
在其中,我有以下依赖性:
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_http</artifactId>
<version>2.13</version>
</dependency>
奇怪的是我看到的是在ThreadGroup
班,在makeThread()
的方法中,有这样一行:
JMeterThread jmeterThread = new JMeterThread(this.cloneTree(threadGroupTree), this, notifier);
对cloneTree
的调用似乎确实克隆了树,但是没有LoopController中的采样器。所以,当JMeterThread
到达这里:
Sampler sampler = this.threadGroupLoopController.next(); // returns null
while(true) {
while(this.running && sampler != null) {
的sampler
为null,并且因此没有执行。
那么我做错了什么?
谢谢。
嗨德米特里T,感谢您的回复。我会用你的建议,并回复你的结果。顺便说一句,关于您提供的链接,请注意第4.3部分“纯粹用Java创建新的JMeter测试”。这正是我从我的回答中拿出榜样的地方,所以他们写下了你不喜欢的东西。 – Nom1fan
谢谢!你的代码有效。我将使用它来让我的代码也起作用。仍然看起来很奇怪,这个链接的例子不正确,但并没有运行。 – Nom1fan