0
我正在使用自定義線程組,FreeFormArrivalsThreadGroup由火焰特性提供使用jmeter-plugins-casutg-2.1.jar。如何使用java在jmeter中創建自定義線程組?
當我嘗試創建用java該線程組(在創建從Java JMX的過程)
FreeFormArrivalsThreadGroup freeFormArrivalsThreadGroup = new FreeFormArrivalsThreadGroup();
代碼:
package com.create.jmx;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.gui.util.PowerTableModel;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import com.blazemeter.jmeter.threads.arrivals.FreeFormArrivalsThreadGroup;
import com.blazemeter.jmeter.threads.arrivals.FreeFormArrivalsThreadGroupGui;
public class CreateJMX
{
final static String JMETER_HOME = "jmeter_home_location";
public static void main(String[] args) throws FileNotFoundException, IOException
{
try
{
//creating a test plan
TestPlan testPlan = new TestPlan();
testPlan.setName("From Program");
testPlan.setEnabled(true);
testPlan.setProperty(TestElement.TEST_CLASS,
TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS,
TestPlanGui.class.getName());
//create a loop controller
LoopController loopController = new LoopController();
loopController.setEnabled(true);
loopController.setLoops(3);
loopController.setProperty(TestElement.TEST_CLASS,
LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS,
LoopControlPanel.class.getName());
// creating freeFormArrivalsThreadGroup
FreeFormArrivalsThreadGroup freeFormArrivalsThreadGroup = new FreeFormArrivalsThreadGroup();
freeFormArrivalsThreadGroup.setName("Free Form Arrivals Thread Group");
PowerTableModel schedule = new PowerTableModel(new String[]{"Start", "End", "Duration"},
new Class[]{Integer.class, Integer.class, Integer.class});
schedule.addRow(new Object[]{1, 10, 60});
freeFormArrivalsThreadGroup.setSamplerController(loopController);
freeFormArrivalsThreadGroup.setData(schedule);
freeFormArrivalsThreadGroup.setProperty(TestElement.TEST_CLASS, FreeFormArrivalsThreadGroup.class.getName());
freeFormArrivalsThreadGroup.setProperty(TestElement.GUI_CLASS, FreeFormArrivalsThreadGroupGui.class.getName());
//creating HTTP sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setEnabled(true);
httpSampler.setName("Google");
httpSampler.setDomain("www.google.com");
httpSampler.setPath("/");
httpSampler.setMethod("GET");
httpSampler.setProperty(TestElement.TEST_CLASS,
HTTPSampler.class.getName());
httpSampler.setProperty(TestElement.GUI_CLASS,
HttpTestSampleGui.class.getName());
HashTree hashTree = new HashTree();
hashTree.add(testPlan).add(freeFormArrivalsThreadGroup).add(httpSampler);
JMeterUtils.setJMeterHome(JMETER_HOME);
JMeterUtils.loadJMeterProperties(JMeterUtils.getJMeterBinDir()+"/jmeter.properties");
SaveService.saveTree(hashTree, new FileOutputStream("ouputfile.jmx"));
}
catch(Throwable e)
{
e.printStackTrace();
}
}
}
上述線拋出異常。這裏是堆棧跟蹤
java.lang.ExceptionInInitializerError
at org.apache.jmeter.reporters.ResultCollector.<init>(ResultCollector.java:173)
at org.apache.jmeter.reporters.ResultCollector.<init>(ResultCollector.java:163)
at com.blazemeter.jmeter.reporters.FlushingResultCollector.<init>(FlushingResultCollector.java:7)
at com.blazemeter.jmeter.threads.AbstractDynamicThreadGroupModel.<init>(AbstractDynamicThreadGroupModel.java:28)
at com.blazemeter.jmeter.threads.AbstractDynamicThreadGroup.<init>(AbstractDynamicThreadGroup.java:23)
at com.blazemeter.jmeter.threads.arrivals.ArrivalsThreadGroup.<init>(ArrivalsThreadGroup.java:20)
at com.blazemeter.jmeter.threads.arrivals.FreeFormArrivalsThreadGroup.<init>(FreeFormArrivalsThreadGroup.java:11)
at com.create.jmx.CreateJMX.main(CreateJMX.java:49)
Caused by: java.lang.NullPointerException
at org.apache.jmeter.samplers.SampleSaveConfiguration.<clinit>(SampleSaveConfiguration.java:296)
... 8 more
幫我用java
的不同方式的詳細信息在使用示例代碼之後,我也會遇到同樣的異常。當創建FreeFormArrivalsThreadGroup的對象時,它拋出_NullPointerException_ – KC7
謝謝@Dmitri T.問題是我在創建_FreeFormArrivalsThreadGroup_之後配置/添加了_JMeterUtils_。只是重新安排了代碼,其工作正常。接受了答案。 – KC7