top of page

Jmeter JSR223 Groovy Examples - A Complete Guide

Apache JMeter is a leading open-source tool widely used for load testing and performance measurement. However, as applications become more complex, the need for flexible and dynamic scripting arises. This is where Jmeter script and especially JMeter's JSR223 post-processor, coupled with the powerful Groovy language, comes to the rescue.

In this comprehensive guide, we dive deep into the world of JMeter JSR223 Groovy Examples to address real-world performance testing challenges. From handling dynamic data to simulating user behaviors and customizing test scenarios, we present an extensive collection of practical examples that showcase the true potential of JMeter's JSR223 Groovy scripting capabilities.

Join us on this journey to become an expert on the subject of JMeter JSR223 Groovy scripts and upgrade your Jmeter load testing proficiency. Explore our performance testing page to engage an expert consultant for your performance testing needs.

Table of Contents

What is post-processor in Jmeter?

In JMeter, a Post-Processor is an element used to process the responses generated by samplers during the execution of a test plan. Post-Processors allow you to extract, modify, or perform additional actions on the response data returned by the samplers before passing it to subsequent elements in the test plan.

What are different post-processors in Jmeter?

  • Regular Expression Extractor: Extracts data from the response using regular expressions and saves it into JMeter variables for later use.

  • XPath Extractor: Extracts data from XML responses using XPath expressions and saves it into JMeter variables.

  • JSON Extractor: Extracts data from JSON responses using JSONPath expressions and saves it into JMeter variables.

  • Boundary Extractor: Extracts data based on left and right boundary strings from the response and saves it into JMeter variables.

  • JSR223 PostProcessor: Executes custom scripts using languages like Groovy, JavaScript, or others to manipulate the response data or perform custom actions.

  • Beanshell PostProcessor: Executes custom scripts using the Beanshell scripting language to modify the response data.

What is JSR223 Groovy post-processor in Jmeter?

This Post-Processor leverages the JSR223 (Java Specification Request 223) API to integrate the powerful Groovy scripting language into your JMeter test plan.

With the JSR223 Groovy Post-Processor, you can manipulate the response data obtained from a sampler, extract specific information, perform calculations, modify variables, or execute any custom logic you require. Groovy is a dynamic language that seamlessly integrates with Java, making it a popular choice for scripting in JMeter due to its flexibility and ease of use.


How to add JSR223 post-processor in Jmeter?

  1. Add a JSR223 Groovy Post-Processor as a child element of the sampler you want to process.

  2. Write your Groovy script inside the Post-Processor.

  3. The script will have access to various JMeter variables and objects, including the response data from the sampler.

  4. The Groovy script will be executed after the sampler, and any modifications or extractions you perform will be applied to the subsequent elements in the test plan.

When to use JSR223 post-processor in Jmeter?

It is important to note that, JSR223 post processor should be used only when JMeter's built-in components are not able to fulfil your need. An improper use of JSR223 post processor can have misleading impact on performance figures.

JSR223 Post-processor allows access to different variables like prev, ctx, sampler, props, vars etc. prev and ctx help in retrieving the information of last sample, with this information custom logic can be added in the post-processor code.

Below are the few examples of JSR223 post-processor use cases.

Custom Assertion

When Jmeter's built-in elements are not capable to add a custom assertion, JSR223 post-processor can be used in that case. For instance, when a response is received in an encrypted or encoded format, attempting to assert the content of the encoded object becomes challenging. In such situations, JSR223 post-processor can be utilised to decode the response, enabling verification of the expected content.

import java.nio.charset.StandardCharsets;
import java.util.Base64;

// Get the previous sampler response
def base64String = prev.getResponseDataAsString();

// To test the script you can use enable the below line. It should decode the string to "hello world"
//base64String = "aGVsbG8gd29ybGQ="

try {
    // Decode the Base64-encoded string
    byte[] decodedBytes = Base64.getDecoder().decode(base64String)
    // Convert the decoded bytes to a string using the appropriate character set (e.g., UTF-8)
    String decodedString = new String(decodedBytes, StandardCharsets.UTF_8)
    // Log the decoded string"Decoded String: " + decodedString)
    // You can now use the 'decodedString' variable for further processing or assertions
} catch (Exception e) {
    log.error("Error decoding Base64: " + e.toString())


Logging Failed Response

In scenarios where you wish to log the details for failed samples in console, a JSR223 post-processor can be utilised in this case. A simple script as below will log the failed sample details in jmeter log console.

if (!prev.isSuccessful()) {"================Failed Sample===================");"Sample failed - Response Code: ${prev.getResponseCode()}")"Sample failed - Response Message: ${prev.getResponseMessage()}")"Sample failed - Response Data:\n${prev.getResponseDataAsString()}")"===================================");

If you would like to log above details in your system console, you can `` instead of ``.


Fetching Cookies Value

JSR223 post processor can be used to manage cookies, including extraction, manipulation, and setting. A groovy script as below can be used to fetch the cookies of last sample:

Refer to javadoc of CollectionProperty class to understand various available function within the class.

def response = prev.getResponseDataAsString()
def headers = prev.getResponseHeaders()
// Check if cookies are present
if (headers.contains("Set-Cookie")) {"Cookies found: ${cookies}")
} else {"No Cookie Found")

Ignore Failed Samples from Result

A small JSR223 post-processor script as below can remove the failed sample from Jmeter result.

if (!prev.isSuccessful()) {"Ignoring the result since sample failed");

Modifying the user defined variables

JSR223 post-processor script can be used to manipulate the user-defined variables.

vars.put("key", "value");             

Fetch the headers from previous response
// Get the previous sampler response headers String serverHeaderValue = prev.getResponseHeaders();;

JSON Parser

A complex json as below can be easily parsed in JSR223 Groovy script.

  "users": [
      "id": "user123",
      "name": "Alice",
      "email": "",
      "privilege": "admin",
      "promoCode": "ALICE20"
      "id": "user456",
      "name": "Bob",
      "email": "",
      "privilege": "host",
      "promoCode": "BOB15"
      "id": "user789",
      "name": "Charlie",
      "email": "",
      "privilege": "guest",
      "promoCode": "CHARLIE10"
import groovy.json.JsonSlurper

def jsonResponse = prev.getResponseDataAsString() //read response

def json = new JsonSlurper().parseText(jsonResponse)
def guestUser = json.users.find { it.privilege == "guest" }
def guestPromoCode = guestUser?.promoCode
vars.put("guestPromoCode", guestPromoCode)"Guest Promo Code: ${guestPromoCode}")


In conclusion, the JSR223 Post-Processor in Apache JMeter is a powerful tool as it allows you to write script in different languages like Java, Groovy, and others to manipulate the response entities. It provides a wide range of possibilities for modifying responses, extracting data, implementing complex logic, and more. However, like any tool, it comes with its own set of pros and cons that should be considered judiciously for effective and efficient usage as it can have an impact on performance.

About Author

Dheeraj is an experienced QA consultant with over a decade of expertise in working with diverse clients. He possesses comprehensive knowledge of test automation, test strategy, and test planning. Dheeraj is passionate for quality, advocating for a quality mindset within his team through his skills.

Recent Posts

See All


bottom of page