Jmeter (32) "Custom Function Development" of Jmeter Question

 

 

  "Technology is the support of business", this is not the first time I heard this sentence, because there are various needs, so many various technologies have been derived. mutual encouragement!

  The installation directory structure of Jmeter is mentioned earlier, and the common functions of Jmeter are also mentioned. Some tasks can be completed by using functions. If there is satisfaction, there must be dissatisfaction. This article will record the development of functions.

  First paste the built-in function diagram, the jar package is located in the ${jmeter_home}\lib\ext directory, ApacheJMeter_functions.jar

  

  There are many familiar names.

  Then pick a typical one to decompile. Random.class.

  GUI interface display content:

  

  The front end implements the Random function and defines three parameters.

  On the source code: 

 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 }

 

  Observing this code, the overall structure is roughly divided into three parts.

  Define, implement logic, return definition.

  There are several details to pay attention to when writing the implementation function code:

    1. The package name must end with functions.

    2. The class inherits AbstractFunction.

   OK, follow the cat and draw the tiger, and write the logic:

  

 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     }

    Add three positive integers and return the sum.

    Package, put the jar package into the lib\ext directory, and start Jmeter

    

  Debug it.

  

  

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324522904&siteId=291194637