当前位置:网站首页>Function secondary development / plug-in development of JMeter (detailed version)

Function secondary development / plug-in development of JMeter (detailed version)

2022-07-23 17:05:00 The test is super standard

Don't go , The whole network should not explain so carefully , Give me a compliment after reading it ~

JMeter Secondary development of functions in ? What's the idea ?

1、JMeter It provides an interface for users to carry out secondary development , The interface can be introduced ApacheJMeter_components.jar、ApacheJMeter_core.jar Two packages get .( Maybe you see what others want to introduce jar There will be more bags , In fact, normal development is just these two , So at least these two bags are enough )

2、 The developed code needs to conform to JMeter The specification of : First of all , The new package name must be in functions ending ; second , Method classes need to inherit AbstractFunction class , and AbstractFunction There are four functions to be implemented in the class , Among them, the functions we want to realize need to be realized by combining these four functions .

Now let's start ~

Environmental preparation

First of all 、 Add dependency package :

Two jar Wrapped in JMeter Install under directory \lib\ext Folder , Build a new one in the project lib Catalog , hold jar Put the bag in , Then add it to the project (IDEA Add dependencies to the project jar package -CSDN Blog ):

  • ApacheJMeter_components.jar

  • ApacheJMeter_core.jar

second , Create packages with functions ending , Class inheritance created at the same time AbstractFunction class , When we inherit , If you find out, you will report an error , According to the prompt , The import method :

AbstractFunction A simple description of the four methods of class

  Function overview :

package com.functions;

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;

public class test extends AbstractFunction {
    @Override
    public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
        // The executing body of the function , Function logic processing , The final processing returns the result 
        return null;
    }

    @Override
    public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {
        // For reception 、 Handle the parameter value entered by the user when calling the function 
    }

    @Override
    public String getReferenceKey() {
        // Name of function , And the function name called when referencing 
        return null;
    }

    @Override
    public List<String> getArgumentDesc() {
        // Custom function parameter list 
        return null;
    }
}

  Case study

function : By input Area code 、 Date of birth 、 Gender , Generate a virtual ID number

Result display : 

Overall code example :

package com.functions;

import com.IdNumTool;
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.LinkedList;
import java.util.List;

public class CardToolFunctions extends AbstractFunction {
    public String card_code = "";
    public String card_birthday = "";
    public String card_sex = "";

    // Define function parameter list 
    @Override
    public List<String> getArgumentDesc() {
        List<String> parms = new LinkedList<String>();
        parms.add(" Please enter the area code ( Example :430922)");
        parms.add(" Please input birthday ( Format :xxxx-xx-xx)");
        parms.add(" Please enter gender ( male :1, Woman :2)");

        return parms;
    }

    // For reception 、 Handle the parameter value passed in when the user calls the function 
    @Override
    public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {
        //collection by getArgumentDesc The value entered by the user received by the function 
        // Check whether the parameter value entered by the user is equal to 3 individual 
        checkParameterCount(collection,3);
        // hold Collection<CompoundVariable> Convert to array , Fixed writing 
        Object[] parmsData = collection.toArray();

        // hold data Object values do CompoundVariable Cast of type , Reuse execute Convert value to String type 
        card_code = ((CompoundVariable)parmsData[0]).execute();
        card_birthday = ((CompoundVariable)parmsData[1]).execute();
        card_sex = ((CompoundVariable)parmsData[2]).execute();
    }

    // The executing body of the function , Execute specific business logic 、 function 
    @Override
    public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
        // initialization idNumTool class 
        IdNumTool idNumTool  = new IdNumTool();
        String idcard = idNumTool.getidcard(card_code,card_birthday,card_sex);

        return idcard; // Return the ID number to the user 
    }

    // The name of the function to call 
    @Override
    public String getReferenceKey() {
        String key = "__idCard";
        return key ;
    }
}

Next, let's talk about each function

getReferenceKey function

Relatively simple , Is to define the name of the function , The main thing to note is that it needs to start with two underscores , This is a JMmter According to the standard .

public String getReferenceKey() {
        String key = "__idCard";
        return key ;
}

 

getArgumentDesc function

Parameters to describe

public List<String> getArgumentDesc() {
    List<String> parms = new LinkedList<String>();
    parms.add(" Please enter the area code ( Example :430922)");
    parms.add(" Please input birthday ( Format :xxxx-xx-xx)");
    parms.add(" Please enter gender ( male :1, Woman :2)");

    return parms;
}

setParameters

Process the parameter values entered by the user

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

	// hold data Object values do CompoundVariable Cast of type , Reuse execute Convert value to String type 
	card_code = ((CompoundVariable)parmsData[0]).execute();
	card_birthday = ((CompoundVariable)parmsData[1]).execute();
	card_sex = ((CompoundVariable)parmsData[2]).execute();
}

execute Method

Specific logic implementation , Result passed return return , The specific implementation here is through IdNumTool Class implementation , This is already encapsulated , Here is just a call ;

public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
        // initialization idNumTool class 
        IdNumTool idNumTool  = new IdNumTool();
        String idcard = idNumTool.getidcard(card_code,card_birthday,card_sex);

        return idcard;
    }

export jar package

After the code is written , export jar package , Method JMeter Of lib\ext Under the table of contents , And then restart JMeter That's all right. ~

export jar The method of has been written before , Specific view :IDEA export jar Package to JMeter-CSDN Blog

Result display : 

Conclusion

Because the production of ID cards is more sensitive , It's not public here jar Bag

Learning reference :Jmeter Detailed explanation of user-defined functions of secondary development _ Bili, Bili _bilibili

原网站

版权声明
本文为[The test is super standard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231336436144.html