美文网首页JMeter学习笔记
JMeter 自定义函数

JMeter 自定义函数

作者: DC_ing | 来源:发表于2019-04-01 12:18 被阅读13次

    一、背景

    JMeter 自带很多函数,但不是所有的函数都适合我们使用,我们在测试业务时,有时候就需要处理特别的数据或者处理业务逻辑时,这时候使用自定义的函数,对测试来说,起到事半功倍的效果。

    二、准备开发环境

    1. 新建JMeter项目

    打开 IDE(我使用的是 IDEA),新建一个 maven 工程。扩展函数的Java类的报名必须是.functions,所以在工程的目录下新增包名functions,如图所示

    新建

    2. 配置 pom.xml 文件

    在配置文件的dependencies节点添加 JMeter 的依赖文件。JMeter 版本需要根据你使用的版本来决定。

    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>ApacheJMeter_core</artifactId>
        <version>${jmeter.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jmeter</groupId>
      <artifactId>ApacheJMeter_java</artifactId>
      <version>${jmeter.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.jmeter</groupId>
      <artifactId>ApacheJMeter_functions</artifactId>
      <version>${jmeter.version}</version>
    </dependency>
    

    三、函数实现

    1. 新增你的自定义函数类

    在刚才创建的functions文件夹下新建你的自定义函数类并继承父类AbstractFunction,比如:com.dc.functions,如图所示

    函数类

    下一步,就是我们往这个函数类填充我们需要实现的业务逻辑或者处理数据的方法。

    2. 实现函数逻辑

    AbstractFunction抽象类提供4个抽奖方法,在扩展的时候需要一一进行实现。

    2.1 变量部分

    log可选。该部分是运行函数时,输出日志到 JMeter 整体日志。
    desc必填,是函数的参数名字,根据需求,填写多少个。
    KEY必填,是显示的函数名称。
    values必填,声明,表示输入的函数值。

    2.2 execute方法

    public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException
    

    JMeter 会将上次运行的SampleResult和当前的Sampler作为参数传入到该方法里,返回值就是在运行该function后得到的值,以String类型返回。

    2.3 setParameters 方法

    public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException
    

    这个方法在用于传递用户在执行过程当中传入的实际参数值。该方法在function没有参数情况下也会被调用。一般该方法传入的参数会被保存在类内全局变量里,并被后面调用的execute方法中使用到。另外,我们也可以增加对传入参数的检查机制,防止出现错误。
    checkMinParameterCount检查最少需要输入的参数数量,达不到就自动报错。
    checkParameterCount检查输入的参数个数是否符合给定的数量或者范围,不符合报错。
    this.values = collection.toArray();将输入的参数值传递到value

    2.4 getReferenceKey

    public String getReferenceKey()
    

    返回就是函数的名字。JMeter的命名规则是在函数名前面加入双下划线__。比如__AddInt,函数的名字需要类名应该一致,而且该名字应该以static final的方式在实现类定义好,防止在运行过程中被修改。

    2.5 getArgumentDesc

    public List<String> getArgumentDesc()
    

    告诉 JMeter 关于你实现函数的描述。

    2.6 最终源代码实现

    实现的源代码如下所示,重要的代码已经有注释。

    package com.dc.functions;
    
    import org.apache.jmeter.engine.util.CompoundVariable;
    import org.apache.jmeter.functions.AbstractFunction;
    import org.apache.jmeter.functions.InvalidVariableException;
    import org.apache.jmeter.samplers.SampleResult;
    import org.apache.jmeter.samplers.Sampler;
    import org.apache.jmeter.threads.JMeterVariables;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.Collection;
    import java.util.LinkedList;
    import java.util.List;
    
    public class AddInt extends AbstractFunction {
        private static final Logger log = LoggerFactory.getLogger(AddInt.class);
        //显示的参数名字
        private static final List<String> desc = new LinkedList<>();
        static {
            desc.add("First int");
            desc.add("Second int");
            desc.add("Third int");
            desc.add("Result Int");
        }
        /**
         * 显示的函数名字
         */
        private static final String KEY = "__AddInt";
        /**
         * 参数值
         */
        private Object[] values;
    
        @Override
        public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
            JMeterVariables localJMeterVariables = getVariables();
            //第一个整数
            String firstInt = ((CompoundVariable)this.values[0]).execute();
            //第二个整数
            String secondInt = ((CompoundVariable)this.values[1]).execute();
            //第三个整数
            String thirdInt = ((CompoundVariable)this.values[2]).execute();
            String sumString = "";
            int a = 0;
            int b = 0;
            int c = 0;
            try {
                a = Integer.parseInt(firstInt);
                b = Integer.parseInt(secondInt);
                c = Integer.parseInt(thirdInt);
                sumString = Integer.toString(this.addInt(a, b, c));
            } catch (NumberFormatException e) {
                log.error(e.toString());
            }
            if ((localJMeterVariables != null) && (this.values.length > 0)) {
                localJMeterVariables.put(((CompoundVariable)this.values[values.length - 1]).execute(), sumString);
            }
            return sumString;
        }
    
        //设置参数值
        @Override
        public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {
            checkMinParameterCount(collection, 3);
            checkParameterCount(collection, 3);
            this.values = collection.toArray();
        }
    
        //返回函数名字
        @Override
        public String getReferenceKey() {
            return KEY;
        }
    
        //返回参数名字
        @Override
        public List<String> getArgumentDesc() {
            return desc;
        }
    
        public int addInt(int a, int b, int c) {
            return a + b + c;
        }
    }
    

    3. 打包和部署

    3.1 生成 jar 包

    代码经单元测试没问题后,执行命令生成 jar 包。可使用mvn package等命令。

    3.2 将 jar 放入到 JMeter

    将生成的 jar 包及其依赖的 jar 包,一并放入到JMETER_HOME/lib/ext/路径中,重启 JMeter 即可。也可以将生成的-jar-with-dependencies.jar放入到上述指定的 JMeter 目录中。

    3.3 使用扩展的函数

    成功重启 JMeter 后,在函数助手即可我们自定义扩展的函数。在对应的参数名称右边输入需要传入的参数值即可。编写后,复制 Function syntax 输入框的内容即可。

    函数助手

    3.4 测试自定义函数

    最后我们创建一个测试,使用Dummy Sampler,在request使用该函数,并添加查看结果树的监听器。运行成功后,在监听器上得到6即可。

    函数使用 正确结果

    相关文章

      网友评论

        本文标题:JMeter 自定义函数

        本文链接:https://www.haomeiwen.com/subject/krzmbqtx.html