我试图用我自己的采样器以编程方式运行JMeter。我已经能够使用HTTPSamplerProxy成功运行它,所以我非常确定该过程有效。
如果我只是替换
HTTPSamplerProxy javaSampler = new HTTPSamplerProxy();
用
CustomJavaSampler javaSampler = new CustomJavaSampler();
CustomJavaSampler
public class CustomJavaSampler extends AbstractJavaSamplerClient implements Serializable, Interruptible{
我得到了
摘要= 0 in 0s = ****** / s平均:0分钟:9223372036854775807最大值:-9223372036854775808错误:0(0.00%)
与
相比摘要= 23.3s = 23 / s平均值:21001最小值:20993最大值:21008错误:23(100.00%)
更完整的代码列表如下:
private StandardJMeterEngine doStandardJMeterSetup() {
File jmeterProperties = new File(jmeterHome.getPath() + File.separator + "bin" + File.separator + "jmeter.properties");
StandardJMeterEngine jmeter = new StandardJMeterEngine();
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome(jmeterHome.getPath());
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
return jmeter;
}
private void doJMeterStuff(int concurrentCount, int port, List<String> testData) {
StandardJMeterEngine jmeter = doStandardJMeterSetup();
// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();
// Second HTTP Sampler - open jon's machine
HTTPSamplerProxy javaSampler = new HTTPSamplerProxy();
System.out.println("The domain is: " + testData.get(0) + " " + testData.get(0).getClass().getName());
javaSampler.setDomain(testData.get(0));
javaSampler.setPort(Integer.parseInt(testData.get(1)));
javaSampler.setPath(testData.get(2));
javaSampler.setMethod("GET");
javaSampler.setName("Open implementation at " + port);
javaSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
javaSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
Arguments jargs = javaSampler.getArguments();
jargs.addArgument(new HTTPArgument(testData.get(3), testData.get(4), testData.get(5), true));
jargs.getArgument(0).setProperty("HTTPArgument.always_encode", false);
// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(concurrentCount);
threadGroup.setRampUp(2);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
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);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(javaSampler);
// threadGroupHashTree.add(javascriptSampler);
//add Summarizer output to get test progress in stdout like:
// summary = 2 in 1.3s = 1.5/s Avg: 631 Min: 290 Max: 973 Err: 0 (0.00%)
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
// Store execution results into a .jtl file
String tmpDir = System.getProperty("java.io.tmpdir");
String logFile = tmpDir + File.separator + "example"+port+".jtl";
CustomResultCollector logger = new CustomResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
String logFile2 = tmpDir + File.separator + "example_results_"+port+".jtl";
ResultCollector r = new ResultCollector();
r.setFilename(logFile2);
SampleSaveConfiguration ssc = new SampleSaveConfiguration();
ssc.setAsXml(true);
ssc.setResponseData(true);
ssc.setResponseHeaders(true);
r.setSaveConfig(ssc);
ResultSaver rs = new ResultSaver();
rs.setEnabled(true);
rs.setName("Responses");
javaSampler.addTestElement(rs);
testPlanTree.add(testPlanTree.getArray()[0], r);
testPlanTree.add(testPlanTree.getArray()[0], rs);
// save generated test plan to JMeter's .jmx file format
try {
SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + File.separator + "example"+port+".jmx"));
} catch (IOException e) {
e.printStackTrace();
}
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
}
答案 0 :(得分:0)
在CustomJavaSampler中,您可能已经覆盖了sample
函数,因此它应该返回的SampleResult将包含统计数据。例如,以下内容将确保样本结果具有开始和结束时间,并将设置为状态&#34;成功&#34;:
@Override
public SampleResult sample(Entry e)
{
SampleResult sampleResult = new SampleResult();
sampleResult.setSampleLabel( "My label" );
sampleResult.sampleStart();
// do something useful
sampleResult.sampleEnd();
sampleResult.setSuccessful( true );
sampleResult.setResponseMessageOK();
return sampleResult;
}