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