Apache JMeter: Load Test Whatever You Want

This is the second post about load testing with Apache JMeter, read the first post here: A step by step tutorial about load testing relational databases.
JMeter has lots of Samplers. If you need a sampler that is not provided by JMeter you can write your custom sampler. (custom samplers are called “Java Request” in JMeter terminology)

This post will show you, step by step, how to write a JMeter Java Request.

Step 1: Preparing the development environment

Add these two jar files to the java classpath.

  1. $JMETER_HOME/lib/ext/ApacheJMeter_core.jar
  2. $JMETER_HOME/lib/ext/ApacheJMeter_java.jar

(If you are using Eclipse, add these files as external jar files to the java build path.)

Step 2: Extending AbstractJavaSamplerClient

After setting up the classpath, create a custom sampler by extending AbstractJavaSamplerClient and override the following methods.

public Arguments getDefaultParameters() {...}
public void setupTest(JavaSamplerContext context) {...}
public void teardownTest(JavaSamplerContext context) {...}
public SampleResult runTest(JavaSamplerContext context) {...}

Implement getDefaultParameters if you want initial values for test paramters. JMeter will display the parameters in its Java Request configuration GUI. (See the contents of the red rectangle in the picture below.) Here’s an example implementation:

public Arguments getDefaultParameters() {
    Arguments defaultParameters = new Arguments();
    defaultParameters.addArgument("memcached_servers", "localhost:11211");
    defaultParameters.addArgument("username", "testuser");
    defaultParameters.addArgument("password", "testpasswd");
    return defaultParameters;

This is where you read test parameters and initialize your test client. JMeter calls this method only once for each test thread.

Clean up the mess.

Write your test logic in this method. JMeter will call runTest method for every execution of test threads. Here is a typical runTest implementation:

public SampleResult runTest(JavaSamplerContext context) {
    SampleResult result = new SampleResult();
    boolean success = true;
    // Write your test code here.
    return result;

The time elapsed betweed result.sampleStart() and result.sampleEnd() is used to calculate average response time of the application under test.

Step 3: Deploy your custom sampler

When you are done create a jar file (containing your custom sampler) in the $JMETER_HOME/lib/ext/ directory. JMeter will display your java request in the java request configuration page.

You can see the results of your test by adding listeners to your test plan. “A step by step tutorial about load testing relational databases” post shows how to add listeners to test plans.

Reference: Load Test Whatever You Want With Apache JMeter from our JCG partner Ilkin Ulas at the All your base are belong to us blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

5 Responses to "Apache JMeter: Load Test Whatever You Want"

  1. Nikhil Ninawe says:

    perfect article..done my job..

  2. Imen CHOk says:

    Great , ii’s been very useful article and it solve my problem thanks a lot :D

  3. Dmitri T says:

    Thanks for great post. There is a way to execute custom Java code right from JMeter test using Beanshell Sampler. It won’t require custom coding, packaging, deploying, etc. however full access to Java and JMeter API will be provided. See How to use BeanShell: JMeter’s favorite built-in component guide for more details

  4. Tejas H says:

    Thanks for a great post. When you define parameters in getDefaultParameters method how do you get the parameter’s value in runTest? How do I get the username and password passed in my code?

  5. Tuna says:

    Thanks for this post. How can I generate result file (jtl) from java code?

Leave a Reply

7 − = three

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books