如何使用java在jmeter中创建自定义线程组?
问题描述:
我正在使用自定义线程组,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
答
- 这是不是一个完整的异常堆栈跟踪创建时间表这个插件,您需要提供全输出(更好地与您的代码清单显示之前的行失败)
-
Free-Form Arrivals Thread Group由
jmeter-plugins-casutg
提供,而不是jmeter-plugins-cmn-jmeter
,但是您需要在您的项目类路径中同时具有这两个jar。 - 如果您无法找到以编程方式创建和配置JMeter测试元素的方式,我会建议考虑在GUI模式下创建它。您将能够更容易地从Java代码运行现有的.jmx测试脚本,而不仅仅是使用JMeter API来创建它。
以防万一,如果你还需要这一点,代码应该是这样的:
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());
见Five Ways To Launch a JMeter Test without Using the JMeter GUI指南关于开展JMeter的测试(包括计划的)
的不同方式的详细信息在使用示例代码之后,我也会遇到同样的异常。当创建FreeFormArrivalsThreadGroup的对象时,它抛出_NullPointerException_ – KC7
谢谢@Dmitri T.问题是我在创建_FreeFormArrivalsThreadGroup_之后配置/添加了_JMeterUtils_。只是重新安排了代码,其工作正常。接受了答案。 – KC7