zoukankan      html  css  js  c++  java
  • Jmeter(三十二)Jmeter Question 之 “自定义函数开发”

      “技术是业务的支撑”,已经不是第一次听到这句话,因为有各种各样的需求,因此衍生了许多各种各样的技术。共勉!

      前面有提到提到过Jmeter的安装目录结构,也提到Jmeter的常用函数功能,有部分工作使用函数便可以完成,有满足,那肯定是有不满足的,本篇来记录函数的开发。

      先贴内置函数图,该jar包位于${jmeter_home}libext目录下,ApacheJMeter_functions.jar

      

      有很多熟悉的名字。

      那么就挑一个典型的来反编译一下。Random.class。

      GUI界面显示内容:

      

      前端实现Random函数,定义个三个参数。

      上源码: 

     1 //
     2 // Source code recreated from a .class file by IntelliJ IDEA
     3 // (powered by Fernflower decompiler)
     4 //
     5 
     6 package org.apache.jmeter.functions;
     7 
     8 import java.util.Collection;
     9 import java.util.LinkedList;
    10 import java.util.List;
    11 import java.util.concurrent.ThreadLocalRandom;
    12 import org.apache.jmeter.engine.util.CompoundVariable;
    13 import org.apache.jmeter.samplers.SampleResult;
    14 import org.apache.jmeter.samplers.Sampler;
    15 import org.apache.jmeter.threads.JMeterVariables;
    16 import org.apache.jmeter.util.JMeterUtils;
    17 
    18 public class Random extends AbstractFunction {
    19     private static final List<String> desc = new LinkedList();
    20     private static final String KEY = "__Random";
    21     private CompoundVariable varName;
    22     private CompoundVariable minimum;
    23     private CompoundVariable maximum;
    24 
    25     public Random() {
    26     }
    27 
    28     public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    29         long min = Long.parseLong(this.minimum.execute().trim());
    30         long max = Long.parseLong(this.maximum.execute().trim());
    31         long rand = ThreadLocalRandom.current().nextLong(min, max + 1L);
    32         String randString = Long.toString(rand);
    33         if (this.varName != null) {
    34             JMeterVariables vars = this.getVariables();
    35             String varTrim = this.varName.execute().trim();
    36             if (vars != null && varTrim.length() > 0) {
    37                 vars.put(varTrim, randString);
    38             }
    39         }
    40 
    41         return randString;
    42     }
    43 
    44     public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
    45         this.checkParameterCount(parameters, 2, 3);
    46         Object[] values = parameters.toArray();
    47         this.minimum = (CompoundVariable)values[0];
    48         this.maximum = (CompoundVariable)values[1];
    49         if (values.length > 2) {
    50             this.varName = (CompoundVariable)values[2];
    51         } else {
    52             this.varName = null;
    53         }
    54 
    55     }
    56 
    57     public String getReferenceKey() {
    58         return "__Random";
    59     }
    60 
    61     public List<String> getArgumentDesc() {
    62         return desc;
    63     }
    64 
    65     static {
    66         desc.add(JMeterUtils.getResString("minimum_param"));
    67         desc.add(JMeterUtils.getResString("maximum_param"));
    68         desc.add(JMeterUtils.getResString("function_name_paropt"));
    69     }
    70 }

      观察这段代码、整体结构大致分为三个部分。

      定义、实现逻辑、返回定义。

      在编写实现函数代码的时候需要注意的几个细节:

        1、包名必须是以functions结尾。

        2、类继承AbstractFunction。

       OK,照猫画虎,编写其中的逻辑:

      

     1 String result = "";
     2 
     3         try {
     4             int a = Integer.valueOf(str1).intValue();
     5             int b = Integer.valueOf(str2).intValue();
     6             int c = Integer.valueOf(str3).intValue();
     7             result = String.valueOf(addInt(a, b, c));
     8         } catch (Exception e) {
     9             // TODO Auto-generated catch block
    10             e.printStackTrace();
    11         } 
    12         
    13         if ((localJMeterVariables != null) && (this.values.length > 0)) {
    14             localJMeterVariables.put(str4, result);
    15         }
    16 
    17         return result;
    18     }
    19 
    20     private int addInt(int a, int b, int c) {
    21         // TODO Auto-generated method stub
    22         return  a + b + c;
    23     }

        三个正整数相加,返回和。

        打包,将jar包放入libext目录下,启动Jmeter

        

      调试一下。

      

      

      像外行一样去思考,像专家一样去实践。

                    ----金出武雄

  • 相关阅读:
    软件工程第一次实验——软件开发文档与工具的安装与使用
    软件工程第四次作业——ATM管理系统
    软件工程第三次作业——举例分析流程图与活动图的区别与联系
    软件工程第二次作业——小学四则运算题目生成程序
    mac终端进入mysql
    计算机网络
    类和对象(未完成)
    html_css
    Mac下使用C语言生成和使用动态链接库
    模运算
  • 原文地址:https://www.cnblogs.com/richered/p/8675840.html
Copyright © 2011-2022 走看看