前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >性能工具之JMeter5.0核心类JMeterEngine源码分析

性能工具之JMeter5.0核心类JMeterEngine源码分析

作者头像
高楼Zee
发布2019-07-17 16:08:33
8820
发布2019-07-17 16:08:33
举报
文章被收录于专栏:7DGroup
概述

JMeterEngine 接口被运行 JMeter 的测试类实现,此接口共 8 个方法。

API地址:

https://jmeter.apache.org/api/org/apache/jmeter/engine/JMeterEngine.html

逻辑关系

简要解读:

  • HashTree 是依赖的数据结构;
  • SearchByClass 用来查找 HashTree 中的所有节点,并把节点实例化为真正的对象,例如图中TestPlan/ThreadGroup/JavaSampler/ResultCollector 在 HashTree 中本来都是只是配置,全部通过 SearchByClass 实例化的;
  • 实例化出来的对象如果是 TestStateListener 类型,则会在有生命周期的函数回调,测试前调 testStarted,结束掉 testEnded, 比如 ResultCollector是该类型的一种,在结束的时候回调 testEnded 方法完成 report 的写入;
  • PreCompiler 用来解析 Arguments, 把 TestPlan 节点中配置的参数作为JMeterVariables 加入到测试线程上线文中;
  • ThreadGroup 用来用来管理一组线程,包括线程的个数/启动/关闭等;
  • StopTest 作为其内部类对外不可见,作为一个 Runnable,作用是异步停止测试,stopTest方法也是通过该内部类实现的。

工程位置

主要方法

  • void configure(HashTree testPlan);
  • void exit();
  • boolean isActive();
  • void reset();
  • void runTest() ;
  • void setProperties(java.util.Properties p);
  • stopTest();
  • void stopTest(boolean now);

void configure(HashTree testPlan)

配置引擎,HashTree 是 JMeter 执行测试依赖的数据结构,configure 在执行测试之前进行配置测试数据。可以参考接口JMeterEngine的实现类性能工具之JMeter5.0核心类StandardJMeterEngine源码分析

代码语言:javascript
复制
    @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。

代码语言:javascript
复制
/** 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 专门用来做清理工作

代码语言:javascript
复制
/** * 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。

void exit()

是为 Remote Test 准备的,如果当前的测试是从一个客户端的 JMeter 执行远程 JMeterEngine 的 remote samples,则应该调用该 exit() 方法来关闭远程的测试。

远程退出由 RemoteJMeterEngineImpl.rexit() 和notifyTestListenersOfEnd() 调用 iff exitAfterTest 为 true; 反过来,run( ) 方法调用,也调用 StopTest 类

代码语言:javascript
复制
/**      * 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();        }    }

boolean isActive()

isActive 在测试中 JMeterEngine 返回值: boolean 用于显示引擎是否处于活动状态的标志(在测试运行时为true)。在测试结束时设置为 false

代码语言:javascript
复制
public boolean isActive() {    return active;}

void reset()

重置。在 StandardJMeterEngine 中就是直接调用 stopTest(true)

代码语言:javascript
复制
  @Override    public void reset() {        if (running) {            stopTest();        }    }

void runTest()

调用该方法用来执行测试。参考 StandardJMeterEngine 的实现,启动一个线程并触发它的run()方法,若报异常则调用stopTest(),抛出 JMeterEngineException。

代码语言:javascript
复制
  @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);        }    }

void setProperties(java.util.Properties p)

设置属性,可以将额外的配置文件通过该方法添加进去。它会保存在JMeterUtils 中,该类保存了JMeterEngine runtime 所需要的所有配置参数。

代码语言:javascript
复制
public void setProperties(Properties p) {    log.info("Applying properties " + p);    JMeterUtils.getJMeterProperties().putAll(p);}

stopTest()

立即停止执行测试

代码语言:javascript
复制
public synchronized void stopTest() {    stopTest(true);}

void stopTest(boolean now)

停止测试,若 now 为 true 则停止动作立即执行; 若为 false 则停止动作缓刑,它会等待当前正在执行的测试至少执行完一个 iteration

代码语言:javascript
复制
@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://blog.csdn.net/yue530tomtom/article/details/78016823#t2

https://jmeter.apache.org/api/org/apache/jmeter/engine/JMeterEngine.html

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-02-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 7DGroup 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 逻辑关系
  • 工程位置
  • 主要方法
    • void configure(HashTree testPlan)
      • void exit()
        • boolean isActive()
          • void reset()
            • void runTest()
              • void setProperties(java.util.Properties p)
                • stopTest()
                  • void stopTest(boolean now)
                  • 小结
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档