May 13, 2021

A Quick Guide to JMeter PreProcessors

Open Source Automation
Functional Testing

 

Back to top

What are JMeter PreProcessors?

PreProcessors are JMeter elements that are used to execute actions before the sampler requests are executed in the test scenario. PreProcessors can be used for different performance testing needs, like fetching data from a database, setting a timeout between sampler execution or before test data generation. The options are plentiful and they are limited only by your imagination and your business needs.

This blog post will go over the 8 JMeter core PreProcessors: BeanShell PreProcessor, JSR223 PreProcessor, HTML Link Parser, HTTP URL Re-writing Modifier, JDBC PreProcessor, RegEx User Parameters, Sample Timeout and User Parameters.

 

Back to top

BeanShell PreProcessor

Let’s assume we want to test a request that requires a “token” parameter. This parameter can be any random string. We could use a hardcoded string for all requests, but that would make our request less realistic than a real-life scenario, since tokens are not the same for all users and workflows.

This is a great example of when we can easily use a PreProcessor to generate a random string and to use it in a sampler. The BeanShell preprocessor is suitable for this since it enables managing any programmer task in your performance scripts, if you are familiar with Java programming language. BeanShell is a lightweight scripting language that has Java-like syntax.

To add the BeanShell PreProcessor to the Sampler request:

Right Click on the Sampler -> Add -> Pre Processors -> BeanShell PreProcessor

jmeter beanshell preprocessor

BeanShell PreProcessor configs:

  • Name - the name of the sampler shown in the Thread Group tree
  • Reset bsh.Interpreter before each call - Resets the interpreter before each call and cleans occupied memory. Setting this option as “True” might be useful for long running scripts, since repeated invocations might consume a lot of memory
  • Parameters - JMeter parameters that will be passed to the BeanShell script. You need to keep in mind that you cannot use JMeter variables in the BeanShell preprocessor if they are not specified in this configuration field. If a parameter is specified, you can use it in the preprocessor like this:
StringBS_Variable_Name=vars.get("JMeterVariable");
  • File Name - the path to an external BeanShell script that needs to be run

The BeanShell preprocessor script might be external or internal. If it’s internal, you can write it into the “Script” field of the BeanShell Preprocessor. In any other case, you need to use the ‘File Name’ configuration field. To generate a random string, you can use this simple code example:

importjava.util.Random;chars="1234567890abcdefghiklmnopqrstuvwxyz-";intstring_length=36;randomstring="";for(inti=0;i<string_length;i++){RandomrandomGenerator=newRandom();intrandomInt=randomGenerator.nextInt(chars.length());randomstring+=chars.substring(randomInt,randomInt+1);}print(randomstring);vars.put("RANDOM_STRING",randomstring);

 

The Result “BeanShell PreProcessor”:

 

jmeter beanshell preprocessor

With the PreProcessor in place, you can now use the random script you created in your sampler:

jmeter beanshell preprocessor

Now, if you add a listener and verify a send request, you will see that your request will contain a unique token.

jmeter beanshell preprocessor

Back to top

JSR223 PreProcessor

The JSR223 PreProcessor is another way of scripting by using PreProcessors. JSR223 has similar functionality to the BeanShell preprocessor. The main difference from BeanShell is that you can use additional scripting languages: ecmascript, groovy, java, javascript, jexl and nashorn.

With the JSR223 preprocessor you need to make sure that your script does not use JMeter variables directly in the script code, as there is a caching mechanism that will only cache the first replacement and all next variable updates will not be available in preprocessor at all.

To add the BeanShell PreProcessor to Sampler request:

Right Click on the Sampler -> Add -> Pre Processors -> JSR223 PreProcessor

To show the JSR223 preprocessor in action we can rewrite the previously created script for a random string creation which we used with BeanShell. This time we will use JavaScript.

 

