Jmeter二次开发——自定义函数

这个是特别需要注意点,以.functions结尾,正常创建包即可。

二次开发时,新建的类,需要继承AbstractFunction,这个也是需要注意的。至于为什么需要继承AbstractFunction,看源码就能明白,源码如下所示:

public abstract class AbstractFunction implements Function { 

    public AbstractFunction() { 

    } 

    public abstract String execute(SampleResult var1, Sampler var2) throws InvalidVariableException;

    public String execute() throws InvalidVariableException {

        JMeterContext context = JMeterContextService.getContext();

        SampleResult previousResult = context.getPreviousResult();

        Sampler currentSampler = context.getCurrentSampler();

        return this.execute(previousResult, currentSampler);

    }

    public abstract void setParameters(Collection<CompoundVariable> var1) throws InvalidVariableException;

    public abstract String getReferenceKey();

    protected JMeterVariables getVariables() {

        return JMeterContextService.getContext().getVariables();

    }

    protected void checkParameterCount(Collection<CompoundVariable> parameters, int min, int max) throws InvalidVariableException {

        int num = parameters.size();

        if (num > max || num < min) {

            throw new InvalidVariableException(this.getReferenceKey() + " called with wrong number of parameters. Actual: " + num + (min == max ? ". Expected: " + min + "." : ". Expected: >= " + min + " and <= " + max));

        }

    }

    protected void checkParameterCount(Collection<CompoundVariable> parameters, int count) throws InvalidVariableException {

        int num = parameters.size();

        if (num != count) {

            throw new InvalidVariableException(this.getReferenceKey() + " called with wrong number of parameters. Actual: " + num + ". Expected: " + count + ".");

        }

    }

    protected void checkMinParameterCount(Collection<CompoundVariable> parameters, int minimum) throws InvalidVariableException {

        int num = parameters.size();

        if (num < minimum) {

            throw new InvalidVariableException(this.getReferenceKey() + " called with wrong number of parameters. Actual: " + num + ". Expected at least: " + minimum + ".");

        }

    }

    protected final void addVariableValue(String value, CompoundVariable[] values, int index) {

        if (values.length > index) {

            String variableName = values[index].execute().trim();

            if (StringUtils.isNotEmpty(variableName)) {

                JMeterVariables vars = this.getVariables();

                if (vars != null) {

                    vars.put(variableName, value);

                }

            }

        }

    }

获取界面所要显示的参数说明

函数的主体业务

获取函数的名称

设置参数,接收用户传递的参数

检测参数数量是否准确

名称自定义,如下所示:

private static final String key = "__XXX";

这里需要注意的是:函数开头是以2个下划线开头。

名称定义好了,那如何获取呢?就用我们刚才说的方法获取即可,如下所示:

@Override

public String getReferenceKey() {

    return key;

}

在Jmeter的函数助手中,对应函数都有对应的参数说明,如下所示:

那如何配置能实现呢?代码如下:

private final static List<String> args = new LinkedList<String>();

static{

      args.add("界面参数");

}

如果有多个参数怎么办?多个参数,多个args.add即可

获取参数名称,同样用刚才介绍的方法获取即可,如下所示:

@Override

public List<String> getArgumentDesc() {

    return args;

}

    @Override

    public void setParameters(Collection<CompoundVariable> args0) throws InvalidVariableException {

      //检测用户调用函数时,检查参数个数,个数不对则报错

      checkParameterCount(args0,3);

      Object[] params = args0.toArray();

      //转换只为string

      telNum = ((CompoundVariable)params[0]).execute();

      start = ((CompoundVariable)params[1]).execute();

      end = ((CompoundVariable)params[2]).execute();

  }

获取参数值中,可以检测函数的入参个数是否准确,不准确则会报错,报错信息如下所示:

介绍到这,就是函数的核心内容了,该函数要实现什么功能,就是在该方法中处理,示例代码如下所示:

    @Override

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

        SampleResult sampleResult1 = new SampleResult();

