I. BACKGROUND
With JMeter, we have our own Java samplers to use, but if we need to reference our own classes in our Java sampler, then customizing your own Java sampler is a good choice. Of course we can also rewrite the HTTP sampler method, here with a custom Java sampler for example
Second, step 1. Add the required packages into the build path of your Java project
The jar packages that need to be added into the build path are the following three
ApacheJMeter_components.jarApacheJMeter_core.jarApacheJMeter_java.jar
However, due to concerns about the dependency of the jar package, it is recommended that you import all the JMeter installation directories \lib and the jar files under \lib\ext\, as follows
2. Writing Custom Java Sampler Code
Before I wrote the custom Java sampler Code, I customized a Hello class to implement a simple, specific string print with the following code
Package Com.selfdefined.components;public class Hello {public String SayHello () {return "Hello";} public string Sayhello2person (string name) {if (name = = NULL | | name.equals ("")) {name = "nobody";} Return (new StringBuilder ()). Append ("Hello"). Append (name). toString (); public int sum (int a, int b) {return a + B;}}
Customize the Java Sampler Code in a detailed description:
Custom Java samplers require the use of the four classes Arguments, Abstractjavasamplerclient, Javasamplercontext, Sampleresult, and the need to rewrite public Arguments Getdefaultparameters () {}, public void Setuptest (Javasamplercontext arg0) {}, public Sampleresult runtest ( Javasamplercontext arg0) {}, public void Teardowntest (Javasamplercontext arg0) {} These methods work as follows:
2.1 Public Arguments Getdefaultparameters () {}
Define the variables to be entered and their default values
2.2 public void Setuptest (Javasamplercontext arg0) {}
Define the work you need to do before you specify a test, which can be empty
2.3 Public Sampleresult runtest (Javasamplercontext arg0) {}
Functions that need to be executed when performing tests
2.4 public void Teardowntest (Javasamplercontext arg0) {}
Cleanup of resources after test execution
2.5 public static void main (string[] args) {}
Main function: Run-time execution of test cases according to the logic defined by the main function
Custom Java Sampler Code
Packagecom.SelfDefined.Components;Importjava.security.Policy.Parameters;Importorg.apache.jmeter.config.Arguments;//This class is used to define and access parameters.ImportOrg.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;//Custom Java Samplers need to inherit this classImportOrg.apache.jmeter.protocol.java.sampler.JavaSamplerContext;//context of the JMeterImportOrg.apache.jmeter.samplers.SampleResult;//return Results//inherit the Abstractjavasamplerclient class when you need to customize the Java sampler, inheriting other classes if you need to override the HTTP sampler Public classMycomponentsextendsabstractjavasamplerclient{PrivateString A; PrivateString B; PrivateString Resultdata; //This method is used to define the parameters of the Java method.//param.addargument ("Num1", ""); indicates that the entry parameter is named NUM1 and the default value is null//set available parameters and default values PublicArguments getdefaultparameters () {Arguments param=NewArguments (); Param.addargument ("Num1", "" "); Param.addargument ("Num2", "" "); returnparam; } //Each thread is executed once before testing, doing some initialization work Public voidsetuptest (Javasamplercontext arg0) {}//starting the test, the parameter values can be obtained from the arg0 parameter Publicsampleresult runtest (Javasamplercontext arg0) {a= Arg0.getparameter ("NUM1"); b= Arg0.getparameter ("num2"); Sampleresult SR=NewSampleresult (); Sr.setsamplelabel ("Java Request"); Try{Sr.samplestart (); Hello Test=NewHello (); Resultdata=string.valueof (Test.sum (Integer.parseint (a), Integer.parseint (b))); if(Resultdata! =NULL&& resultdata.length () > 0) {Sr.setresponsedata ("The result is:" + resultdata,NULL); Sr.setdatatype (Sampleresult.text); } sr.setsuccessful (true);//If the parameter is correct, the setting returns the result as true}Catch(Throwable e) {sr.setsuccessful (false);//If the parameter is wrong, the set returns the result as falseE.printstacktrace (); } finally{sr.sampleend ();//JMeter End statistic response time stamp } returnSR; } Public voidteardowntest (Javasamplercontext arg0) {} Public Static voidMain (string[] args) {Arguments params=NewArguments (); Params.addargument ("Num1", "1");//gets the value of the variable NUM1, if the default value of 1 is empty, I tried it.Params.addargument ("Num2", "2"); Javasamplercontext arg0=NewJavasamplercontext (params);//Create JMeter ContextMycomponents test =Newmycomponents (); Test.setuptest (arg0); Test.runtest (arg0); Test.teardowntest (arg0); } }
3. Export the jar package
The guide pack will have a warning, no need to control it
4. Restart JMeter to see our custom components in the Java sampler
5. Implementation results
Jmeter01: Custom Java Sampler