jmeter系列五(Config配置元素)

今天研究的核心元素是Config

同样的道理在jmeter中每个testelement元素都对应着他的gui,Config也不例外,可以定位到包org.apache.jmeter.config和org.apache.jmeter.config.gui

包下面的类全部都是有关于Config这个元素,Config的元素的界面使用到了Jmete的两种界面策略(用到了TestBean和继承AbstractJMeterGuiComponent的实现方式)

前面分析了AbstractJMeterGuiComponent,TestBean后面会独立开篇分析

Config元素的TestElement的接口

 1 package org.apache.jmeter.config;
 2 
 3 public interface ConfigElement extends Cloneable {
 4 
 5     /**
 6      * Add a configuration element to this one. This allows config elements to
 7      * combine and give a "layered" effect. For example,
 8      * HTTPConfigElements have properties for domain, path, method, and
 9      * parameters. If element A has everything filled in, but null for domain,
10      * and element B is added, which has only domain filled in, then after
11      * adding B to A, A will have the domain from B. If A already had a domain,
12      * then the correct behavior is for A to ignore the addition of element B.
13      *
14      * @param config
15      *            the element to be added to this ConfigElement
16      */
17     void addConfigElement(ConfigElement config);
18 
19     /**
20      * If your config element expects to be modified in the process of a test
21      * run, and you want those modifications to carry over from sample to sample
22      * (as in a cookie manager - you want to save all cookies that get set
23      * throughout the test), then return true for this method. Your config
24      * element will not be cloned for each sample. If your config elements are
25      * more static in nature, return false. If in doubt, return false.
26      *
27      * @return true if the element expects to be modified over the course of a
28      *         test run
29      */
30     boolean expectsModification();
31 
32     Object clone();
33 }
View Code

一般的实现

public class ConfigTestElement extends AbstractTestElement implements Serializable, ConfigElement {
    private static final long serialVersionUID = 240L;

    public static final String USERNAME = "ConfigTestElement.username";

    public static final String PASSWORD = "ConfigTestElement.password";

    public ConfigTestElement() {
    }

    @Override
    public void addTestElement(TestElement parm1) {
        if (parm1 instanceof ConfigTestElement) {
            mergeIn(parm1);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void addConfigElement(ConfigElement config) {
        mergeIn((TestElement) config);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean expectsModification() {
        return false;
    }
}
View Code

Config的所有TestElement都会继承这个实现比如Arguments,CSVDataSet等

---------------------------------------------------------------现在是Config元素在engine中的执行情况----------------------------------------------------------------

PreCompiler类涉及到ConfigTestElement的操作,PreCompiler类中的AddNode方法的中涉及到Arguments的代码

1 if (node instanceof Arguments) {
2             ((Arguments)node).setRunningVersion(true);
3             Map<String, String> args = ((Arguments) node).getArgumentsAsMap();
4             replacer.addVariables(args);
5             JMeterContextService.getContext().getVariables().putAll(args);
6         }

总之,Config元素的作用就是设置参数将其放在JMeterContext中,供后面的JMeter元素使用

原文地址:https://www.cnblogs.com/liliqiang/p/4307288.html