Creating and Running Apache JMeter Test Scripts from a Java Program

Creating and Running Apache JMeter Test Scripts from a Java Program

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Integrating Apache JMeter test scripts directly into your Java applications can streamline your development and testing workflows. This guide will walk you through the process of creating and running JMeter test scripts programmatically using Java.

Setting Up Your Environment

Before diving into the code, ensure you have the necessary JAR files from JMeter:

  • ApacheJMeter_core.jar
  • jorphan.jar
  • avalon-framework-4.1.4.jar
  • ApacheJMeter_http.jar
  • commons-logging-1.1.1.jar
  • logkit-2.0.jar
  • oro-2.0.8.jar
  • commons-io-2.2.jar
  • commons-lang3-3.1.jar

Creating the Test Plan Programmatically

Here is a simple example of how to create and run a JMeter test plan directly from Java:


import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.sampler.HTTPSampler;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.SetupThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

public class JMeterTestFromCode {

    public static void main(String[] args) {
        // Initialize JMeter Engine
        StandardJMeterEngine jmeter = new StandardJMeterEngine();

        // Load JMeter properties
        JMeterUtils.loadJMeterProperties("c:/tmp/jmeter.properties");

        // Initialize test plan components
        HashTree testPlanTree = new HashTree();

        // HTTP Sampler
        HTTPSampler httpSampler = new HTTPSampler();
        httpSampler.setDomain("www.google.com");
        httpSampler.setPort(80);
        httpSampler.setPath("/");
        httpSampler.setMethod("GET");

        // Loop Controller
        LoopController loopController = new LoopController();
        loopController.setLoops(1);
        loopController.addTestElement(httpSampler);
        loopController.setFirst(true);

        // Thread Group
        SetupThreadGroup threadGroup = new SetupThreadGroup();
        threadGroup.setNumThreads(1);
        threadGroup.setRampUp(1);
        threadGroup.setSamplerController(loopController);

        // Test Plan
        TestPlan testPlan = new TestPlan("My Test Plan");

        // Build Test Plan Tree
        testPlanTree.add("testPlan", testPlan);
        testPlanTree.add("loopController", loopController);
        testPlanTree.add("threadGroup", threadGroup);
        testPlanTree.add("httpSampler", httpSampler);

        // Configure and run JMeter
        jmeter.configure(testPlanTree);
        jmeter.run();
    }
}
  

Dependencies

The example above requires a set of dependencies. Ensure you have the following JAR files in your classpath:

  • ApacheJMeter_core.jar
  • jorphan.jar
  • ApacheJMeter_http.jar
  • commons-logging-1.1.1.jar
  • logkit-2.0.jar

Running the Test Plan

After setting up your environment and writing the Java code, you can run your test plan using the standard Java execution process. This approach is particularly useful for integrating performance tests directly into your CI/CD pipelines.

Enhancing Your Testing Process

While creating and running JMeter test scripts programmatically can be powerful, managing and maintaining these scripts can become cumbersome. This is where tools like Repeato come into play. Repeato is a no-code test automation tool for iOS and Android that allows you to create, run, and maintain automated tests for your apps quickly and efficiently.

Repeato offers a no-code interface and an intuitive test recorder, making it simple for users to get started with test automation. For more advanced testers, Repeato provides a scripting interface to automate complex use cases. Moreover, Repeato supports testing websites inside an Android emulator or device, with explicit web testing support coming soon.

For more information on configuring your test environment, visit our Getting Started page.

By leveraging tools like Repeato alongside JMeter, you can enhance your testing strategy, making it more robust and easier to manage.

Like this article? there’s more where that came from!