Why do you need to use custom jars in JMeter?

In some cases, we need to share or pass variables between threads and thread groups in JMeter. There are a few ways to do this and using custom jars in Jmeter is one of them. At JMeter documentation:

using custom jars in JMeter

But sometimes we want to ensure synchronization between threads and thread groups. In this case, one of the best ways is to “write your own Java classes.”

Creating a Custom Jar

  • Create a new java class in your favorite IDE. Our java package structure is as follows:

loadium_load_testing

 

Create the methods that you need in your test. In this example, we created methods about BlockingQueue operations.
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class Loadium {
private static LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<>();

public static void put(Object obj) throws InterruptedException {
queue.put(obj);
}

public static Object take() throws Exception {
return queue.take();
}

public static Object get(long timeout) throws Exception {
return queue.poll(timeout, TimeUnit.MILLISECONDS);
}

public static void clear() throws Exception {
queue.clear();
}

public static int size() throws Exception {
return queue.size();
}

}

  • Create a JAR file by following this instruction “Click Build->Build Artifacts.” Then a jar will be created in your workspace.

loadium_jar

  • Copy and paste the jar file in <your JMeter path>/lib folder.
  • Restart JMeter to load this JAR file.

Loadium compatible with Jmeter

Using the custom jar in your JMeter script

In this section, we will learn how to use the custom class in JMeter script.

  • Create a Test Plan in your JMeter GUI.
  • Add a Thread Group element.
  • Add a BeanShell Sampler to your test plan.

Now we can call our custom method as follows in BeanShell Sampler element.

beanshell_sampler

Run your test plan, and the result is below.

using custom jars in JMeter

 

How To Use Custom Jars On Loadium?

  • Upload the JMX file.
  • Add your JAR file.
  • Set your JMeter configuration as thread count, engine count, etc.
  • Save and run your test on Loadium.
using custom jars in JMeter

using custom jars in JMeter

For reporting purposes, go to Logs tab in your test report.

using custom jars in JMeter

 

The result is below:

using custom jars in JMeter

Please feel free to ask questions if you have any.

Happ load testing!

Postman to Jmeter Convertor