Jmeter tool secondary development

1. JMeter secondary development direction

1. Function development, mainly JMeter function library

2. Plug-in development, generally focusing on sampler development

3. Based on execution engine development, it effectively solves the problem of relatively complex and long-term development of the underlying execution engine in independently developed test platforms or tools. It uses the API of the JMeter execution engine to perform basic calling, monitoring, extraction and other operations to accelerate the underlying execution. Engine development efficiency.

2. Environmental preparation

JDK: Java Development Kit, which is the basis for all Java development or project running.

IntelliJ IDEA: Integrated development tool for Java project development, Eclipse can also be used.

JMeter: Performance testing tool.

3. Jmeter function development

JMeter’s own function library provides a wealth of functions and is widely used, such as __Random, __UUID, etc.

It can be tested through the menu Tools->Function Assistant dialog box.

However, the functions that come with JMeter may not necessarily meet all business needs, so it is necessary to conduct secondary development based on JMeter.

3.1. Basics of custom functions

Custom functions must inherit the AbstractFunction class and override the 4 methods of the parent class

(1) getArgumentDesc, function parameter description. If the custom function has parameters, it is used to return the function parameter description.

(2) execute, function execution logic, required, customize the core logic of the function, and return the processed content

(3) getReferenceKey, function name, required, returns a string representing the function name of the custom function in JMeter, usually starting with a double underscore, such as __Operate

(4) setParameters, set the function to receive parameter values. If the custom function has parameters, it is used to receive the parameters passed when calling. Note that when using, do not add double quotes to the string parameters.

The encoding format is as follows:

package com.functions; #com.functions is the package name
 
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
 
import java.util.Collection;
import java.util.List;


#test is a new class name, inheriting the method of AbstractFunction

public class test extends AbstractFunction {<!-- -->

 
    @Override
    public List<String> getArgumentDesc() {<!-- -->
        //Custom function parameter list
        return null;
    }
\t
@Override
    public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {<!-- -->
        //Used to receive and process the parameter values entered by the user when calling the function
    }
\t
    @Override
    public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {<!-- -->
        //The execution body of the function, function logic processing, and final processing return result
        return null;
    }
 
    @Override
    public String getReferenceKey() {<!-- -->
        //The name of the function, and the function name called when referencing
        return null;
    }

}

3.2. IDEA new project

3.3. Add dependency packages

The two jar packages are in the \lib\ext folder under the JMeter installation directory. Create a lib directory in the project, paste the jars into it, and then add dependencies in the order shown below.

ApacheJMeter_components.jar

ApacheJMeter_core.jar

3.4. Create a new package package under the src file. The name of the created package ends with functions, such as “org.apache.jmeter.functions”. The created class inherits the methods of the AbstractFunction class.

3.5. Add a new class under the created package “org.apache.jmeter.functions”, such as naming it “IsJoinFunction”, and inherit the AbstractFunction that comes with jmeter.

3.6. According to the template format:

a. Add imported module

b. Let the new class inherit AbstractFunction

c. Add 4 methods and implement specific business logic

3.7. After the code is written, compile and export the jar package

a.Configure Artifacts

b. Compile Artifacts and generate jar packages

The previous step just set up the environment to generate the jar package. Next, you need to compile the code and generate the jar package.

The generated jar is placed under out/artifacts in the project directory by default and can be copied directly~

Another: If you update the code, you only need to “compile Artifacts” directly, but if you update the jar package, you need to reconfigure the environment! !

3.8. Place the jar package in the lib\ext directory of JMeter, then restart JMeter and it will be OK.

4. Custom function demo

Function: By inputting 3 numbers, concatenate them into a three-digit number

package org.apache.jmeter.functions;


import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class IsJoinFunction extends AbstractFunction {<!-- -->
    public String numberOne = "";
    public String numbertwo = "";
    public String numberThree = "";

    //Define function parameter list
    @Override
    public List<String> getArgumentDesc() {<!-- -->
        List<String> parms = new LinkedList<String>();
        parms.add("Please enter a number (example: 1)");
        parms.add("Please enter a number (example: 2)");
        parms.add("Please enter a number (example: 3)");

        return parms;
    }

    //Used to receive and process the parameter values passed in when the user calls the function
    @Override
    public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {<!-- -->
        //collection is the value input by the user received by the getArgumentDesc function
        //Check whether the parameter value entered by the user is equal to 3
        checkParameterCount(collection,3);
        //Convert Collection<CompoundVariable> to an array, fixed writing method
        Object[] parmsData = collection.toArray();

        //Forcibly convert the value of the data object to the CompoundVariable type, and then use execute to convert the value to the String type.
        numberOne = ((CompoundVariable)parmsData[0]).execute();
        numbertwo = ((CompoundVariable)parmsData[1]).execute();
        numberThree = ((CompoundVariable)parmsData[2]).execute();
    }

    //The execution subject of the function executes specific business logic and functions
    @Override
    public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {<!-- -->
        String isJoin = numberOne + numbertwo + numberThree;
        return isJoin; //Return the execution structure to the user
    }

    //The name of the function to be called
    @Override
    public String getReferenceKey() {<!-- -->
        String key = "__isJoin";
        return key ;
    }
}

The four methods that inherit the AbstractFunction class are as follows:

4.1. getArgumentDesc function: parameter description

 public List<String> getArgumentDesc() {<!-- -->
        List<String> parms = new LinkedList<String>();
        parms.add("Please enter a number (example: 1)");
        parms.add("Please enter a number (example: 2)");
        parms.add("Please enter a number (example: 3)");

        return parms;
    }

4.2, setParameters: Process the parameter values entered by the user

 public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {<!-- -->
        //collection is the value input by the user received by the getArgumentDesc function
        //Check whether the parameter value entered by the user is equal to 3
        checkParameterCount(collection,3);
        //Convert Collection<CompoundVariable> to an array, fixed writing method
        Object[] parmsData = collection.toArray();

        //Forcibly convert the value of the data object to the CompoundVariable type, and then use execute to convert the value to the String type.
        numberOne = ((CompoundVariable)parmsData[0]).execute();
        numbertwo = ((CompoundVariable)parmsData[1]).execute();
        numberThree = ((CompoundVariable)parmsData[2]).execute();
    }

4.3. execute method: specific logic implementation, the result is returned through return

public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {<!-- -->
        String isJoin = numberOne + numbertwo + numberThree;
        return isJoin; //Return the execution result to the user
    }

4.4. getReferenceKey function: It is relatively simple, just define the name of the function. The main thing to note is that it needs to start with two underscores, which is required by the JMmter specification.

public String getReferenceKey() {<!-- -->
        String key = "__isJoin";
        return key ;
    }