jmeter自定义函数--生成Email

上一篇 / 下一篇  2022-11-14 13:03:45 / 个人分类:Jmeter

packageorg.apache.jmeter.functions;

importjava.util.Collection;
importjava.util.LinkedList;
importjava.util.List;

importorg.apache.jmeter.engine.util.CompoundVariable;
importorg.apache.jmeter.samplers.SampleResult;
importorg.apache.jmeter.samplers.Sampler;
importorg.apache.jmeter.threads.JMeterVariables;
importorg.apache.jmeter.util.JMeterUtils;

public classMyEmailextendsAbstractFunction {
public staticStringbase="abcdefghijklmnopqrstuvwxyz0123456789";

private static finalString[]email_suffix="@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.net,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn"
.split(",");

public staticStringgetEmail(intlMin, intlMax) {
intlength =getNum(lMin,lMax);
StringBuffer sb =newStringBuffer();
for(inti =0;i < length;i++) {
intnumber = (int) (Math.random() *base.length());
sb.append(base.charAt(number));
}
sb.append(email_suffix[(int) (Math.random() *email_suffix.length)]);
returnsb.toString();
}

public static intgetNum(intstart, intend) {
return(int) (Math.random() * (end - start +1) + start);
}

private static finalList<String>desc=newLinkedList<>();

private static finalStringKEY="__My_GetEmail";

private static final intMAX_PARA_COUNT=3;

private static final intMIN_PARA_COUNT=2;

privateObject[]values;

privateCompoundVariablevarName;

static{
desc.add("设置最小长度");
desc.add("设置最大长度");
desc.add(JMeterUtils.getResString("function_name_paropt"));
}

publicList<String>getArgumentDesc() {
returndesc;
}

publicStringexecute(SampleResult arg0,Sampler arg1)throwsInvalidVariableException {
String email,varTrim =null;
try{
intmin = (newInteger(((CompoundVariable)this.values[0]).execute().trim())).intValue();
intmax = (newInteger(((CompoundVariable)this.values[1]).execute().trim())).intValue();
email =getEmail(min,max);
if(this.varName!=null) {
JMeterVariables vars = getVariables();
varTrim =this.varName.execute().trim();
if(vars !=null&& varTrim.length() >0)
vars.put(varTrim,email);
}
}catch(Exception ex) {
throw newInvalidVariableException(ex);
}
returnemail;
}

publicStringgetReferenceKey() {
return"__Gx_GetEmail";
}

public voidsetParameters(Collection<CompoundVariable> parameters)throwsInvalidVariableException {
checkParameterCount(parameters,2,3);
this.values= parameters.toArray();
if(this.values.length>2) {
this.varName= (CompoundVariable)this.values[2];
}else{
this.varName=null;
}
}
}

TAG: Java Jmeter jmeter java

 

评分:0

我来说两句

我的栏目

日历

« 2022-12-01  
    123
45678910
11121314151617
18192021222324
25262728293031

数据统计

  • 访问量: 720
  • 日志数: 1
  • 建立时间: 2017-11-23
  • 更新时间: 2022-11-14

RSS订阅

Open Toolbar