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<String> desc = new LinkedList<String>();

   //定義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<CompoundVariable> parameters) throws InvalidVariableException {

      //校驗參數個數  checkParameterCount(Collection<CompoundVariable> 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<String> 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<CompoundVariable> 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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM