DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • A General Overview of TCPCopy Architecture
  • Monolithic Decomposition and Implementing Microservices Architecture
  • Implementing DDoS With MuleSoft: Demonstration With Apache JMeter
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases

Trending

  • Understanding the Shift: Why Companies Are Migrating From MongoDB to Aerospike Database?
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  • The Role of AI in Identity and Access Management for Organizations
  • Advancing Robot Vision and Control
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Implement Custom JMeter Samplers

Implement Custom JMeter Samplers

Stress testing tools are much more necessary in today's environment of complex architecture. Apache JMeter is one of these awesome tools, offering great load-testing capabilities. Here's an example of implementing custom JMeter samplers!

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Feb. 09, 16 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
25.1K Views

Join the DZone community and get the full member experience.

Join For Free

As we proceed on different architectures and implementations, the need for versatile stress testing tools rises.

Apache JMeter is one the most well-known tools when it comes to load testing. It supports many protocols such as FTP HTTP TCP and also it can be used easily for distributed testing.

JMeter also provides you with an easy way to create custom samplers. For example, if you need to load test a HTTP endpoint that requires a specific procedure for signing the headers then a custom sampler will come in handy.

The goal is to implement a custom sampler project which will load test a simple function.

I use Gradle for this example.


group 'com.gkatzioura.jmeter'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.6

repositories {
    mavenCentral()
}


dependencies {
    compile 'org.apache.jmeter:ApacheJMeter_java:2.11'
    compile 'org.json:json:20151123'
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

task copySample(type:Copy,dependsOn:[build]) {

    copy {
        from project.buildDir.getPath()+'/libs/jmeter-sampler-1.0-SNAPSHOT.jar'
        into 'pathtoyourjmeterinstallation/apache-jmeter-2.13/lib/ext/'
    }
}

I include the ApacheJMeter dependency on the project since the sampler will have to extend the AbstractJavaSamplerClient.
The copySample task will copy the jar to the lib/ext path of Jmeter where all samplers reside.

A simple function will be called by the sampler


package com.gkatzioura.jmeter;

/**
 * Created by gkatzioura on 30/1/2016.
 */
public class FunctionalityForSampling {

    public String testFunction(String arguement1,String arguement2) throws Exception {

        if (arguement1.equals(arguement2)) {
            throw new Exception();
        }

        else return arguement1+arguement2;
    }

}

The CustomSampler class extends the AbstractJavaSamplerClient class and invokes the testFunction.
By overriding the getDefaultParameters function, we can apply default parameters that can be used with the request.


package com.gkatzioura.jmeter;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * Created by gkatzioura on 30/1/2016.
 */
public class CustomSampler extends AbstractJavaSamplerClient implements Serializable {

    private static final String METHOD_TAG = "method";
    private static final String ARG1_TAG = "arg1";
    private static final String ARG2_TAG = "arg2";

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomSampler.class);

    @Override
    public Arguments getDefaultParameters() {

        Arguments defaultParameters = new Arguments();
        defaultParameters.addArgument(METHOD_TAG,"test");
        defaultParameters.addArgument(ARG1_TAG,"arg1");
        defaultParameters.addArgument(ARG2_TAG,"arg2");

        return defaultParameters;
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {

        String method = javaSamplerContext.getParameter(METHOD_TAG);
        String arg1 = javaSamplerContext.getParameter(ARG1_TAG);
        String arg2 = javaSamplerContext.getParameter(ARG2_TAG);

        FunctionalityForSampling functionalityForSampling = new FunctionalityForSampling();

        SampleResult sampleResult = new SampleResult();
        sampleResult.sampleStart();

        try {
            String message = functionalityForSampling.testFunction(arg1,arg2);
            sampleResult.sampleEnd();;
            sampleResult.setSuccessful(Boolean.TRUE);
            sampleResult.setResponseCodeOK();
            sampleResult.setResponseMessage(message);
        } catch (Exception e) {
            LOGGER.error("Request was not successfully processed",e);
            sampleResult.sampleEnd();
            sampleResult.setResponseMessage(e.getMessage());
            sampleResult.setSuccessful(Boolean.FALSE);

        }

        return sampleResult;
    }

}

After compile is finished the jar created must be copied to the lib/ext directory of the JMeter installation home.
Also, in case there are more dependencies that have to be imported they should also be copied to the lib path of the JMeter installation home

Once the process is complete by adding Java Sampler to a JMeter Thread Group we can choose our custom sampler.

Screenshot from 2016-01-31 01:30:06

You can also find the source code here.

Stress testing Apache JMeter Architecture Testing Dependency JAR (file format) Transmission Control Protocol Requests Directory

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A General Overview of TCPCopy Architecture
  • Monolithic Decomposition and Implementing Microservices Architecture
  • Implementing DDoS With MuleSoft: Demonstration With Apache JMeter
  • Motivations for Creating Filter and Merge Plugins for Apache JMeter With Use Cases

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!