JMeter 源碼二次開發函數示例


 

 

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

 

函數已經生效。歡迎留言討論。

公眾號:大家可以關注:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM