JMeter 源码二次开发函数示例

时间:2024-03-11 13:08:10

 

 

JMeter 源码二次开发函数示例

一、JMeter 5.0 版本

 


实际测试中,依靠jmeter自带的函数已经无法满足我们需求,这个时候就需要二次开发。本次导入的是jmeter 5.0的源码进行实际的函数开发。

二、开发函数

 

在src/functions新建class-IntThreeSum(函数求和)

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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName IntThreeSum三个参数求和
 * @Description TODO
 * @Author Hardy.Feng
 * @Date 2018/12/20 14:00
 * @Version 1.0
 **/

public class IntThreeSum extends AbstractFunction { //继承AbstractFunction类,必须对父类的代码重写
    
    private static final Logger log = LoggerFactory.getLogger(IntThreeSum.class);

    private static final List<String> desc = new LinkedList<>();    //描述
    private static final String KEY = "__IntThreeSum";  //方法描述,必须双下划线

    static {
        
        desc.add(JMeterUtils.getResString("first_param")); 
        desc.add(JMeterUtils.getResString("secone_param")); 
        desc.add(JMeterUtils.getResString("third_param")); 
    }

    private Object[] values;

    public IntThreeSum() {
    }

    @Override
    public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {

        JMeterVariables vars = getVariables();

        int sum = 0;
        String varName = ((CompoundVariable) values[values.length - 1]).execute().trim();
        log.info("varName==>:{}", varName);

        //遍历获取3个数之和
        for (int i = 0; i < values.length - 1; i++) {
            sum += Integer.parseInt(((CompoundVariable) values[i]).execute());
        }

        try {
            sum += Integer.parseInt(varName);
            varName = null;
        } catch (NumberFormatException ignored) {

        }

        String totalString = Integer.toString(sum);
        if (vars != null && varName != null) {
            vars.put(varName.trim(), totalString);
            log.info("varName:", vars.get(varName.trim()));
        }
        return totalString;
    }

    @Override
    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
        //对入参进行检查,最小3个参数
        checkMinParameterCount(parameters,3);
        values = parameters.toArray();

    }

    @Override
    public String getReferenceKey() {
        return KEY;
    }

    @Override
    public List<String> getArgumentDesc() {
        return desc;
    }
}

三、测试类Test

 

在test/src新建测试类IntThreeSumTest

package org.apache.jmeter.functions;

import org.apache.jmeter.JMeter;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.junit.JMeterTestCase;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.LoggerFactory;

import static org.apache.jmeter.functions.FunctionTestHelper.makeParams;

import java.util.Collection;

/**
 * @ClassName IntThreeSumTest
 * @Description TODO
 * @Author hardy.Feng
 * @Date 2018/12/20 14:30
 * @Version 1.0
 **/

public class IntThreeSumTest extends JMeterTestCase {

    @Test
    public void sumTest() throws Exception {
        IntThreeSum intThreeSum = new IntThreeSum();
        checkInvalidParameterCounts(intThreeSum,3); //检查一下参数最小入参3位
        Collection<CompoundVariable> params = makeParams("1","2","3");
        intThreeSum.setParameters(params);
        String totalString = intThreeSum.execute();
        System.out.println("total:" + totalString);

    }
}

四、导出jar文件

如果测试类测试没有问题,就可以导出class为jar文件。放到lib/ext目录下面,如下图所示

 

五、启动jmeter

 

函数已经生效。欢迎留言讨论。

公众号:大家可以关注: