What’s up Guys! Welcome to automationcalling.com

022_Framework_pix

In this article, we are going to take a look at how to run JMeter test using Cucumber JVM. I believe and sure this would be an interesting topic to see how we can set up, configure, develop a framework to run a performance test using code base but this is an experiment and tries from my side for considering in mind regarding better collaboration among team members, shift left approach, early performance test script development, etc.,

Note: Before going through further, I recommend you guys to go through or refer JMeter beginners tutorial or basics as I’m not going to explain step by step for each sampler, assertion, Timer, Logic Controller, Config Element, or any JMeter components.

For this topic, we are going to take a look at the following areas. The idea is, picking an HTTP sampler request for REST API test, and how we can use it by transforming in different variety of execution starting from JMeter Tool to Cucumber JVM execution using IDE.

  • JMeter Pre-requisites & Setup
  • How to run an HTTP sampler performance test using JMeter Tool
  • How to run an HTTP sampler performance test using NON-GUI Mode
  • How to run an HTTP sampler performance test using Maven
  • How to run an HTTP sampler performance test using JMeter API (Java Code)
  • How to run an HTTP sampler performance test using Cucumber JVM
    • Run an HTTP sampler Performance test using JMX file by Cucumber JVM
    • Run an HTTP sampler Performance test using JMeter API by Cucumber JVM

JMeter Pre-requisites & Setup

  1. Download JMeter from here
  2. 001_JMeter_Image
  3. Click on Zip folder for Windows installer
  4. Copy the zip folder in a specific folder location and unzip it.
  5. Set Environment variable for JMeter Downloaded location and Add Path variable as mentioned below snapshots002_JMeter_Home003_JMeterPath
  6. Open command prompt and supply command “jmeter –version” to verify current version gets installed.004_JMeter_version

