我试图通过java运行Jmeter测试计划,下面是代码,
package com.jmeter;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.FileInputStream;
public class JMeterFromExistingJMX {
public static void main(String[] argv) throws Exception {
// JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
// Initialize Properties, logging, locale, etc.
JMeterUtils.loadJMeterProperties("c:\\JMeter\\apache-jmeter-3.0\\bin\\jmeter.properties");
JMeterUtils.setJMeterHome("c:\\JMeter\\apache-jmeter-3.0");
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
// Initialize JMeter SaveService
SaveService.loadProperties();
// Load existing .jmx Test Plan
FileInputStream in = new FileInputStream("c:\\JMeter\\apache-jmeter-3.0\\extras\\Test.jmx");
HashTree testPlanTree = SaveService.loadTree(in);
in.close();
// Run JMeter Test
jmeter.configure(testPlanTree);
jmeter.run();
}
}
以下是我在类路径中添加的.jars文件, Classpath Libraries
在编译上述代码时,我会遇到异常,
INFO 2017-01-28 20:25:58.549 [jmeter.e]():启用运行版后将启动监听器 INFO 2017-01-28 20:25:58.572 [jmeter.e]():要恢复到之前的行为,请定义jmeterengine.startlistenerslater = false
Exception in thread "main" java.lang.NullPointerException
at org.apache.jmeter.engine.StandardJMeterEngine.configure(StandardJMeterEngine.java:176)
at com.jmeter.JMeterFromExistingJMX.main(JMeterFromExistingJMX.java:32)
答案 0 :(得分:0)
根据Five Ways To Launch a JMeter Test without Using the JMeter GUI文章
要从Java代码执行现有的JMeter测试或通过编程创建一个,必须使用基本的Java知识,并且必须满足以下条件:
- 将JMeter安装在某个地方
- 在项目或模块类路径中,从 / lib ,尤其是JMeter安装的 / lib / ext 文件夹中获取所需的JMeter jar。
醇>
因此您需要删除apachejmeter_core-2.6.jar并添加最新JMeter版本安装文件夹中的库。