Pre-Processors and Post-Processors in JMeter

Apache JMeter is an open-source performance testing tool popular for assessing and evaluating web application performance. Regarding performance testing, JMeter provides an extensive feature set to simulate different scenarios and stress levels. Pre-Processors and Post-Processors are two vital parts that are particularly important in increasing the adaptability and efficiency of JMeter scripts.

JMeter processor
JMeter processor

This Answer provides a thorough exploration of Pre-Processor and Post-Processor concepts in JMeter.

Pre-Processors in JMeter

A JMeter Pre-Processor is a scripting component that enables users to carry out specific tasks before the actual sampler runs. It’s an effective tool for preparing and customizing the test data since these runtime operations allow us to modify the request parameters or variables. This flexibility is crucial for setting up realistic and effective performance tests.

Types of Pre-Processors

Pre-Processors from JMeter come in various forms, each with a distinct function. Typical ones are as follows:

Types of Pre-Processors
Types of Pre-Processors

1. User Parameters

It permits the customization of user input dynamically. Imagine we’re testing an online banking application where each user needs a unique session ID. A User Parameters Pre-Processor can be set up to generate and assign unique session IDs to each virtual user dynamically:

User Parameters:
- Parameter Name: sessionId
- Parameter Value: ${__UUID()}
Example of a User Parameters Pre-Processor

This setup ensures that each user session in our test has a unique session ID, more accurately mimicking actual usage scenarios.

2. HTML Link Parser

It extracts links from HTML responses for use in later queries. For a website with numerous internal links, using an HTML Link Parser Pre-Processor can automate the extraction and utilization of these links. This Pre-Processor will parse the HTML responses to identify and store links, which can then be used in subsequent requests to simulate realistic browsing behavior.

HTML Link Parser:
- URLs to include: .*
- URLs to exclude: none
Example of an HTML Link Parser Pre-Processor

3. BeanShell Pre-Processor

It enables the execution of custom actions using BeanShell scripting. A BeanShell Pre-Processor can be used to perform complex data transformations or conditional logic before sending a request. For example, we might want to modify a request parameter based on a certain condition:

import org.apache.jmeter.protocol.http.control.CookieManager;
CookieManager cookieManager = sampler.getCookieManager();
if (cookieManager != null) {
// Custom logic to modify cookies or parameters
cookieManager.add(new org.apache.jmeter.protocol.http.control.Cookie("authToken", "12345", "example.com", "/", false, 0));
}
Example of a BeanShell Pre-Processor

Use Case for Each Pre-Processor

Pre-Processor

Use Case

User Parameters

This Pre-Processor is used to vary user-specific data, such as usernames or account numbers, during the test execution.

HTML Link Parser

It automatically processes web pages to find and utilize links, which is essential for simulating realistic user navigation patterns.

BeanShell Pre-Processor

It provides the ability to write and execute complex scripts for data manipulation, making it highly versatile for various preprocessing needs.

Dynamic parameterization

Pre-Processors are frequently used for dynamic parameterization, an essential component of practical performance evaluation. For example, a Pre-Processor may dynamically generate and insert the unique identification needed for each request made during a user session.

Handling authentication

Pre-Processors come in handy especially when there is authentication involved. To provide a realistic and seamless user experience, a Pre-Processor can build up the required credentials, authentication tokens, or session parameters before submitting a request.

Post-Processors in JMeter

Conversely, JMeter’s Post-Processors take action after the execution of the sampler. They’re in charge of handling the replies that the server sends them. Post-Processors are essential to extracting and processing data from the server’s responses, making them a crucial component of the validation and analysis of results.

Types of Post-Processors

JMeter provides a variety of Post-Processors to meet various requirements. Some of the prominent ones are:

Types of Post-Processors
Types of Post-Processors

1. Regular expression extractor

It uses regular expressions to extract data from the server response. Consider a scenario where a user logs in, and the server response contains a session ID. The Regular Expression Extractor can be used to pull this session ID from the response:

Response:
{
"sessionID": "abc123xyz"
}
Regular Expression Extractor:
- Reference Name: sessionID
- Regular Expression: "sessionID": "(.+?)"
- Template: $1$
- Match No.: 1
- Default Value: NOT_FOUND
Examaple of a regular expression extractor Post-Processor

This setup captures the session ID and stores it in a variable named sessionID for use in subsequent requests.

2. JSON extractor

It makes it easier to retrieve data from JSON responses. If the server response is in JSON format and contains user details, the JSON Extractor can be used to retrieve specific fields:

Response:
{
"user": {
"id": 123,
"name": "John Doe"
}
}
JSON Extractor:
- Variable Name: userId
- JSON Path Expressions: $.user.id
- Default Value: NOT_FOUND
Example of a JSON extractor Post-Processor

This configuration extracts the user ID from the JSON response and stores it in the userId variable.

3. XPATH Extractor

It uses XPATH expressions to extract data from XML responses. For responses in XML format, the XPATH Extractor can be employed to extract data using XPATH expressions:

Response:
<response>
<user>
<id>123</id>
<name>John Doe</name>
</user>
</response>
XPath Extractor:
- Reference Name: userId
- XPath Query: /response/user/id/text()
- Default Value: NOT_FOUND

This setup extracts the user ID from the XML response and assigns it to the userId variable.

Data extraction and validation

Post-Processors are frequently used to extract data from server responses, which can be provided as arguments to subsequent requests or utilized for additional validation. For instance, a user ID or session token can be taken from a response and utilized in the next request.

Data transformation

Additionally, data received from server responses can be transformed or manipulated by using Post-Processors. This is quite helpful when data has to be formatted or changed before being utilized in further queries.

Conclusion

To sum up, JMeter’s Pre-Processors and Post-Processors are essential for increasing the adaptability and effectiveness of performance testing scripts. While Post-Processors make retrieving and processing data from server responses easier, Pre-Processors let users customize and prepare test data dynamically. By effectively using these components, performance testers can create realistic situations, confirm findings, and identify possible bottlenecks in web applications. Utilizing JMeter’s full capability in performance testing requires understanding its Pre-Processor and Post-Processor applications.




Copyright ©2024 Educative, Inc. All rights reserved