jmeter二次开发函数插件Function

jmeter二次开发函数插件Function

  • maven引入
  • java代码
  • 编译
  • 将编译后的class文件放入lib/ext/ApacheJMeter_functions.jar
  • 启动jmeter查看函数助手

maven引入

 
        UTF-8
        5.1
    

    
        
            org.apache.jmeter
            ApacheJMeter_core
            ${jmeter-version}
            provided
        

        
            org.apache.jmeter
            ApacheJMeter_java
            ${jmeter-version}
            provided
        

    

java代码

注意:org.apache.jmeter.functions 是jmeter扩展功能包,所有扩展需位于此包中

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.util.JMeterUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class UrlEncrypt extends AbstractFunction{

    //定义function名称
    private static final String KEY = "__UrlEncrypt";
    private static final List desc = new LinkedList();

    static {
        desc.add(JMeterUtils.getResString("URL encrypt"));
    }

    //定义CompoundVariable 变量
    private CompoundVariable varName;

    /**
     * 根据两个入参,获取扩展函数的类型结果值。
     * SampleResult 上一个Sampler的结果,Sampler当前运行Sampler。
     * @param sampleResult
     * @param sampler
     * @return
     * @throws InvalidVariableException
     */
    public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
        String urlString = "";
        try {
            urlString = URLEncoder.encode(varName.execute().trim(), "UTF-8" );
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return urlString;
    }

    /**
     * 参数值设置,根据定义的参数名称获取对应的参数值,保存在JMeterUtils全局变量中
     * @param collection
     * @throws InvalidVariableException
     */
    public void setParameters(Collection collection) throws InvalidVariableException {
        //检查参数数量是否设置正确
        checkParameterCount(collection, 0, 1);

        Object[] values = collection.toArray();
        if (values.length > 0) {
            varName = (CompoundVariable) values[0];
        } else {
            varName = null;
        }
    }

    /**
     * 函数名称
     * @return
     */
    public String getReferenceKey() {
        return KEY;
    }

    /**
     * 函数助手界面显示函数定义
     * @return
     */
    public List getArgumentDesc() {
        return desc;
    }
}

编译

jmeter二次开发函数插件Function_第1张图片

将编译后的class文件放入lib/ext/ApacheJMeter_functions.jar

1.用WinRar打开ApacheJMeter_functions.jar
jmeter二次开发函数插件Function_第2张图片
2.进入org\apache\jmeter\functions目录
jmeter二次开发函数插件Function_第3张图片
3.将编译后的class文件复制到org\apache\jmeter\functions目录下
jmeter二次开发函数插件Function_第4张图片

启动jmeter查看函数助手

jmeter二次开发函数插件Function_第5张图片

你可能感兴趣的:(jmeter,jmeter,funtion,性能测试,jmeter)