Run massively scalable performance tests on web, mobile, and APIs

Request a Demo
May. 8th, 2019

How to write your own JMeter listener. A guide

What is a Listener in JMeter?

 

Listeners enable developers and performance testers to monitor JMeter requests and analyze test results. Listeners aggregate data, process and manipulate the information in it and even enable customization.

 

There are a lot of useful listeners for JMeter, which can be found by right clicking the test plan Add->Listeners. For a list of different types of JMeter listeners, you can refer to this, this and this blog.

 

With listeners, you can visualize almost any data gathered by your samplers. JMeter also provides unlimited possibilities to tune and tweak any component you want, so performance testers can customize the output of the tests. So, let’s see how we can write our own listener to visualize the data collected in any way we want. Let’s get started.

 

Basics of writing plugins for JMeter

 

Before we start, we should look at the basics of writing your plug-ins for JMeter. First of all, keep in mind the structure of JMeter sources. If you feel lost, you can always refer to the implementation of the existing components here:

 

  • components – contains non-protocol-specific components like visualizers, assertions, etc.;
  • core – the core code of JMeter including all core interfaces and abstract classes;
  • examples – example sampler demonstrating how to use the new bean framework;
  • functions – standard functions used by all components;
  • jorphan – utility classes providing common utility functions;
  • protocol – contains various protocols JMeter supports.

 

Also, it’s very important to follow the established hierarchy of JMeter classes. If you want to write your own component, you should inherit it from standard JMeter abstract classes. Abstract classes already implement basic JMeter integration, so if your components extend one of them, you only need to write the code for differences between your implementation and the standard one. Here are some abstract classes for component GUI:

 

AbstractSamplerGui - an abstract GUI class for a Sampler;
AbstractAssertionGui - an abstract GUI class for an Assertion;
AbstractConfigGui - an abstract GUI class for a Configuration Element;
AbstractControllerGui - an abstract GUI class for a Logic Controller;
AbstractPostProcessorGui - an abstract GUI class for a PostProcessor;
AbstractPreProcessorGui - an abstract GUI class for a PreProcessor;
AbstractVisualizer - an abstract GUI class for a Visualizer or Listener;
AbstractTimerGui - an abstract GUI class for a Timer.

 

When you make your own GUI class, it’s important to keep in mind the basic methods you need to implement, to make a unique component:

 

createTestElement() - in this method you should instantiate a class that implements actual logic of your component. Example:
@Override
public TestElement createTestElement() {
   TestElement te = new MySampler();
   modifyTestElement(te);
   return te;
}

modifyTestElement(TestElement te) - this method is used to pass the data from the GUI class to the logic class. Example:

@Override
public void modifyTestElement(TestElement te) {
   if (te instanceof MySampler) {
       te.setFilename(getFile());
       sampler = te;
   }
    configureTestElement(te)
}

configure(TestElement te) - here you configure your GUI according to the saved data from your test element. Example:

public void configure(TestElement te) {
   super.configure(te);

    nameField.setText(te.getPropertyAsText(MySampler.NAME));
}

 

And here are abstract classes for component logic:

 

AbstractSampler - abstract class for a Sampler logic;
Assertion - interface class for an Assertion logic.
ConfigElement - interface class for a Configuration Element logic.
GenericController - basic class for a Logic Controllerlogic.
PostProcessor - interface class for a PostProcessor logic.
PreProcessor - interface class for a PreProcessor logic.
AbstractListenerElement - abstract class for a Listener logic.
Timer - interface class for a Timer logic.

 

Creating the GUI for your JMeter listener

 

A GUI for JMeter components is made on Javax Swing. It allows you to construct your GUI with ease. All of the basic GUI classes are inherited from the Java Container class. This allows you to add new GUI elements simply by invoking add method, that is also inherited from the Container class. For example, if you want to add a text field to your GUI component you can do the following in your GUI class:

 

JTextField myTextField = new JTextField();
add(myTextField, BorderLayout.NORTH);

 

Remember, basic GUI elements for the class have already been added by the basic implementation you inherit your class from. So, for example, there is no need to add standard Name and Comment fields to your script - you only need to add specific elements.

 

Collecting data

 

A data to display is collected in a form of SampleResult JMeter class objects. To add your own logic of processing results, you can override add method. For example:

 

@Override
public void add(final SampleResult res) {
   newSamples.add(res);
   updateGui(res);
}

 

In the method above you can modify the data collected in any way you want. Here you also call updateGui method to draw a table or a graph for your visualizer.

 

Visualizing your data

 

There are a number of ways you can visualize your data - it can be a table, tree or graph. Each of these representations uses updateGui method that is usually called when your listener receives a sample result.

 

For example, if you want to use JMeter graph to display the results of your test, you should define a variable for a Graph object and a model object in your listener script. A model object manages a data that will be visualized:

 

private final Graph graph;
private final CachingStatCalculator model;

public GraphVisualizer() {
   model = new CachingStatCalculator("Graph");
   graph = new Graph(model);
   init();
}

public void updateGui(final SampleResult res){
   graph.updateGui(res);
}

 

Here, the model is an object of the CachingStatCalculator class, that processes collected data in a way we want our output. If you want to change the output format, you should extend the SamplingStatCalculator class and implement your own logic.

 

There is basically how to create and  implement your own JMeter listener. 

 

You can run your own script in BlazeMeter for testing in the cloud. Just upload your url here to get started:

 

 

 

 

     
arrow Please enter a valid URL

Interested in writing for our Blog? Send us a pitch!