How to develop a plugin for Apache JMeter (ii)--the first JMeter plugin

Source: Internet
Author: User

Article content reproduced in: http://lib.csdn.net/article/softwaretest/25700, and add personal some

This article opens the journey for developing a plugin for jmeter, and we choose to use the function component as the starting object for plug-in development, and in the previous chapters we divide it into non-GUI components, and the reason to choose it is not only because the function plugin is minimalist in terms of development, And in the actual use of JMeter to perform tests, the application of function components will greatly facilitate your testing, and some are even necessary.

What is a function component?

It is still necessary to make a brief review of the functions of function components:

By opening the function assistant, we can use the drop-down menu to see JMeter as our default set of functional functions, using the function is very simple, such as "__uuid" function, the function is to generate a UUID, in the following input box only need to enter "${__uuid}" You can introduce the UUID value returned by invoking the function method, and all components can be introduced into the function component.

A sampler plug-in for testing

Here we can first develop a dedicated test function or a custom variable output sampler (originally it is the first plug-in we developed), we call it Testsampler, the development method is the separation method, Divided into logical control Section Testsampler and GUI part Testsamplergui:

1, first create Java project, File>new>project, choose not maven project,next> named Testsampler>next

2, first in the Java project introduced the main JMeter plug-in development jar package, how to introduce jar package can refer to IntelliJ Idea Java Project import jar package, hit jar package: http://www.cnblogs.com/yulia/p/6824058.html:

3. The Testsampler Class code reference is as follows:

Publicclass testsampler extends abstractsampler {public final static String function = " FUNCTION ";  @Override public sampleresult sample ( Entry Entry) {//TODO auto-generated Method stub Sampleresult res = new Sampleresult (); Res.samplestart (); System.out.println (this.getproperty (FUNCTION)); true); return Res;}} 
/span>

 

In the previous chapters, we briefly introduced the main implementation of sampler, by overriding the sample method, implementing a sampling method, where we output the value corresponding to the "function" property, the function method introduced by the output GUI interface to return the result.
4. The Testsamplergui Class code reference is as follows:

PublicClassTestsamplerguiExtendsabstractsamplergui{Private JTextField Functiontextfield =NullPublicTestsamplergui () {init ();}@OverridePublicvoidConfigure (testelement Element) {Super.configure (Element); Functiontextfield.settext (element.getpropertyasstring (testsampler.function)); }PrivatevoidInit () {JPanel Mainpanel =New JPanel (New GridBagLayout ()); Functiontextfield =New JTextField (20); Mainpanel.add (Functiontextfield); Add (Mainpanel); }@OverridePublic testelementCreatetestelement () {Create the corresponding samplerTODO auto-generated Method Stub Testelement Sampler =New Testsampler (); Modifytestelement (sampler);return sampler; }@OverridePublic StringGetlabelresource () {TODO auto-generated Method StubReturnThis.getclass (). Getsimplename (); }@OverridePublicvoidModifytestelement (Testelement Sampler) {TODO auto-generated Method StubSuper.configuretestelement (sampler);if (samplerinstanceof Testsampler) {Testsampler Testsmpler = (testsampler) sampler; Testsmpler.setproperty (Testsampler.function, Functiontextfield.gettext ()); }}  @Override public String  Getstaticlabel () {//set display name //TODO auto-generated Method stub Span class= "Hljs-keyword" >return  "Testsampler";} private void initFields () { Functiontextfield.settext ( @Override public void cleargui () {super.cleargui (); Initfields ();}} 
/span>

Our primary purpose is to assign the value entered in the Functiontextfield input box to the function property of the Testsampler object through the Modifytestelement method.

5, through the above simple code to complete the test sampler, the Java project packaged as a jar package, how to package can refer to IntelliJ Idea Java Project import jar package, hit jar package: http://www.cnblogs.com/yulia/p/ 6824058.html

6, the previous step to hit the package into the jmeter frame, that is, put Testsampler.jar into the JMeter package path, under the example D:\apache-jmeter-2.13\lib\ext

7. If JMeter is turned on during the previous step, restart JMeter, and then add directory or. jar to under the test plan Classpath the Testsampler.jar configuration, and you can see that the sampler appears in the Sampler list:


Add the sampler under the thread group and enter the function to be tested, run the test plan, and you can see the console output:

OK, consistent with our intentions, outputs the results returned after the __UUID function was run.
With the test sampler, we can develop function plug-ins that have their own functions.

function plug-in development case

Re-create the project, the project is named Factorial. You can now write a function that calculates the factorial, name it factorial, and the main code reference is as follows (packaged under *.jmeter.functions):

Import java.util.Collection;Import java.util.LinkedList;Import java.util.List;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 Org.apache.jmeter.util.JMeterUtils;Import Org.apache.jorphan.logging.LoggingManager;Import Org.apache.log.Logger;PublicClassFactorialExtendsabstractfunction {PrivateStaticFinal Logger log = Loggingmanager.getloggerforclass ();PrivateStaticFinal list<string> desc =New Linkedlist<string> ();PrivateStaticFinal String KEY ="__factorial";Private object[] Values =Nullstatic {Desc.add ("Factorial_value"); }Description parameters@OverridePublic list<string>Getargumentdesc () {TODO auto-generated Method Stubreturn desc; }function execution, return result@OverridePublic StringExecute (sampleresult previousresult, Sampler Currentsampler)Throws Invalidvariableexception {TODO auto-generated Method Stub String numberstring = ((compoundvariable) values[0]). Execute (). Trim ();int num;try{num = integer.valueof (numberstring);}catch (Exception e) {ReturnNull }Return string.valueof (factorial (num)); }Get function Reference keyword@OverridePublic StringGetreferencekey () {TODO auto-generated Method Stubreturn KEY; }Setting parameters@OverridePublicvoidSetparameters (collection<compoundvariable> parameters)Throws Invalidvariableexception {TODO auto-generated Method StubThe number of parameters can be checked, mainly including the following two methods Checkminparametercount (parameters,1); Checkparametercount (parameters, 1, 1); values = Parameters.toarray (); } private int factorial (int num) {int result = 1; if (num < 0) {return- 1;} if (num = 0) {result = 1;} else {for (int i = num; i > 0; i--) {result *= i;}} return result;}}            

By inheriting the Abstractfunction abstract class, overriding the Getargumentdesc method to implement the description of the function arguments, overriding the Setparameters method to check and set the parameters of the function, The override Getreferencekey method tells JMeter the function's reference name in the framework, overrides the Execute method, implements execution on the function, and returns the result. Through the above code we have finished writing the factorial function component.
Insert the plug-in package into the JMeter framework, which you can view in the function assistant as follows:

The framework has introduced the function components that we have developed, and we calculate the factorial of 10 based on the parameter settings as follows:

Tested with Testsampler, the test results are as follows:

The console outputs a 10 factorial return result. In this context, we have basically mastered the main development process of function plug-in components through this chapter.

How to develop a plugin for Apache JMeter (ii)--the first JMeter plugin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.