functiongenerateRandom(){vartext="";varpossible="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(vari=0;i<36;i++)text+=possible.charAt(Math.floor(Math.random()*possible.length));returntext;}varrandom=generateRandom();vars.put("RANDOM_STRING",random);

 

jmeter jsr2233 preprocessor

We recommend using Groovy with the JSR223 PreProcessor.

Back to top

The HTML Link Parser PreProcessor can be used to parse a response, extract all the found links and request them further. This can be useful when the main goal of your script is to simulate web crawling.

First of all add a basic request to your home page.

jmeter html link parser preprocessor

After that, add a controller. The Simple controller can be used to make a random request using a link from a previous sampler response, and the While controller with a condition can be used for crawling all links.

The next step is to add a Sampler that will limit the scope of crawled pages.

jmeter html link parser preprocessor

As a last step, add the “HTML Link Parser”:

Right Click on the Sampler -> Add -> Pre Processors -> HTML Link parser

jmeter html link parser preprocessor

When you run your script, you will see that JMeter parsed response from the initial request and crawled all the links it found:

jmeter, HTML Link Parser preprocessor

Please note this preprocessor is less popular than the others.

Back to top

HTTP URL Re-writing Modifier

The HTTP URL Re-writing Modifier PreProcessor can be used if your application uses URL rewriting in order to store a connection session. Briefly, it allows your script to find a session id parameter on a page and add that parameter to all the requests in its scope.

To add the Re-writing Modifier:

Right Click on the Sampler -> Add -> Pre Processors -> HTTP URL Re-writing Modified

jmeter html url re-writing modifier

HTTP URL PreProcessor configs:

  • Name - the name of the element in the Thread group
  • Session Argument Name - the parameter name that will be parsed from the response and used as a substitution in all further requests
  • Path Extension - should be checked in case your application rewrites URLs by appending a semi-colon with the session parameter.
  • Do not use equals in path extension - should be checked if your application rewrites URLs without the “=” sign between the parameter name and value
  • Do not use questionmark in path extension - should be checked if your application needs to prevent query strings from ending up in the path extension
  • Cache Session Id - should be checked in case your application requires a session Id for later use when the session Id is not present
  • URL Encode URL Encode - should be checked if your application needs encoded URLs

Now, all you need to do is to enter the name of the session ID into the ‘Session Argument Name’ field. If it’s found, it will be added to all further requests. If further requests already have session ID values, then these values will be replaced.

Please note this preprocessor is less popular than the others.

 

Back to top

JDBC PreProcessor

By using the JDBC PreProcessor, you can run SQL statements right before your sampler. JDBC is an application programming interface that defines how a client can access a database.

Let’s assume that we have a login workflow that contains a user email and password as request params.

jmeter jdbc preprocessor

You can keep these test values in the sampler itself, but that would be very inefficient, since each time your user updates their email or password, you would have to update your script accordingly. This is a great example of when you can substitute hardcoded parameters with dynamic values fetched from database.

First of all, we need to make a proper configuration of the JDBC driver for your database. After that, add the JDBC PreProcessor:

Right Click on the Sampler -> Add -> Pre Processors -> JDBC PreProcessor

jmeter, jdbc preprocessor

JDBC PreProcessor configs:

  • Name - the name of sampler shown in the JMeter tree
  • Variable name - the name of the connection pool (specified in JDBC Connection Configuration)
  • Query type - all commonly used statement types (for example, you can not use select type if your query updates db values)
  • SQL Query - the SQL query itself (without a trailing semi-colon)
  • Parameter values - a list of comma-separated parameters if the SQL query is parameterized
  • Parameter types - a list of comma-separated SQL parameter types (see more about supported types here: Javadoc for java.sql.Types)
  • Variable Names - a list of comma-separated variables to keep fetched database data values which are returned from database
  • Result Variable Name - key-values variable containing a returned dataset
  • Query timeout (s) - max seconds of results querying
  • Handle ResultSet - specify how query results should be handled

In our case, we can just specify the Select Statement to retrieve the required data from the database.

jmeter, jdbc preprocessor

After that, we can substitute the input parameters with dynamically fetched data:

jmeter, jdbc preprocessor

Back to top

RegEx User Parameters PreProcessor

 

The RegEx User Parameters PreProcessor should be used in combination with the Regular Expression Extractor PostProcessor. It allows you to use dynamic values for the HTTP request that have been extracted from another HTTP sampler. With this component you can use extracted names and values of different parameters in response to the previous request.

Let’s assume we have the first http request which returns the response body with this kind of html:

 

……..


   
    


…….. 

 

We see that all parameters contain names and values. In this case we can make a generic regex to extract all variables including name and appropriate value which will be separated using regex groups (round brackets):

jmeter, RegEx User Parameters PreProcessor

Based on the specified regex, we see that ‘listParams’ reference will contain all variables parsed from first request. Group 1 of that regexp will return names while group 2 will return the appropriate value. In this case we can specify RegEx User Parameters configuration this way:

jmeter, regex user parameters preprocessor

With this configuration, our second http request will take all the parameters from the listParams reference and use them as request parameters. In other words: RegEx User Parameters help to parse list of parameters form the http request and easily apply these parameters in next request.

 

Back to top

Sample Timeout PreProcessor

 The Sample Timeout PreProcessor specifies the max time execution for the specified sampler. If the sampler execution takes too long, it will be interrupted.

To add the Sample Timeout preprocessor to the Sampler request:

Right Click on the Sampler -> Add -> PreProcessors -> Sample Timeout  

Please note that only the following samplers support this interaction: AJP, BeanShell, FTP, HTTP, Soap, AccessLog, MailReader, JMS Subscriber, TCPSampler, TestAction, JavaSampler.

Based on JMeter documentation, this sampler is marked as “BETA CODE - the test element may be moved or replaced in a future release”. You need to keep this in mind if you usually upgrade your JMeter with new versions.

 

Back to top

User Parameters PreProcessor

 

The User Parameters PreProcessor specifies user input parameters specific to individual threads. For each thread, the variable value will be used based on the order of that user thread in sequence.

jmeter user parameters preprocessor

 If you want to update variables using only one iteration and to make sure the values are updated each time based on the execution of the parent controller, you need to check the “Update Once Per Iteration” checkbox.

In general, this preprocessor is one option for parameterizing your requests. But usually, csv data set configuration element provides more flexibility for parameterization.

Congratulations! You now have basic knowledge of JMeter’s core processors. Want to learn more JMeter? Check out helpful resources and earn certifications at BlazeMeter University.

START TESTING NOW

 

Back to top