        try {

            sampleResult1.sampleStart();

            int index=getNum(0,telFirst.length-1);

            String telNum = telFirst[index];

            String two = String.valueOf(getNum(1, 888) + 10000).substring(1);

            String three = String.valueOf(getNum(1, 9100) + 10000).substring(1);

            tel = telNum + two + three;

            logger.info("手机号区段:"+ telNum +" 随机生成的手机号是:" + tel);

            if (varName != null) {

                JMeterVariables vars = getVariables();

                final String varTrim = varName.execute().trim();

                if (vars != null && varTrim.length() > 0) {

                    vars.put(varTrim, telNum);

                }

            }

            sampleResult1.setResponseData("手机号区段:"+ telNum +" 随机生成的手机号是:" + tel,"utf-8");

            sampleResult1.setSuccessful(true);

        }catch (Exception e){

            sampleResult.setSuccessful(false);

            e.printStackTrace();

        }finally {

            sampleResult1.sampleEnd();

        }

        return tel;

    }

写到这里,基本完成了,但还是得测试下,功能是否正常,如果先打jar包,丢到Jmeter中,发现有bug的话,来来回回处理,就折腾了,所以还是需要先测试下的。

在test下新建测试类,示例代码如下所示:

import org.junit.Test;

public class Function_Test {

    @Test

    public void phoneTest() throws Exception {

        RandomPhoneJmeterFunctions randomPhone= new RandomPhoneJmeterFunctions();

        String phoneString = randomPhone.execute();

        System.out.println("随机手机号:" + phoneString);

    }

}

测试代码很简单,运行测试类,没有报错并打印出手机号,则说明没有问题。运行后的结果如下所示:

生成jar包就不重复讲了,可以看以前的博文,IDEA的基本操作——导入导出jar包

代码写好后,自然是要在jmeter中验证下功能的,我们将生成的jar包放到jmeter的\lib\ext文件夹下,如果jmeter已启用,则需要重启哦,不然不会生效。

打开jmeter后,使用函数助手,看新开发的函数是否有展示,如下所示:

生成函数变量,操作如下所示:

新建线程组,并添加http请求,验证码生成的手机号是不是随机的,运行后,查看结果树,如下所示:

也可以通过日志查看,开发的时候,加了响应日志,如下所示:

到此,就说明功能没问题了。函数开发按上述步骤就可以完成,遇到不满足测试场景的时候,就可以自己diy一个了。

最后附上完整代码,如下所示:

private static Logger logger = LogManager.getLogger(RandomPhoneJmeterFunctions.class.getName());

    private String tel;

    //定义函数名称

    private static final String KEY = "__RandomPhone";

    //定义函数界面显示的参数名称

    private static final List<String> desc = new LinkedList<String>();

    static{

        desc.add("界面参数");

    }

    private static final String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153 ".split(",");

    private CompoundVariable varName;

    //业务主逻辑

    @Override

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

        SampleResult sampleResult1 = new SampleResult();

        try {

            sampleResult1.sampleStart();

            int index=getNum(0,telFirst.length-1);

            String telNum = telFirst[index];

            String two = String.valueOf(getNum(1, 888) + 10000).substring(1);

            String three = String.valueOf(getNum(1, 9100) + 10000).substring(1);

            tel = telNum + two + three;

            logger.info("手机号区段:"+ telNum +" 随机生成的手机号是:" + tel);

            if (varName != null) {

                JMeterVariables vars = getVariables();

                final String varTrim = varName.execute().trim();

                if (vars != null && varTrim.length() > 0) {

                    vars.put(varTrim, telNum);

                }

            }

            sampleResult1.setResponseData("手机号区段:"+ telNum +" 随机生成的手机号是:" + tel,"utf-8");

            sampleResult1.setSuccessful(true);

        }catch (Exception e){

            sampleResult.setSuccessful(false);

            e.printStackTrace();

        }finally {

            sampleResult1.sampleEnd();

        }

        return tel;

    }

    //获取参数值

    @Override

    public void setParameters(Collection<CompoundVariable> args0) throws InvalidVariableException {

        //检测用户调用函数时,检测参数个数

        checkParameterCount(args0,1);

        Object[] params = args0.toArray();

        if (params.length > 0) {

            varName = (CompoundVariable) params[0];

        } else {

            varName = null;

        }

    }

    //获取函数的名称

    @Override

    public String getReferenceKey() {

        return KEY;

    }

    //获取界面所要显示的参数说明

    @Override

    public List<String> getArgumentDesc() {

        return desc;

    }

    private static int getNum(int start,int end)

    {

        return (int)(Math.random()*(end-1));

    }

USB Microphone https://www.soft-voice.com/

Wooden Speakers  https://www.zeshuiplatform.com/

亚马逊测评 www.yisuping.cn

深圳网站建设www.sz886.com

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容