How to run an HTTP sampler performance test using JMeter Tool

  1. Open “jmeter.bat” under bin folder where you extracted JMeter Zip.
  2. Right-click on Test Plan -> Add->Thread (Users)->Thread Group
  3. Right click on Thread Group->Sampler->HTTP Request
  4. Add https in protocol (In case https otherwise leave default)
  5. Enter Host or ip address in Server Name or IP field
  6. Select Request type, add Rest of resource path in Path field like below snapshot
  7. 005_HTTP_Sampler
  8. Right click on “HTTP Request”->Add->Listeners->View Results Tree
  9. Right click on “HTTP Request”->Add->Listeners->Summary Report
  10. Save Test Plan (The extension of JMeter file is JMX
  11. Set Through group value as mentioned in the below snapshot
    • For each second 2 users hit the endpoint in parallel that complete executing 10 users in 5 secs which repeat 2 times, so overall 20 users hit this endpoint in 10 seconds.007_Thread_Group
  12. Click on Run 006_Run_Button button
  13. After a run, we can see the following results in JMeter008_view_result_tree009_Summary_Report

With these steps, we ran and understood how to do simple REST API test using HTTP Sampler in JMeter.

How to run an HTTP sampler performance test using NON-GUI Mode

For this test, I’m going to save as the same JMX file which we used in the previous section. In this section, I will show you how to run the same test in NON-GUI Mode but for this test, I will disable all listeners (View Result Tree and Summary Report) and generate remote using NON-GUI Mode.

  1. Go to JMX File stored location in command prompt.010_Generate_Report
  2. Supply the following command
    • jmeter -n -t HTTP_Non_GUI_Mode.jmx -l D:\JMeter\JMeter_Examples\test_result\jtlresult.jtl -e -o D:\JMeter\JMeter_Examples\test_result\report
      • -n  : This specifies JMeter is to run in non-gui mode
      • -t   : name of JMX file that contains the Test Plan
      • -l   : name of JTL file to log sample results to
      • -e: generate report dashboard after load test
      • -o: output folder for report dashboard
  3. After running the test, go to a specific folder where reports get generated successfully.
  4. 011_Report_JTL
  5. 012_JTL_Results
  6. 013_generated_report
  7. 014_report_dashboard

How to Run JMeter Test in NON-GUI Mode by assigning  Runtime value

In this example, I use the same JMX file in the previous section by saving as a test plan, modify the following parameters and save it.

015_Runtimevalue

__P is for property value which is allowed during runtime for eg., command line execution.

Supply the following command by assigning runtime value for the variables

jmeter -n -t HTTP_Non_GUI_Mode_Runtime.jmx -Jusers=10 -Jrampup=5 -Jloop=2 -l D:\JMeter\JMeter_Examples\test_result\results.jtl

016_Runtime_Threadgroup

-J: [prop_name]=[value]defines a local JMeter property.

How to run an HTTP sampler performance test using Maven

Pre-requisites:

  • Please make sure to download Maven standalone zip file, add environment variable and path.

JMeter can be integrated with Apache Maven via the JMeter Maven Plugin.  In this section, I’m going to explain how to run existing JMX file using Maven Integration.

In order to understand by Maven plugin, the Maven project structure must be created in the following format.

017_JMX_Maven_format

the jmeter folder must exist under test folder, all JMX files can be copied under jmeter folder or its subfolder.

To demonstrate further, I’m going to create Maven project in Intellij and add the following details in pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>com.lazerycode.jmeter</groupId>
            <artifactId>jmeter-maven-plugin</artifactId>
            <version>2.5.1</version>
            <executions>
                <execution>
                    <id>jmeter-tests</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jmeter</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jMeterProcessJVMSettings>
                    <xms>1024</xms>
                    <xmx>1024</xmx>
                    <arguments>
                        <argument>-Xprof</argument>
                        <argument>-Xfuture</argument>
                    </arguments>
                </jMeterProcessJVMSettings>
                <propertiesUser>
                    <users>${users}</users>
                    <rampup>${rampup}</rampup>
                    <loop>${loop}</loop>
                </propertiesUser>
                <testFilesIncluded>
                    <jMeterTestFile>${jMeterTestFile}</jMeterTestFile>
                </testFilesIncluded>
                <testResultsTimestamp>false</testResultsTimestamp>
            </configuration>
        </plugin>
        <plugin>
            <groupId>de.codecentric</groupId>
            <artifactId>jmeter-graph-maven-plugin</artifactId>
            <version>0.1.0</version>
            <configuration>
                <inputFile>${project.build.directory}/jmeter/results/jmetereg_HTTP_Non_GUI_Mode_Runtime.jtl</inputFile>
                <graphs>
                    <graph>
                        <pluginType>ResponseTimesOverTime</pluginType>
                        <width>800</width>
                        <height>600</height>
                        <outputFile>${project.build.directory}/jmeter/results/jmetereg_HTTP_Non_GUI_Mode_Runtime.png</outputFile>
                    </graph>
                </graphs>
            </configuration>
        </plugin>
    </plugins>
</build>

In our previous example, we kept the following parameters as a runtime variable. Hence I use the same format to show you how to run this in maven command

015_Runtimevalue

mvn clean verify -Dusers=10 -Drampup=5 -Dloop=2 -DjMeterTestFile=jmetereg/HTTP_Non_GUI_Mode_Runtime.jmx

Performance test gets succeeded and you can see the results under the following location

  • [Maven_Project]/target/jmeter/results

After a successful result, supply the following command to generate a graph

mvn jmeter-graph:create-graph

The JPG file gets generated in the same results folder

  • [Maven_Project]/target/jmeter/results

018_Generated_Report

How to run an HTTP sampler performance test using JMeter Java API

This section is mainly focused on how to run a performance test using pure java code and execute existing JMX file using Java code.

I’m going to explain how to create a JMeter test using Java code in an easy way by comparing the JMX File. In below example, I opened one of the existing JMX files which were used in the previous section in this blog.

019_JMX_Configuration_Analysis

Take a close look at the above snapshot taken from JMX file. <hashTree>  is associated with Test Plan and Test Plan elements. So, the same logic used to implement in Java API.

To run JMeter using Java API, here are the following or minimum test plan elements/controller required.

  1. StandardJMeterEngine – This the main class which actually helps you to configure your test plan and execute.
  2. HashTree – Holds Test Plan element for eg., Test Plan, Thread Group, Http Sampler, Loop Controller

Test Plan Elements

  •  TestPlan – This must be mandatory which exists top in JMeter, in fact by default Test Plan exists by default if you open in JMeter

Example Code:

public static void initializeTestPlan(String testPlanName) {
    testPlan = new TestPlan(testPlanName);
    testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
    testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
    testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
}

LoopController – Since you must have at least one loop, it’s essential to have a Loop Controller instance set as the main Sampler controller for a Thread Group.   Example Code:

public static void setLoopController(int loopCount) {
    loopController = new LoopController();
    loopController.setLoops(loopCount);
    loopController.setFirst(true);
    loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
    loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
    loopController.initialize();
    LOG.info("Initalized Loop Controller");
}
  • ThreadGroup – At least one thread group must be present in Test Plan which needs to be configured no of  users, Ramp-up period and loop count.

Example Code:

public static void setThreadGroup(int noOfThreads, int setRamupNo) {
    threadGroup = new ThreadGroup();
    threadGroup.setName("Thread Group");
    threadGroup.setNumThreads(noOfThreads);
    threadGroup.setRampUp(setRamupNo);
    threadGroup.setSamplerController(loopController);
    LOG.info("Initialized Thread Group");
    threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
    threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
}
  • Sampler do the actual work for eg., http sampler, ftp request, jdbc request

Example Code:

public static void setHttpSampler(String protocolType, String setDomainName, int setPort, String setPath, String requestType) {
    httpSamplerProxy = new HTTPSamplerProxy();
    httpSamplerProxy.setDomain(setDomainName);
    httpSamplerProxy.setPort(setPort);
    httpSamplerProxy.setPath(setPath);
    httpSamplerProxy.setMethod(requestType);
    httpSamplerProxy.setProtocol(protocolType);
    httpSamplerProxy.setName("Http Sampler");
    LOG.info("Initalized Http Sampler");
    httpSamplerProxy.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
    httpSamplerProxy.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
}

To configure Test Plan and Test Plan Element in HashTree:

Example Code:

public static HashTree configureTestPlan() {
    testplanTree.add("TestPlan", testPlan);
    testplanTree.add("loopController", loopController);
    testplanTree.add("ThreadGroup", threadGroup);
    testplanTree.add("HTTPSamplerProxy", httpSamplerProxy);
    return testplanTree;
}

After all initialization and configuration, it’s time to run JMeter Performance test using StandardJMeterEngine. The below lines are important code to run a performance test in Java

static {
    jMeterEngine = new StandardJMeterEngine();
}
jMeterEngine.configure(testplanTree); 
jMeterEngine.run();

So, to look on final code, please go visit bitbucket repository (JMeterPerformanceFramework ) where JMeterUtil package under src/main/java.

How to run an HTTP sampler performance test using Cucumber JVM

In this section, I’m going to build a simple cucumber framework that helps you to configure all runtime properties in the feature file and run more robust as well as more flexible performance test.

020_using cucumberJVM

021_Feature_File

First scenario:

  • It’s pure Java code (JMeter Util implemented in Step Definition).
  • Configuring runtime properties like Threads, Rampuptime and Loop Controller
  • Execute test and generate a report in the target folder.

Second Scenario:

  • The second scenario, I use the same JMeterUtil code but the only difference is, not configured all test plan element in scratch using Java, instead, I use pre-configured JMX file.
  • Configuring runtime properties like Threads, Rampuptime and Loop Controller – Used XML util to update the value in JMX file.
  • Execute test and generate a report in the target folder.

To play with Cucumber Performance Test Framework, please visit my GitHub page:JMeterPerformanceFramework

Thanks for your time. Please subscribe automationcalling.com for more updates!

2 Comments

  1. when am working on performance-test-framework-cucumberjmetertestng, we are getting “SEVERE: Class name “org.apache.jmeter.report.dashboard.JsonExporter” is not valid.”
    could you please help on the same.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s