|
一. 配置源码
下载源码包 地址
导入源码包,我这里使用的工具IntelliJ。直接File->open就行。导入后的项目目录如下图:
下载jar包。jar包下载下来的地址为:/lib
添加jar包。File->Project Structure->Libraries
install
运行 src->core->apache.jmeter->NewDriver
- 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);
- }
- }
复制代码 从上述代码中我们可以看出,其实之际上调用的是src->core->apache.jmeter->Jmeter 的start() 方法。
而当我们打开Jmeter的Gui时,实际上调用的用的是src->core->apache.jmeter->Jmeter 的startGui() 方法
- private void startGui(String testFile) {
- String jMeterLaf = LookAndFeelCommand.getJMeterLaf();
- try {
- UIManager.setLookAndFeel(jMeterLaf);
- } catch (Exception ex) {
- log.warn("Could not set LAF to:"+jMeterLaf, ex);
- }
- PluginManager.install(this, true);
- JMeterTreeModel treeModel = new JMeterTreeModel();
- JMeterTreeListener treeLis = new JMeterTreeListener(treeModel);
- final ActionRouter instance = ActionRouter.getInstance();
- instance.populateCommandMap(); //这个方法会去寻找<your project>/lib/ext 下所有的jar
- treeLis.setActionHandler(instance);
- // NOTUSED: GuiPackage guiPack =
- GuiPackage.getInstance(treeLis, treeModel);
- MainFrame main = new MainFrame(treeModel, treeLis);
- ComponentUtil.centerComponentInWindow(main, 80);
- main.setVisible(true);
- instance.actionPerformed(new ActionEvent(main, 1, ActionNames.ADD_ALL));
- if (testFile != null) {
- try {
- File f = new File(testFile);
- log.info("Loading file: " + f);
- FileServer.getFileServer().setBaseForScript(f);
- HashTree tree = SaveService.loadTree(f);
- GuiPackage.getInstance().setTestPlanFile(f.getAbsolutePath());
- Load.insertLoadedTree(1, tree);
- } catch (ConversionException e) {
- log.error("Failure loading test file", e);
- JMeterUtils.reportErrorToUser(SaveService.CEtoString(e));
- } catch (Exception e) {
- log.error("Failure loading test file", e);
- JMeterUtils.reportErrorToUser(e.toString());
- }
- } else {
- JTree jTree = GuiPackage.getInstance().getMainFrame().getTree();
- TreePath path = jTree.getPathForRow(0);
- jTree.setSelectionPath(path);
- FocusRequester.requestFocus(jTree);
- }
- }
复制代码 二.开发sampler
JMeter加载插件的机制比较简单,扫描扩展下的的所有实现了JMeterGUIComponent和TestBean接口的
类,然后进行初始化。
- ClassFinder.findClassesThatExtend(
- JMeterUtils.getSearchPaths(),
- new Class[] {JMeterGUIComponent.class, TestBean.class }
复制代码 在getSearchPaths()在这个方法中,它寻找的路径是/lib/ext,所以要确保你开发的插件的jar存在与这
个路径中。
- public static String[] getSearchPaths() {
- String p = JMeterUtils.getPropDefault("search_paths", null); // $NON-NLS-1$
- String[] result = new String[1];
- if (p != null) {
- String[] paths = p.split(";"); // $NON-NLS-1$
- result = new String[paths.length + 1];
- System.arraycopy(paths, 0, result, 1, paths.length);
- }
- result[0] = getJMeterHome() + "/lib/ext"; // $NON-NLS-1$
- return result;
- }
复制代码
|
|