当前位置:网站首页>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
边栏推荐
- 拼多多APP商品详情接口获取activity_id值(拼多多activity_id接口)
- 系统内存介绍和内存管理
- Weisfeiler-Lehman图同构测试及其他
- 怎么正确设置路由器
- It's not safe to open an account at qiniu business school
- Nodejs implements token login registration (koa2)
- sprintf和cv::putText
- MATLAB基础
- Taishan Office Technology Lecture: layout drawing analysis of paragraph borders
- Notes on Microcomputer Principle and technical interface
猜你喜欢

软件测试计划包括哪些内容,测试计划如何编写。分享测试计划模板

Microcomputer principle and technical interface practice in class

合宙ESP32C3基于VSCode PIO Arduino开发框架初探教程

通过SSH方式访问内网RDS+mysql

Scale Match for Tiny Person Detection

SSD: Single Shot MultiBox Detector

解决data functions should return an object 并(Property “visible“ must be accessed with “$data.visible“)

Direct exchange

数组和特殊矩阵的压缩存储

虾皮二面:JVM内存布局你知道的都说一下?
随机推荐
ROS2自学笔记:RQT可视化工具
面试官:生成订单30分钟未支付,则自动取消,该怎么实现?
深度学习学习记录-优化器的学习率的更新
Lake Shore—EMPX-H2 型低温探针台
移动、电信、联通:5G To B的花式解法
How does MySQL query data that is not in the database?
Pinia (pineapple)
Is it safe for online account managers to open accounts when choosing securities companies in flush
SSD: Single Shot MultiBox Detector
Visualization of gross domestic product (GDP) data
使用“soup.h1.text”爬虫提取标题会多一个\
AutoCAD基本操作
灰色关联分析(MATLAB)
MySQL中有几种常见的 SQL 错误用法?
Lake Shore - empx-h2 low temperature probe station
灰色预测(MATLAB)
腾讯撕开中国NFT的“遮羞布”
Is PMP a great help for practical work?
CSR、SSR 与 SSG
Tensorflow2.x actual combat series softmax function
