Jmeter源码解析(1)

来源:互联网 发布:精子优化处理哪个医院 编辑:程序博客网 时间:2024/05/24 00:21

查了很多网上的资料,对jmeter源码解释都很粗,对新接触的人没有什么帮助。 好吧,那就自己干。 一步一步学习。

持续更新,我下载的是jmeter3.1的源码。

先上启动类,jmeter/src/core/org.apache.jmeter/NewDriver.Java,是的,这就是jemter的总入口。

    static {
        final List<URL> jars = new LinkedList<>();
        final String initial_classpath = System.getProperty(JAVA_CLASS_PATH);


        // Find JMeter home dir from the initial classpath
        String tmpDir=null;
        StringTokenizer tok = new StringTokenizer(initial_classpath, File.pathSeparator);
        if (tok.countTokens() == 1
                || (tok.countTokens()  == 2 // Java on Mac OS can add a second entry to the initial classpath
                    && OS_NAME_LC.startsWith("mac os x")// $NON-NLS-1$
                   )
           ) {
            File jar = new File(tok.nextToken());
            try {
                tmpDir = jar.getCanonicalFile().getParentFile().getParent();
            } catch (IOException e) {
            }
        } else {// e.g. started from IDE with full classpath
            tmpDir = System.getProperty("jmeter.home","");// Allow override $NON-NLS-1$ $NON-NLS-2$
            if (tmpDir.length() == 0) {
                File userDir = new File(System.getProperty("user.dir"));// $NON-NLS-1$
                tmpDir = userDir.getAbsoluteFile().getParent();
            }
        }
        jmDir=tmpDir;

在定义各种静态变量后,开始找jemeter的本地安装路径,这里对mac的操作系统作了特别处理。基本所有的方法都很基础,全是I/O处理。


  boolean usesUNC = OS_NAME_LC.startsWith("windows");// $NON-NLS-1$


        // Add standard jar locations to initial classpathvbol+
        StringBuilder classpath = new StringBuilder();
        File[] libDirs = new File[] { new File(jmDir + File.separator + "lib"),// $NON-NLS-1$ $NON-NLS-2$
                new File(jmDir + File.separator + "lib" + File.separator + "ext"),// $NON-NLS-1$ $NON-NLS-2$
                new File(jmDir + File.separator + "lib" + File.separator + "junit")};// $NON-NLS-1$ $NON-NLS-2$
        for (File libDir : libDirs) {
            File[] libJars = libDir.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {// only accept jar files
                    return name.endsWith(".jar");// $NON-NLS-1$
                }
            });
            if (libJars == null) {
                new Throwable("Could not access " + libDir).printStackTrace();
                continue;
            }
            Arrays.sort(libJars); // Bug 50708 Ensure predictable order of jars
            for (File libJar : libJars) {
                try {
                    String s = libJar.getPath();


                    // Fix path to allow the use of UNC URLs
                    if (usesUNC) {
                        if (s.startsWith("\\\\") && !s.startsWith("\\\\\\")) {// $NON-NLS-1$ $NON-NLS-2$
                            s = "\\\\" + s;// $NON-NLS-1$
                        } else if (s.startsWith("//") && !s.startsWith("///")) {// $NON-NLS-1$ $NON-NLS-2$
                            s = "//" + s;// $NON-NLS-1$
                        }
                    } // usesUNC


                    jars.add(new File(s).toURI().toURL());// See Java bug 4496398
                    classpath.append(CLASSPATH_SEPARATOR);
                    classpath.append(s);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        }

        // ClassFinder needs the classpath
        System.setProperty(JAVA_CLASS_PATH, initial_classpath + classpath.toString());
        loader = AccessController.doPrivileged(
                new java.security.PrivilegedAction<DynamicClassLoader>() {
                    @Override
                    public DynamicClassLoader run() {
                        return new DynamicClassLoader(jars.toArray(new URL[jars.size()]));
                    }
                }
        );
    }

找到安装路径后,这里开始筛选bin里面的jar包,然后使用DynamicClassLoader加载。

剩下比较冗余就不贴代码了,分别是这个类的构造方法和一些静态方法。 最后还有它:

    public static void main(String[] args) {
        Thread.currentThread().setContextClassLoader(loader);
        if (System.getProperty("log4j.configuration") == null) {// $NON-NLS-1$ $NON-NLS-2$
            File conf = new File(jmDir, "bin" + File.separator + "log4j.conf");// $NON-NLS-1$ $NON-NLS-2$
            System.setProperty("log4j.configuration", "file:" + conf);
        }


        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){
            e.printStackTrace();
            System.err.println("JMeter home directory was detected as: "+jmDir);
        }
    }
}

设置新的系统属性,加载类org.apache.jmeter.JMeter,执行”start“方法。jmeter开始最初的启动。

原创粉丝点击