我正在尝试通过我的代码运行Jmeter,但结果显示错误。 错误:非HTTP响应代码:java.lang.IllegalArgumentException,非HTTP响应消息:URI不能为null。,threadGroup
StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.setJMeterHome("/Users/apple/Downloads/apache-jmeter-5.1");
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.loadJMeterProperties("/Users/apple/Downloads/apache-jmeter-5.1/bin/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, basic all u JOrphan HashTree
HashTree testPlanTree = new HashTree();
// HTTP Sampler
HTTPSampler httpSampler = new HTTPSampler();
httpSampler.setDomain("http://url");
httpSampler.setPort(8080);
httpSampler.setPath("/");
httpSampler.setMethod("GET");
HeaderManager headerManager = new HeaderManager();
headerManager.add(new Header("Content-type", "application/json"));
headerManager.add(new Header("Authorization", "Basic *****"));
// headerManager.add(new Header("Postman-Token", "***"));
httpSampler.setHeaderManager(headerManager);
// Loop Controller
LoopController loopController = new LoopController();
loopController.setContinueForever(true);
loopController.setLoops(1);
loopController.setFirst(true);
// loopController.setProperty(TestElement.TEST_CLASS,LoopController.class.getName()); // loopController.setProperty(TestElement.GUI_CLASS,LoopControlPanel.class.getName()); loopController.initialize(); loopController.setEnabled(true);
// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("threadGroup");
threadGroup.setNumThreads(1);
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);
///Report
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
String logFile = "/Users/apple/result.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
// code below is use to add authorization
HashTree requestHashTree = new HashTree();
requestHashTree.add(httpSampler, headerManager);
// Construct Test Plan from previously initialized elements
// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
//
testPlanTree.add(httpSampler, headerManager);
// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(requestHashTree);
// code above is use to add authorization
//
// Construct Test Plan from previously initialized elements
testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("httpSampler", httpSampler);
testPlanTree.add(testPlanTree.getArray()[0], logger);
// Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();