【jmeter二次开发】二、Jmeter扩展 |
您所在的位置:网站首页 › 二次开发jmeter › 【jmeter二次开发】二、Jmeter扩展 |
import java.util.Collection; import java.util.LinkedList; import java.util.List; 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;
public class MobileGenerator extends AbstractFunction {
private static final List desc = new LinkedList(); //定义function名称 private static final String KEY = “__MobileGenerator”; private static String[] telFirst = “134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153” .split(“,”);
//自定义function的描述 static { desc.add(“Name of variable in which to store the result (optional)”); }
private CompoundVariable varName;
//执行部分 public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException { int index = getNum(0, telFirst.length - 1); String first = telFirst[index]; String second = String.valueOf(getNum(1, 888) + 10000).substring(1); String thrid = String.valueOf(getNum(1, 9100) + 10000).substring(1); String mobile = first + second + thrid;
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, mobile); } } return mobile; }
//设置参数值 public void setParameters(Collection parameters) throws InvalidVariableException { //校验参数个数 checkParameterCount(Collection parameters, int min, int max) checkParameterCount(parameters, 0, 1); //将值存入变量中 Object[] values = parameters.toArray(); if (values.length > 0) { varName = (CompoundVariable) values[0]; } else { varName = null; } }
@Override public String getReferenceKey() { return KEY; }
@Override public List getArgumentDesc() { return desc; }
private static int getNum(int start, int end) { return (int) (Math.random() * (end - 1)); } }
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |