JMeter BeanShell PreProcessor
JMeter BeanShell PreProcessor
Pre-Processors execute before the sampler runs. They allow you to dynamically manipulate variables, set up preconditions, or modify requests on the fly.
BeanShell PreProcessor
This pre-processor uses BeanShell scripting (a Java-like language) to run code. It has access to JMeter variables (vars
), properties, and logging functions (log
).
An example of a JMeter Pre-Processor and a breakdown of how it works. This example uses a BeanShell PreProcessor to modify a variable before the sampler runs.
-
Setup:
- Create a new Thread Group.
- Add a Sampler (e.g., HTTP Request) under the Thread Group.
- Right-click on the HTTP Request and add a Pre-Processor → BeanShell PreProcessor.
-
Script Example:
-
Explanation:
- Purpose: This pre-processor script generates a unique identifier by appending the current timestamp to the prefix “ID_”. This can be useful for ensuring each request has unique data, such as for avoiding caching issues.
vars.put(...)
: Saves the computed value into a JMeter variable nameduniqueId
that can be referenced later in your HTTP Request (e.g., in parameters or headers) using${uniqueId}
.log.info(...)
: Logs the generated ID to the JMeter log file for debugging purposes.
Example Use Case
Imagine you need to send a unique value in every HTTP request to avoid server caching issues. Instead of manually updating this value, you can use a pre-processor to automatically generate and set it. Later in your HTTP Request, you can refer to the variable as ${uniqueId}
, and each request will have a unique identifier based on the current time.