Jmeter 函數(shù)助手提供了一系列功能,比如csv參數(shù)文件讀取、Md5加密等等,工作上可能會(huì)遇到其他jmeter尚未實(shí)現(xiàn)的功能,為了更簡(jiǎn)便的完成工作,可以選擇擴(kuò)展Jmeter

函數(shù)助手
org.apache.jmeter.functions 是jmeter擴(kuò)展功能包,所有擴(kuò)展需位于此包中

image.png
Jmeter擴(kuò)展AbstractFunction類
抽象類AbstractFunction包括四個(gè)抽象方法,需要在類中分別實(shí)現(xiàn)
//參數(shù)值設(shè)置,根據(jù)定義的參數(shù)名稱獲取對(duì)應(yīng)的參數(shù)值,保存在JMeterUtils全局變量中
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException;
//函數(shù)名稱,比如_CSVRead
public String getReferenceKey();
/*根據(jù)兩個(gè)入?yún)?,獲取擴(kuò)展函數(shù)的<String>類型結(jié)果值。
SampleResult 上一個(gè)Sampler的結(jié)果,Sampler當(dāng)前運(yùn)行Sampler。*/
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException;
//函數(shù)助手界面顯示函數(shù)定義
public List<String> getArgumentDesc()
基于源碼解讀
通過_Random功能源碼分析具體實(shí)現(xiàn)

_Random
package org.apache.jmeter.functions;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
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;
/**
* Provides a Random function which returns a random long integer between a min
* (first argument) and a max (second argument).
* @since 1.9
*/
public class Random extends AbstractFunction {
private static final List<String> desc = new LinkedList<>();
//定義函數(shù)名稱
private static final String KEY = "__Random"; //$NON-NLS-1$
//初始化代碼塊定義函數(shù)名稱
static {
desc.add(JMeterUtils.getResString("minimum_param")); //$NON-NLS-1$
desc.add(JMeterUtils.getResString("maximum_param")); //$NON-NLS-1$
desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$
}
//定義CompoundVariable 變量
private CompoundVariable varName;
private CompoundVariable minimum;
private CompoundVariable maximum;
/**
* No-arg constructor.
*/
public Random() {
}
/** {@inheritDoc}
具體實(shí)現(xiàn)代碼
*/
@Override
public String execute(SampleResult previousResult, Sampler currentSampler)
throws InvalidVariableException {
long min = Long.parseLong(minimum.execute().trim());
long max = Long.parseLong(maximum.execute().trim());
long rand = ThreadLocalRandom.current().nextLong(min, max+1);
//計(jì)算得到結(jié)果隨機(jī)數(shù)
String randString = Long.toString(rand);
/**
結(jié)果保存到JMeterVariables 并發(fā)線程組參數(shù)中,可以函數(shù)名稱的方式調(diào)用
*/
if (varName != null) {
JMeterVariables vars = getVariables();
final String varTrim = varName.execute().trim();
if (vars != null && varTrim.length() > 0){// vars will be null on TestPlan
//put方法,key/value類型,可使用${varTrim}方式獲取randString值
vars.put(varTrim, randString);
}
}
return randString;
}
/** {@inheritDoc}
參數(shù)值獲取,以及參數(shù)檢查
*/
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
//檢查參數(shù)數(shù)量是否設(shè)置正確
checkParameterCount(parameters, 2, 3);
Object[] values = parameters.toArray();
//獲取具體參數(shù)值
minimum = (CompoundVariable) values[0];
maximum = (CompoundVariable) values[1];
if (values.length>2){
varName = (CompoundVariable) values[2];
} else {
varName = null;
}
}
/** {@inheritDoc}
返回?cái)U(kuò)展函數(shù)名稱
*/
@Override
public String getReferenceKey() {
return KEY;
}
/** {@inheritDoc}
函數(shù)助手界面展示
*/
@Override
public List<String> getArgumentDesc() {
return desc;
}
}