前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【测开技能】Jmeter二次开发之自定义函数

【测开技能】Jmeter二次开发之自定义函数

作者头像
雷子
发布2022-02-11 14:18:55
7050
发布2022-02-11 14:18:55
举报
文章被收录于专栏:雷子说测试开发

前言

在之前分享中,我们都是分享一些使用,也有一些二开的内容。在我们作为测试开发工程师,工具的二次开发是必不可少的,今天,带着大家去自定义一个Jmeter的自定义函数。

正文

首先依赖需要,ApacheJMeter_core.jar以及ApacheJMeter_functions.jar两个包,功能本身所依赖的包就是功能实现的jar包,功能本身是脱离JMeter存在的。我们在创建项目的时候注意:

1.package中必须包含关键字functions

2.创建类,继承自AbstractFunction。

创建一个项目,然后创建一个lib目录,在lib中放入这两个文件,

添加作为

我们去创建一个类AddInt.java,内容如下

代码语言:javascript
复制
package org.example.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 java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class AddInt extends AbstractFunction {
    //显示的参数名字
    private static final List<String> desc = new LinkedList<>();
    static {
        desc.add("First int");
        desc.add("Second int");
        desc.add("Third 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) {
        }
        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;
    }
}

其实很简单就是实现了三个数相加,那么我们打包成jar包。放在lib下的ext目录下。

代码语言:javascript
复制
小技巧:
如果希望将依赖包放在lib以外的目录,
  则建议修改Jmeter的配置文件jmeter.properties. 中 user.classpath
自定义函数,其package中必须包含关键字functions具体以Jmeter.properties文件中的配置项为准。
classfinder.functions.contain=.functions

然后我们启动jmeter。输入对应的参数,我们这里只是简单的演示如何自定义。

可以看到,AbstractFunction需要重写的方法的含义为

代码语言:javascript
复制
getArgumentDesc()
获取界面所要显示的参数说明
execute()
函数的主体业务
getReferenceKey()
获取函数的名称
setParameters()
设置参数,接收用户传递的参数
checkParameterCount()
检测参数数量是否准确

这里只是简单的实现一个自定义函数,下次给大家分享一个如何自定义一个java请求。

后续

最近的分享会在一些技术深度上分享,一些开发框架的教程也会对应的更新出来。

发现问题,解决问题。遇到问题,慢慢解决问题即可。

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

本文分享自 雷子说测试开发 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档