zoukankan      html  css  js  c++  java
  • Jmeter二次开发实现自定义functions函数(九)

    在Jmeter->选项->函数助手对话框中我们可以看到Jmeter内置的一些常用函数,但考虑到测试过程中的实际情况,我们经常需要在脚本引用或者实现自定义的函数。那么如何在“函数助手对话框中”看到我们自定义的函数呢,下面将介绍详细步骤:

    Jmeter源码导入参考上一篇:https://www.cnblogs.com/xiaozhaoboke/p/14103865.html

    一、在目录..apache-jmeter-5.0srcfunctionsorgapachejmeterfunctions新建自定义class文件,例如GetPhoneNumber.class并继承AbstractFunction重写以下方法:

    package org.apache.jmeter.functions;
    
    import org.apache.jmeter.engine.util.CompoundVariable;
    import org.apache.jmeter.samplers.SampleResult;
    import org.apache.jmeter.samplers.Sampler;
    
    import java.util.Collection;
    import java.util.List;
    
    public class GetPhoneNumber extends AbstractFunction{
    
        @Override
        public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
            return null;
        }
    
        @Override
        public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
    
        }
    
        @Override
        public String getReferenceKey() {
            return null;
        }
    
        @Override
        public List<String> getArgumentDesc() {
            return null;
        }
    }

    二、实现函数逻辑后代码如下:

    package org.apache.jmeter.functions;
    
    import org.apache.jmeter.engine.util.CompoundVariable;
    import org.apache.jmeter.samplers.SampleResult;
    import org.apache.jmeter.samplers.Sampler;
    import org.apache.jmeter.threads.JMeterVariables;
    import org.apache.jmeter.util.JMeterUtils;
    
    import java.util.Collection;
    import java.util.LinkedList;
    import java.util.List;
    
    public class GetPhoneNumber extends AbstractFunction{
        //定义function名称
        private static final String KEY = "__MobileGenerator";
        private static final List<String> desc = new LinkedList<String>();
        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(",");
        //自定义function描述
        static{
            desc.add(JMeterUtils.getResString("Name of variable in which to store the result (optional)"));
        }
    
        private CompoundVariable varName;
    
        //执行部分
        @Override
        public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
            int index = getNum(0, telFirst.length - 1);
            String first = telFirst[index];
            String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
            String third = String.valueOf(getNum(1, 9100) + 10000).substring(1);
            String mobile = first + second + third;
            if (varName != null){
                JMeterVariables vars = getVariables();
                final String varTrim = varName.execute().trim();
                if (vars != null && varTrim.length() > 0) {
                    vars.put(varTrim, mobile);
                }
            }
            return mobile;
        }
    
    
        //设置参数
        @Override
        public void setParameters(Collection<CompoundVariable> parameters)throws InvalidVariableException
        {
            checkParameterCount(parameters, 0, 1);
            Object[] values = parameters.toArray();
            if (values.length > 0) {
                varName = (CompoundVariable) values[0];
            } else {
                varName = null;
            }
        }
    
        /** {@inheritDoc} */
        @Override
        public String getReferenceKey() {
            return KEY;
        }
    
        /** {@inheritDoc} */
        public List<String> getArgumentDesc() {
            return desc;
        }
    
        private static int getNum(int start,int end)
        {
            return (int)(Math.random()*(end-1));
        }
    
    }

     三、运行编译

     也可以写个测试类

     编译后得到GetPhoneNumber.class文件

     四、将编译后的class文件放到ApacheJMeter_functions.jar中

      使用WinRAR打开ApacheJMeter_functions.jar,复制GetPhoneNumber.class文件至orgapachejmeterfunctions目录下保存;

     

     五,重启Jmeter,打开函数助手

     六、学习后总结,不足之处请指出,后续修正!

             参考原文链接:https://blog.csdn.net/liujingqiu/article/details/87891378

  • 相关阅读:
    vi使用方法详细介绍
    Jenkins实现Android自动化打包
    JSON知识总结
    React Native中pointerEvent属性
    从零学React Native之06flexbox布局
    Android Http实现文件的上传和下载
    从零学React Native之05混合开发
    React Native声明属性和属性确认
    从零学React Native之04自定义对话框
    Android 在图片的指定位置添加标记
  • 原文地址:https://www.cnblogs.com/xiaozhaoboke/p/14109168.html
Copyright © 2011-2022 走看看