"Go" how to develop plug-ins for Apache JMeter (i)

Source: Internet
Author: User

This article is reproduced in Http://blog.csdn.net/column/details/12925.html,xreztento

The author of the very essence, I intend to operate in this series once again, add more points for more people to get started faster plug-in development

Why choose to use JMeter

When asked this question, perhaps you will have a lot of reasons in mind, such as:

    • There is no copyright issue with the open source project under the Apache Foundation;
    • A handful of open-source performance automation testing tools that are constantly being updated;
    • Rich support Protocol, is the best substitute for commercial testing tools;
    • There are special plug-in projects to support, so that you have more choices in practice, such as http://jmeter-plugins.org/provides a lot of good plug-ins for you in the use of JMeter to perform testing, you can choose more components to develop test plans, complete the test process, Monitor test data.

And my answer is:

The key is not simply to understand JMeter as a purely performance testing tool, but to realize that it is still an excellent framework, which is even the most fundamental reason for me to choose it, where all the components can be freely written plug-in way to add and perfect, For a test engineer, writing plug-in components for JMeter is fun!

JMeter basic component types and implementation methods

For the basic components of jmeter, we can simply divide them into two main categories:

    • A class is a GUI-capable component that can be added in the test plan tree through the JMeter Graphics management controller, mainly including Threadgroup (thread group), config (configuration component), timer (timer), Modifier (front processor), Extractor (post processor), controller (logic Controller), Sampler (test sample), assertion (assertion), and listener (listener);
    • The other is non-GUI components, which typically represent function (functions) and some sub-test sampling, such as javasamplerclient.

There are two common implementations of components:

    1. GUI and logical control separation: GUI part by inheriting various component GUI abstract class, logic control part through inheriting component logic abstract class and implement various interface way to realize internal logic control of different components;
    2. GUI and logical control are not separated: the difference between the separation method is not to implement the GUI part separately, in the logic control part realizes the configuration of GUI interface by implementing the Testbean interface method.
JMeter plug-in component Implementation Details overview

Testelement is the most basic unit of all components, and the component class is a subclass of the Testelement class, JMeter defines the several component models described in the previous section and regulates the roles that each of them needs to play.

(1) Realization of GUI part
The implementation of the GUI section can be found in the jmeter implementation of the main class Org.apache.jmeter.JMeter, which actually implements the Geticonmappings () method in the Jmeterplugin interface to map the GUI icon for the component. A two-dimensional array is defined for the mapping relationship, with the following code:

PrivateStaticFinal string[][] Default_icons = {{"Org.apache.jmeter.control.gui.TestPlanGui","Org/apache/jmeter/images/beaker.gif"}, {"Org.apache.jmeter.timers.gui.AbstractTimerGui","Org/apache/jmeter/images/timer.gif"}, {"Org.apache.jmeter.threads.gui.ThreadGroupGui","Org/apache/jmeter/images/thread.gif"}, {"Org.apache.jmeter.visualizers.gui.AbstractListenerGui","Org/apache/jmeter/images/meter.png"}, {"Org.apache.jmeter.config.gui.AbstractConfigGui","Org/apache/jmeter/images/testtubes.png"}, { "Org.apache.jmeter.processor.gui.AbstractPreProcessorGui",  " Org/apache/jmeter/images/leafnode.gif "}, {" Org.apache.jmeter.processor.gui.AbstractPostProcessorGui ", " org/apache/jmeter/images/ Leafnodeflip.gif "}, {" Org.apache.jmeter.control.gui.AbstractControllerGui ",  "Org/apache/jmeter/images/knob.gif"}, {" Org/apache/jmeter/images/clipboard.gif " }, {" Org.apache.jmeter.assertions.gui.AbstractAssertionGui ", " org/apache/jmeter/images/ Question.gif "}};            

You can see that as long as the GUI part of the plug-in class inherits the GUI class from the above array, the JMeter framework automatically maps it to the corresponding component type and icon.

(2) The implementation details of the logic Control section are described in terms of component categories:

Threadgroup (thread Group) components

The Threadgroup (thread Group) component inherits Abstractthreadgroup abstract classes by overriding various control methods, such as void Schedulethread (Jmeterthread thread), Stopthread ( String ThreadName, Boolean Now), threadfinished (Jmeterthread thread), to control and coordinate the behavior of each thread (virtual user), the thread group is the most basic component of building a performance test model.

Config component (configuration component)

Config (configuration component) component is special relative to other components, the ontology task can be accomplished by inheriting the Configtestelement class or only the implementation of the GUI part. For a component class that needs to be configured, you need to implement the public Boolean applies (Configtestelement Configelement) method of the Configmergabilityindicator interface, to indicate which config components can be used to configure them, the source code for reference Tcpsampler is as follows:

