中文文档,请查看:JMeter: 插件开发,看完这一篇,就可以入门了,绝对干货!!
Here are the clear & brief sample to create custom JMeter plugin.
The plugins here are very handy, something like: Hello World
1. Hello Configor: A Config Element, and user could input hello words, and JMeter read the words, and save to parameter
2. Hello Function: A custom function __hello to fulfil: Hello xxx, today is xxx
3. Hello Java Sampler: A Java Sampler to fulfil: Hello xxx, today is xxx
4. Hello Sampler: A Sampler to fulfil: Hello xxx, today is xxx
Use maven to build, then HelloJMterPlugin-x.x.x.jar could be ready
mvn clean package
After test, the jar is compatible with JMeter 2.x, 3.x, 4.x, 5.x
Once building success, copy the HelloJMterPlugin-x.x.x.jar to <JMeterHome>/lib/ext
Here are the best practise for pom.xml
-
For JMeter Dependencies
<dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_core</artifactId> <version>5.2.1</version> <scope>provided</scope> </dependency>
-
version: Choose a proper version based on your project
-
scope: provided
Because JMeter itself could provide the jar, no need to export
For more info, please refer to: https://howtodoinjava.com/maven/maven-dependency-scopes/
-
All the JMeter jar files are here: https://mvnrepository.com/artifact/org.apache.jmeter
If you confused about which jar provide which functions, check here:
-
Simply see what content in it:
jar tf xxx.jar
-
For java doc: https://javadoc.io/doc/org.apache.jmeter
-
For download java doc (rename jar to zip, and unzip): https://repo1.maven.org/maven2/org/apache/jmeter/
-
For official API documentation: https://jmeter.apache.org/api/
For example:
ApacheJMeter_core.jar: All the core logic and mechanism
ApacheJMeter_java.jar: All the core protocol mechanism
ApacheJMeter_components.jar: All the component, such as assertion, pre/post processors, listener
-
-
-
Maven Build Plugin
-
maven-compiler-plugin:
Mandatory, compile the source, more info: https://maven.apache.org/plugins/maven-compiler-plugin/
-
maven-shade-plugin:
Suggested, provides the capability to package the artifact in an uber-jar, more info: https://maven.apache.org/plugins/maven-shade-plugin/
-
A Config Element, and user could input hello words, and JMeter read the words, and save to parameter
A JMeter config element needs at least 3 parts:
-
<YourConfigName>.java
Here is: HelloConfigor.java
All the core logic are here, such as define/get/process user input
-
<YourConfigName>BeanInfo.java
Here is: HelloConfigorBeanInfo
Set the default values for the user input
-
<YourConfigName>Resources.properties
Here is: HelloConfigorResources.properties
Set the JMeter GUI info, such as label name, comment
A custom function __Hello to fulfil: Hello xxx, today is xxx
-
Only one java file to make things ready: HelloFunction.java
The java mainly needs to do the 3 things:
-
Show the custom function name in Function Helper
-
Show the parameter description in Function Helper
-
Core logic in "execute"
-
-
Note: For jmeter functions, all the source files should be put into: "functions" folder
A Java Sampler to fulfil: Hello xxx, today is xxx
Only 1 java file to make things ready: HelloJavaSampler.java
Contain 4 main parts:
-
getDefaultParameters
Set the default values for parameters, if empty, please set null
-
setupTest
This is where you read test parameters and initialize your test client. JMeter calls this method only once for each test thread
-
teardownTest
Clear the mess after the test
-
runTest
All the core logic put here
A Sampler to fulfil: Hello xxx, today is xxx
Here are the things we need to know:
-
Suggest to use Java Sampler instead if possible, that is much simple
-
For more info about how to create sampler: https://github.com/apache/jmeter/tree/master/src/examples/src/main
-
The Hello Sampler contains 2 java files:
-
-
Should override "sample"
-
getTitle: get sampler lable
-
getHello: get user input words
-
-
-
Should override "getStaticLabel" and "getLabelResource" to set the sampler lable
-
Should override "configure" to get user put
-
Should override "createTestElement" and "modifyTestElement" to create new sampler, and save user input to JMeter properties
-
Should override "clearGui"
-
Should create "createDataPanel" to set GUI info, such as label for parameters
-
-
if you think this really do a help to you. Please give the repo a STAR 🌟
Thank you in advance. 🥳