Jmeter二次开发

Jmater函数扩展的步骤
1、 导入Jmeter源码,或使用maven项目,引入依赖的jar包

2、 继承AbstractFunction,实现自定义Function

3、 继承JMeterTestCase,对自定义的函数进行单元测试

4、 对自定义函数进行编译打包,并放在lib\ext下

5、 Jmeter ->选项 ->函数助手对话框,选择已开发的函数调用

 

自定义Function
继上一篇文章已经介绍如何导入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));

   }

}

 

 

对开发的函数进行单元测试
import static org.apache.jmeter.functions.FunctionTestHelper.makeParams;

import java.util.Collection;

import org.apache.jmeter.engine.util.CompoundVariable;

import org.apache.jmeter.junit.JMeterTestCase;

import org.junit.Test;

 

public class MobileGeneratorTest extends JMeterTestCase {

  

   @Test

   public void test() throws Exception {

      MobileGenerator mobileGenerator = new MobileGenerator();

      Collection params = makeParams("mobile",null,null);

      mobileGenerator.setParameters(params);

      String string = mobileGenerator.execute();

      System.out.println(string);

   }

 

}

 

编译并打包
   编译并打包到lib\ext目录下,覆盖下之前的ApacheJMeter_functions.jar,重启Jmeter

 

调用自定义函数

---------------------
作者:Jerry最胖
来源:CSDN
原文:https://blog.csdn.net/qq_27791709/article/details/78952054
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/linwenbin/p/10712581.html

你可能感兴趣的:(java,测试)