When building your JMeter test, samplers provide the core functionality. They are responsible for sending the actual HTTP requests and handling those responses. However, as your test becomes more complex and sophisticated, it may be desirable to add conditional logic to your requests. Furthermore, you may wish to dynamically process the inputs and outputs to these. JMeter provides this functionality though a host of preprocessors and postprocessors. This brief post will serve as a guide to JMeter preprocessors and postprocessors, comparing and contrasting their use in JMeter test plans.
JMeter Preprocessors
Most of the available preprocessors are included with JMeter (though a few exist as plugins). These can be added to your test plan just like any other element as shown below:
The key difference between a preprocessor and a postprocessor is when your custom logic is executed. As the name suggests, a preprocessor runs before any requests existing at the same level in your test plan.
For the purposes of illustration we will add a JSR223 preprocessor to a sample test plan. This simple component allows us to specify a few lines of Java-based code to run before our requests. In our case, we’ll define a simple test plan consisting of a single thread group, with an HTTP Request element and our preprocessor. We will also add a View Results Tree element so we can see the output:
Our HTTP Request element we will set up with the following parameters. For this request we will be accessing a Wikipedia page, with the specific article to be determined by the preprocessor.
Note that the request path takes the form of https://en.wikipedia.org/wiki/${path_to_article}
, where the ${...}
notation is shorthand for the insertion of a JMeter variable which we will define in our script. We then set up the JSR223 Preprocessor with the following script:
import java.util.Random;
String[] paths_to_articles = ["Hydrogen", "Oxygen", "Water"]
Random random = new Random();
int number = random.nextInt(3);
vars.put("path_to_article", paths_to_articles[number]);
The variable paths_to_articles
is an array containing references to articles on Hydrogen, Oxygen, and Water, respectively. These are intended to correspond to Wikipedia articles on these topics. The purpose of the random
variable is to select a topic at random each time the test is run.
We can then run our test and view the results within the output area of the View Results Tree element.
After multiple runs of our test, each of the possible endpoints are hit by the single HTTP Request element. We can certainly get more sophisticated with our preprocessor and execute more complex pre-request logic, however the concept conveyed here would remain the same.
JMeter Postprocessors
As mentioned earlier, the differentiating feature of a JMeter Postprocessor is that it runs after any request elements at the same level within our test plan. This is useful in the case where we would like to take an action depending on the response from any such request. There is an equivalent JSR223 Postprocessor to the above example that would allow us to run a script to analyze the response. However, a simpler and more practical example might be worth a look in the form of the Result Status Action Handler. If our request fails for any reason due to an error, we might want to abort the test or take some other action. Below is an example of how we would configure the Result Status Action Handler to do just that:
Trying to request a URL from Wikipedia that does not exist, our HTTP Request element throws an error, with the following output in the logs:
15:50:34,931 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
15:50:34,931 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)
This indicates that the Result Status Action Handler has ended the test as a result of this error.
Other JMeter Preprocessors and Postprocessors
JMeter comes with a rich selection of preprocessors and postprocessors. The complete listing of all of these components can be found in the component reference section of the JMeter User’s Manual, but here are a few selections that can be particularly useful in your JMeter tests:
JMeter Preprocessors
- HTML Link Parser – Attaching this element to HTTP Requests in your test plan, JMeter can perform matches on links and form fields to generate dynamic requests.
- Sample Timeout – Using this element, you can specify a request-specific timeout that will interrupt a sample if it takes too long to complete.
- JSR223 Preprocessor – This element is covered in the example above, and is extremely versatile as it allows you to specify a Java-based script to perform any action. It also supersedes the BeanShell Preprocessor performing much of the same functionality.
JMeter Postprocessors
- Regular Expression Extractor – With this postprocessor, you can specify regular expressions that will parse sampler output and set local variables to matches. These variables can then be used in subsequent HTTP Request samplers or even processed further with custom scripting.
- Result Status Action Handler – This is a simple postprocessor used in the example above, which can take an action when an error is encountered within your test.
- JSR223 Postprocessor – This element functions much in the same way that the JSR223 Preprocessor does, however it executes your custom script after any associated sampler completes. It also replaces the BeanShell Postprocessor.
Did you know that RedLine13 offers a full-featured free trial that grants time-limited access to all Premium level features? Sign up now and start load testing with RedLine13 today!