private  Static final set<string> appliable_config_classes = new Hashset<string> (arrays.aslist (new string[]{ "Org.ap Ache.jmeter.config.gui.LoginConfigGui ", " Org.apache.jmeter.protocol.tcp.config.gui.TCPConfigGui ", " Org.apache.jmeter.config.gui.SimpleConfigGui "}));  @Override public boolean Span class= "Hljs-title" >applies (configtestelement configelement) {String Guiclass = Configelement.getproperty ( Testelement.gui_class). GetStringValue (); return appliable_config_classes.contains (Guiclass);}       

The code above indicates that the three configuration components Loginconfiggui, Simpleconfiggui, and Tcpconfiggui can be configured for the Tcpsampler component.

Timer (timer) components

The timer (timer) component realizes the control of time by inheriting the Abstracttestelement abstract class and implementing the delay () method of the timer interface, the main control contents are as follows:

    • When the control line Chengyan, it is used to imitate think time (thinktime) or keyboard time (KeyTime);
    • Controlling threading behavior, such as Synctimer (synchronous timers), is the internal use of Cyclicbarrier to control the logical behavior of blocking and releasing all running threads, thus achieving the purpose of the "meeting point".

Modifier (predecessor processor) components

The Modifier (predecessor processor) component implements the process () method control logic of the preprocessor interface by inheriting the Abstracttestelement abstract class. It is often necessary to identify and judge the current sampler and the previous Sampleresult in the thread context to make the correct processing, The general behavior is to decorate some of its properties by removing certain values of sampleresult or directly before starting the sample method in the current sampler.

Extractor (post processor) components

The Extractor (post processor) component implements the process () method control logic for the Postprocessor interface by inheriting the Abstracttestelement abstract class. It is often necessary to identify and judge the previous Sampleresult in the thread context to make the correct processing.

Controller (Controllers) component

The controller component inherits the Genericcontroller class by overriding sampler next (), void Setdone (Boolean done), int getitercount (), void Reinitialize () and other methods to control the test behavior of sampler.

Sampler (test sample) assembly

The Sampler (test sample) component inherits the Abstractsampler abstract class and implements the test process and the acquisition of test results by overriding the Sampleresult sample (Entry e) method.

Assertion (assertion) component

The assertion (assertion) component implements the assertion method by inheriting the Abstracttestelement abstract class and implementing the GetResult (Sampleresult result) method of the assertion interface to determine the result content. Used to assert the content of sampled acquisition results generated by the sampler component.

There are two main scenarios for Listener (listener):

    • Direct inheritance of abstracttestelement to implement interface methods such as Samplelistener or visualizer
    • Implement interface methods such as Resultcollector and runnable

We can divide it into two major types of report (reports) and vizualizers (monitors) from a practical purpose.
The report inherits the Abstractlistenerelement abstract class and, by implementing the Sampleoccurred (SampleEvent e) method, processes the sampleresult generated in all acquisition events to generate a report;
Vizualizers (monitor) is mainly used for specific monitoring tasks, such as monitoring the system resource utilization of components, and the report is the difference between vizualizers must inherit a Resultcollector class, The custom data collection is done in the collector by opening the additional threading method.

It also introduces non-GUI components such as function (functions), which are simple to implement, and have a single function, and only need to inherit the corresponding abstract class.

(3) The main interface description that some testelement need to implement

In order to achieve more features, the components need to implement some of the main interfaces and methods when necessary, as illustrated below:

Nothreadclone Interface:
This is a cloned per thread, so the is shared, it can be understood that once the Nothreadclone interface is implemented, this testelement will not be created in each thread under the Threads group. Instead of a global component, the GetThreadContext () method cannot be used. Conversely, we can find that JMeter actually creates a testelement,a new instance is created in the context of each thread under the thread group through the Testelement Clone () method. The thread group, and the Clone () method are then called to create copies for each of the thread in a thread group. If you want to synchronize certain shared resources, you need to Refer to the following methods:

private transient Object lock = new Object();//锁对象不需要进行序列化,因为分布式在不同主机内存中的锁对象不必保持一致@Overridepublic Object clone() { Clazz clazz = (Clazz) super.clone(); clazz.lock = lock; //保证所有克隆对象共享同一个锁对象 return clazz;}

Note: This is an explicit shared object method, since Java is a "shallow clone" by default when cloning a class object, so the lock object is shared by default without explicitly sharing the lock object.

Loopiterationlistener Interface:
The logic that is required to implement each iteration event is controlled by implementing the Iterationstart (Loopiterationevent event) method.

Serializable interface:
In order to maintain the consistency of some configuration and objects in the distributed test model, it is necessary to implement the serializable interface to maintain the consistency of different host object properties through serialization.

Teststatelistener Interface:
The logic required to implement the test state change event is controlled by implementing the Teststarted (String string) and Testended (String string) methods.

In addition, the actual plug-in writing process will include such as Remoteable, Interruptible, Threadlistener and other interface applications, will be detailed in the following specific chapters and applications.

Component is a plug-in, just one step into the JMeter frame

Inserting a well-written plug-in component into the JMETER framework is very simple, just package the component as a jar package and copy it to the $jmeter_home/lib/ext path for use!

"Go" how to develop plug-ins for Apache JMeter (i)

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.