JMeterEngine 接口被运行 Jmeter 的测试类实现,此接口共 8 个方法。
API地址:https://jmeter.apache.org/api/org/apache/jmeter/engine/JMeterEngine.html
简要解读:
配置引擎,HashTree 是 JMeter 执行测试依赖的数据结构,configure 在执行测试之前进行配置测试数据。可以参考接口JMeterEngine的实现类
StandardJMeterEngine
@Override
public void configure(HashTree testTree) {
// Is testplan serialised?
SearchByClass<TestPlan> testPlan = new SearchByClass<>(TestPlan.class);
testTree.traverse(testPlan);
Object[] plan = testPlan.getSearchResults().toArray();
if (plan.length == 0) {
throw new IllegalStateException("Could not find the TestPlan class!");
}
TestPlan tp = (TestPlan) plan[0];
serialized = tp.isSerialized();
tearDownOnShutdown = tp.isTearDownOnShutdown();
active = true;
test = testTree;
}
从 HashTree中 解析出 TestPlan , 获取 TestPlan 的 serialized 和tearDownOnShutdown 并保存为 local 属性,同时把整个 HashTree 也保存到 local。
/** Thread Groups run sequentially */
private volatile boolean serialized = false;
/** tearDown Thread Groups run after shutdown of main threads */
private volatile boolean tearDownOnShutdown = false;
StandardJMeterEngine 依赖线程组 ThreadGroup, 一个测试中可能会有多个线程组,如果 serialized 为 true,则 StandardJMeterEngine 会串行的去执行这些线程组,每启动一个 ThreadGroup 主线程都会等它结束;否则就并行执行所有的线程组。
tearDownOnShutdown 与 PostThreadGroup 配合使用的,这个 Special Thread Group 专门用来做清理工作
/**
* PostThreadGroup is a special type of ThreadGroup that can be used for
* performing actions at the end of a test for cleanup and such.
*/
public class PostThreadGroup extends ThreadGroup {
private static final long serialVersionUID = 240L;
}
如果在 HashTree 配置中有 PostThreadGroup,那么在主线程组(ThreadGroup)跑完之后,StandardJMeterEngine 会去检查这个tearDownOnShutdown 属性,若该属性值 true 就启动PostThreadGroup。
是为 Remote Test 准备的,如果当前的测试是从一个客户端的 JMeter 执行远程 JMeterEngine 的 remote samples,则应该调用该 exit() 方法来关闭远程的测试。
远程退出由 RemoteJMeterEngineImpl.rexit() 和notifyTestListenersOfEnd() 调用 iff exitAfterTest 为 true; 反过来,run( ) 方法调用,也调用 StopTest 类
/**
* Remote exit
* Called by RemoteJMeterEngineImpl.rexit()
* and by notifyTestListenersOfEnd() iff exitAfterTest is true;
* in turn that is called by the run() method and the StopTest class
* also called
*
*/
@Override
public void exit() {
ClientJMeterEngine.tidyRMI(log); // This should be enough to allow server to exit.
if (REMOTE_SYSTEM_EXIT) { // default is false
log.warn("About to run System.exit(0) on {}", host);
// Needs to be run in a separate thread to allow RMI call to return OK
Thread t = new Thread() {
@Override
public void run() {
pause(1000); // Allow RMI to complete
log.info("Bye from {}", host);
System.out.println("Bye from "+host); // NOSONAR Intentional
System.exit(0); // NOSONAR Intentional
}
};
t.start();
}
}
isActive 在测试中 JMeterEngine 返回值:
boolean 用于显示引擎是否处于活动状态的标志(在测试运行时为true)。在测试结束时设置为 false
public boolean isActive() {
return active;
}
重置。在 StandardJMeterEngine 中就是直接调用 stopTest(true)
@Override
public void reset() {
if (running) {
stopTest();
}
}
调用该方法用来执行测试。参考 StandardJMeterEngine 的实现,启动一个线程并触发它的run()方法,若报异常则调用stopTest(),抛出 JMeterEngineException。
@Override
public void runTest() throws JMeterEngineException {
if (host != null){
long now=System.currentTimeMillis();
System.out.println("Starting the test on host " + host + " @ "+new Date(now)+" ("+now+")"); // NOSONAR Intentional
}
try {
Thread runningThread = new Thread(this, "StandardJMeterEngine");
// 启动一个线程并触发它的run()方法
runningThread.start();
} catch (Exception err) {
stopTest();
throw new JMeterEngineException(err);
}
}
设置属性,可以将额外的配置文件通过该方法添加进去。它会保存在JMeterUtils 中,该类保存了JMeterEngine runtime 所需要的所有配置参数。
public void setProperties(Properties p) {
log.info("Applying properties " + p);
JMeterUtils.getJMeterProperties().putAll(p);
}
立即停止执行测试
public synchronized void stopTest() {
stopTest(true);
}
停止测试,若 now 为 true 则停止动作立即执行;
若为 false 则停止动作缓刑,它会等待当前正在执行的测试至少执行完一个 iteration
@Override
public synchronized void stopTest(boolean now) {
Thread stopThread = new Thread(new StopTest(now));
stopThread.start();
}
private class StopTest implements Runnable{……}
执行引擎(JMeterEngine),本质是一个线程,JMeterEngine 接口被运行 JMeter的测试类实现。
参考资料:https://jmeter.apache.org/api/org/apache/jmeter/engine/JMeterEngine.html