在jmeter使用过程中,遇到一些无法使用jmeter函数助手解决的问题,那么就需要自己开发一个jmeter函数,用于解决一些问题
如何进行jmeter函数的二次开发详情见如下代码:其实个人建议可以下载jmeter源代码
见如下目录D:\tools\apache-jmeter-5.6.3_src\apache-jmeter-5.6.3\src\functions\src\main\java\org\apache\jmeter\functions
package org.apache.jmeter.functions; import java.util.ArrayList; import java.util.Collection; 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; //import com.google.auto.service.AutoService; /** * Provides a Random function which returns a random long integer between a min * (first argument) and a max (second argument). * @since 1.9 */ //@AutoService(Function.class) public class Random extends AbstractFunction { private static final List<String> desc = new ArrayList<>(); private static final String KEY = "__Random"; //$NON-NLS-1$ 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$ } private CompoundVariable varName; private CompoundVariable minimum; private CompoundVariable maximum; /** * No-arg constructor. */ public Random() { } /** {@inheritDoc} */ @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); String randString = Long.toString(rand); if (varName != null) { JMeterVariables vars = getVariables(); final String varTrim = varName.execute().trim(); if (vars != null && varTrim.length() > 0){// vars will be null on TestPlan vars.put(varTrim, randString); } } return randString; } /** {@inheritDoc} */ @Override public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException { checkParameterCount(parameters, 2, 3); Object[] values = parameters.toArray(); minimum = (CompoundVariable) values[0]; maximum = (CompoundVariable) values[1]; if (values.length>2){ varName = (CompoundVariable) values[2]; } else { varName = null; } } /** {@inheritDoc} */ @Override public String getReferenceKey() { return KEY; } /** {@inheritDoc} */ @Override public List<String> getArgumentDesc() { return desc; } }