近日,正在做一个压测平台,主要功能是想整合压力机资源,通过一个统一的web平台,创建和管理压测任务,并展示包括实时压测结果、压力机性能数据、应用服务器性能数据等数据的性能测试报告。
本系统的核心是对JMeter进行二次封装,从而实现web版的JMeter平台。针对其中的实现原理,会逐一通过文章记录。本文主要是对JMeter的源码进行初步解析。
我们以非GUI模式运行JMeter为例,了解下JMeter的运行机制。首先我们找到入口类NewDriver,代码如下。
public static void main(String[] args) {
if(!EXCEPTIONS_IN_INIT.isEmpty()) {
System.err.println("Configuration error during init, see exceptions:"+exceptionsToString(EXCEPTIONS_IN_INIT));
} else {
Thread.currentThread().setContextClassLoader(loader);
setLoggingProperties(args);
try {
Class<?> initialClass = loader.loadClass("org.apache.jmeter.JMeter");// $NON-NLS-1$
Object instance = initialClass.newInstance();
Method startup = initialClass.getMethod("start", new Class[] { new String[0].getClass() });// $NON-NLS-1$
startup.invoke(instance, new Object[] { args });
} catch(Throwable e){ // NOSONAR We want to log home directory in case of exception
e.printStackTrace(); // NOSONAR No logger at this step
System.err.println("JMeter home directory was detected as: "+JMETER_INSTALLATION_DIRECTORY);
}
}
}
很明显,这里是通过反射调用JMeter类的start方法。接下来我们看下start方法,如下。
public