我有一個演示Java代碼的JMeter測試的類。JMeter Java代碼 - 如何將ConstantThroughputTimer添加到我的測試中
測試的對象是每秒設置N個請求。
我想添加一個ConstantThroughputTimer到我的測試中,以設置JMeter正在創建的最大RPS(請求每秒)。
在gui中創建了一個,它運行良好,但我想從java代碼運行它。
現在,我有2個問題:
- 我不知道如何設置線程組「循環計數」永遠。 (見屏幕截圖)
- 我無法將ConstantThroughputTimer添加到我的測試計劃中。
我搜索了,我找不到任何有關它的文檔,也沒有代碼示例。
任何幫助將非常感激。
我的代碼:
public static void main(String[] args) {
StandardJMeterEngine jMeterEngine = new StandardJMeterEngine();
//Setting JMeter Properties
File properties = JmeterUtils.getPropertiesFile();
File home = JmeterUtils.getHomePath();
JMeterUtils.setJMeterHome(home.getPath());
JMeterUtils.loadJMeterProperties(properties.getPath());
JMeterUtils.initLocale();
//Creating HashTreeTestPlan
HashTree testPlanTree = new HashTree();
//Creating HttpSampler
HTTPSamplerProxy sampler = new HTTPSamplerProxy();
sampler.setMethod("GET");
sampler.setDomain("example.com");
sampler.setUseKeepAlive(true);
sampler.setFollowRedirects(true);
sampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
sampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
sampler.setEnabled(true);
//Creating LoopController
LoopController loopController = new LoopController();
loopController.setContinueForever(true);
loopController.setLoops(10000);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();
loopController.setEnabled(true);
//Creating the number of Threads (clients)
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("threadGroup");
threadGroup.setNumThreads(10);
threadGroup.setScheduler(true);
threadGroup.setRampUp(0);
threadGroup.setDuration(60);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
threadGroup.setEnabled(true);
//Adding Constant Throughput Timer - This is what i want to add
ConstantThroughputTimer timer = new ConstantThroughputTimer();
timer.setProperty(TestElement.TEST_CLASS, ConstantThroughputTimer.class.getName());
timer.setName("constantTimer");
double rpsCalc = 10 * 60;
timer.setThroughput(rpsCalc);
timer.setEnabled(true);
timer.setCalcMode(2);
//NOT WORKING//
//NOT WORKING//
threadGroup.addTestElement(timer);
//Test Plan
TestPlan testPlan = new TestPlan("Test Plan");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
jMeterEngine.configure(testPlanTree);
try {
jMeterEngine.runTest();
} catch (JMeterEngineException e) {
e.printStackTrace();
}
}
究竟它是如何 「//不工作//」?它編譯了嗎?它會拋出運行時錯誤嗎?發生什麼事? –
對不起。忘了提到,測試運行,但沒有計時器 - 這意味着要求的提出和RPS是比什麼是設置定時器 – razrog
更加大,我根本不知道你打你的系統下測試的。我看到您將回路控制器添加到線程組,但我無法發現將採樣器添加到迴路控制器的時刻。以及將線程組添加到測試計劃的那一刻。您在此顯示的代碼與您嘗試運行的代碼不同嗎? –