jmeter二次开发函数-生成身份证号
代码参考这个
java 随机生成身份证代码
Java的身份证号码工具类
pom文件添加
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>5.6.2</version>
</dependency>
创建
import com.saic.ptest.sign.IdCardGenerator;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class IdCardGeneratorFunction extends AbstractFunction {
private static final List<String> desc = new ArrayList();
//定义函数参数列表
@Override
public List<String> getArgumentDesc() {
return desc;
}
//用来接收、处理用户调用函数时所传入的参数值
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
this.checkParameterCount(parameters, 0, 0);
}
//函数的执行主体,执行具体的业务逻辑、功能
@Override
public String execute(SampleResult sampleResult, Sampler sampler) {
IdCardGenerator idCardGenerator = new IdCardGenerator();
return idCardGenerator.generate(); //把执行结构返回给用户
}
//要调用的函数名称
@Override
public String getReferenceKey() {
String key = "__IdCardGenerator";
return key ;
}
}
打成包,放到jmeter的/lib/ext路径下
效果演示
参考的是/lib/ext路径下的
ApacheJMeter_functions.jar反编译后的文件–uuid
package org.apache.jmeter.functions;
import com.google.auto.service.AutoService;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
@AutoService({Function.class})
public class Uuid extends AbstractFunction {
private static final List<String> desc = new ArrayList();
private static final String KEY = "__UUID";
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
return UUID.randomUUID().toString();
}
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
this.checkParameterCount(parameters, 0, 0);
}
public String getReferenceKey() {
return "__UUID";
}
public List<String> getArgumentDesc() {
return desc;
}
}