如何使用Java 开发Apache JMeter Function插件
一.开发JMeter函数插件的目的
有时候,JMeter自带的插件不能满足自动化/性能测试的需求,这个时候就需要自己来开发插件,解决这个问题
二.什么是Function组件?
通过打开函数助手,我们可以通过下拉菜单,查看JMeter为我们默认提供的一系列实用的函数功能,使用函数非常简单,比如”__testMobile”函数,功能是生成一个手机号码,在后面的输入框中只需输入”${__
testMobile()}”便可以引入调用该函数方法所返回的值,所有的组件都可以对函数组件进行引入
不带参数的函数
带参数的函数
三.接下来我们来实现上面两个函数插件
介绍下我使用的是IDEA 2021版本,JDK1.8;创建MAVEN项目编译打包
1、首先创建Java工程,File>New>Project,选择maven工程,next>命名为hs-jmeter-auto>next
2、先在Java工程中引入主要的JMeter插件开发jar包【Apache JMeter Core和 Apache JMeter Java】,可在https://mvnrepository.com/搜索需要的jar包
3.代码实现
创建package hg.auto.functions,在该包下创建CarId.Java类继承AbstractFunction类,并重写该类的execute/setParameters/getReferenceKey/getArgumentDesc方法
打包后,把jar放到JMeter lib下的ext下,重新启动JMeter
最后附上代码
/**
?*@author derrick
?*/
public class CarId extends AbstractFunction {
??? private static final List<String> desc = new LinkedList<>();
??? /**在函数助手中的显示名称**/
??? private static final String KEY = "__CarId";
??? static{
??????? //函数参数-省份简称
??????? desc.add(JMeterUtils.getResString("Province_param"));
??????? //函数参数-市级ID
??????? desc.add(JMeterUtils.getResString("CityId_param"));
??????? desc.add(JMeterUtils.getResString("function_name"));
??? }
??? private CompoundVariableProvince_param;
??? privateCompoundVariable CityId_param;
??? privateCompoundVariable varName;
??? @Override
??? public List<String>getArgumentDesc() {
??????? return desc;
??? }
??? @Override
??? public String execute(SampleResult
previousResult, Sampler currentSampler) throws InvalidVariableException{
??????? //获取所输入参数-省级简称
??????? String province = Province_param.execute().trim();
??????? //获取所输入参数-市级ID
??????? String city = CityId_param.execute().trim();
??????? //实现逻辑不变
??????? char[] license = new char[5];
??????? inti = 0;
??????? intk = 0;
??????? intn = 0;
??????? for(; i < 5; i++) {
??????????? if (Math.random() >0.5) {
??????????????? if (k < 3) {
??????????????????? license[i] = (char) ('A' + Math.random() *26);
??????????????????? k++;
??????????????? } else {
??????????????????? license[i] = (char) ('0' + Math.random() *10);
??????????????????? n++;
??????????????? }
??????????? }else {
??????????????? if (n < 4) {
??????????????????? license[i] = (char) ('0' + Math.random() *10);
??????????????????? n++;
??????????????? } else {
??????????????????? license[i] = (char) ('A' + Math.random() *26);
??????????????????? k++;
??????????????? }
??????????? }
??????? }
??????? String licenses = String.valueOf(license);
??????? String carId = province+city+licenses;
??????? if(varName != null) {
??????????? JMeterVariables vars =getVariables();
??????????? finalString varTrim = varName.execute().trim();
??? ????????if(vars != null && varTrim.length() > 0) {
??????????????? vars.put(varTrim, carId);
??????????? }
??????? }
??????? return carId;
??? }
??? @Override
??? public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException{
??????? checkParameterCount(parameters, 2, 3);
??????? Object[] values = parameters.toArray();
??????? Province_param =
(CompoundVariable) values[0];
??????? CityId_param =
(CompoundVariable) values[1];
??????? if(values.length > 2) {
??????????? varName =
(CompoundVariable) values[2];
??????? } else {
??????????? varName = null;
??????? }
??? }
??? @Override
??? public String getReferenceKey() {
??????? return KEY;
??? }
}
随机生成手机号码
/**
?*@author created by derrick
?*@date 2021年4月19日---上午11:45:40
?*/
public class MobileFunction extends AbstractFunction {
??? /** function名称 */
??? private static final String KEY = "__testMobile";
??? private static finalList<String> desc = new LinkedList<String>();
??? private static finalString[] 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(JMeterUtils.getResString("derrick test"));
??? }
??? private CompoundVariablevarName;
??? /** 执行部分 */
??? @Override
??? public String execute(SampleResult
previousResult, Sampler currentSampler) throws InvalidVariableException{
??????? int index =getNum(telFirst.length - 1);
??????? String first = telFirst[index];
??????? String second = String.valueOf(getNum(999) + 10000).substring(1);
??????? String third = String.valueOf(getNum(9990) + 10000).substring(1);
??????? String mobile = first + second + third;
??????? if(varName != null) {
??????????? JMeterVariables vars =getVariables();
??????????? finalString varTrim = varName.execute().trim();
??????????? if(vars != null && varTrim.length() > 0) {
??????????????? vars.put(varTrim, mobile);
??????????? }
??????? }
??????? return mobile;
??? }
??? /** 设置参数 */
??? @Override
??? public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException{
??????? 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<String>getArgumentDesc() {
??????? return desc;
??? }
??? private static int getNum(int end) {
??????? return (int) (Math.random() *(end -1));
??? }
}