JMeterEngine接口被运行Jmeter的测试类实现,此接口共8个方法,其中无参stopTest()是调用的stopTest(boolean now),也可说是7个方法
HashTree是JMeter执行测试依赖的数据结构,configure在执行测试之前进行配置测试数据。可以参考接口JMeterEngine的实现类StandardJMeterEngine
public void configure(HashTree testTree) {
// Is testplan serialised?
SearchByClass testPlan = new SearchByClass<>(TestPlan.class);
testTree.traverse(testPlan);
Object[] plan = testPlan.getSearchResults().toArray();
if (plan.length == 0) {
throw new RuntimeException("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。
调用该方法用来执行测试。参考StandardJMeterEngine的实现
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 + ")");
}
try {
Thread runningThread = new Thread(this, "StandardJMeterEngine");
runningThread.start();
} catch (Exception err) {
stopTest();
throw new JMeterEngineException(err);
}
}
启动一个线程并触发它的run()方法,若报异常则调用stopTest(),抛出JMeterEngineException。
停止测试,若now为true则停止动作立即执行;若为false则停止动作缓刑,它会等待当前正在执行的测试至少执行完一个iteration。
public synchronized void stopTest(boolean now) {
shutdown = !now;
Thread stopThread = new Thread(new StopTest(now));
stopThread.start();
}
private class StopTest implements Runnable{……}
public synchronized void stopTest() {
stopTest(true);
}
重置。在StandardJMeterEngine中就是直接调用stopTest(true).
public void reset() {
if (running) {
stopTest();
}
}
设置属性,可以将额外的配置文件通过该方法添加进去。它会保存在JMeterUtils中,该类保存了JMeterEngine runtime所需要的所有配置参数。
public void setProperties(Properties p) {
log.info("Applying properties " + p);
JMeterUtils.getJMeterProperties().putAll(p);
}
是为Remote Test准备的,如果当前的测试是从一个客户端的JMeter执行远程JMeterEngine的remote samples,则应该调用该exit()方法来关闭远程的测试.被RemoteJMeterEngineImpl.rexit()调用和exitAfterTest为真时被notifyTestListenersOfEnd()调用 ;
public void exit() {
ClientJMeterEngine.tidyRMI(log);
if (REMOTE_SYSTEM_EXIT) {
log.warn("About to run System.exit(0) on " + host);
Thread t = new Thread() {
@Override
public void run() {
pause(1000);
log.info("Bye from " + host);
System.out.println("Bye from " + host);
System.exit(0);
}
};
t.start();
}
}
引擎是否有效的标识,在测试结束时设为false
public boolean isActive() {
return active;
}
在confgiure()的时候设该值为true,在执行完测试(指的是该JMeterEngine所有ThreadGroup)之后设置为false。如果active==true,则说明该JMeterEngine已经配置完测试并且还没执行完,我们不能再进行configure或者runTest了;若active == false, 则该JMeterEngine是空闲的,我们可以重新配置HashTree,执行新的测试.
学习备忘
原文连接 http://blog.csdn.net/yue530tomtom/article/details/78016823