How to use Processors in JMeter? What is a Pre-Processor? Practical hands on exercise

The behavior of a Sampler can be modified using Processors in JMeter.

In the previous topic, we learnt how to create a Sampler (request to the server). JMeter creates the Sampler with default properties. What happens if you want to modify that sampler? JMeter offers a valuable feature known as Processor.

There are two types of Processors in JMeter: Pre-Processors and Post-Processors. In this topic we will learn about Pre-Processors. We will explore Post-Processors in the next topic.Processors_In_JMeter_Example

Pre-Processor

Pre-processor performs the action before creating the Sampler request and it runs immediately before the Sampler is run.JMeter_Pre_Processor_guide

Why do we need a Pre-Processor?

Let us assume that you want to pass parameters like Username, Password or Category etc that need to be specific to individual threads. Specifying them in the Test Plan will not work since they would be common to all thread. In such a scenario, you can use the User Parameters Pre-Processor.

You can add a User Parameter Pre-Processor by right clicking on a Thread Group > Add > Pre-Processors > User ParameterTutorial_Add_User_Parameters_Pre-Processors_In_JMeter_Example

Clicking on Add Variable button will add another row where you can add variables like username, password, category etc. You can specify the variable name in the Name: column. Clicking on Add User will add columns like User_1, User_2 where you can add thread specific values for each variable.

These variables are available to any element in the same Thread Groups and they can be retrieved through the function syntax: ${variable}.Tutorial_Configure_User_Parameters_In_JMeter_Example

Practical Hands On Pre-Processor Exercise

Let us assume that you are working for Amazon and you want to test the performance of category pages of Amazon.com The URL for loading the category page is http://www.amazon.com/b/ref=nav_shopall_tg?ie=UTF8&node=abcd

Each category has a node id associated with it. In the above URL, we will need JMeter to dynamically add the node id to the parameter node (which will replace the value abcd in the URL above). In our example, we will test the below categories.

Category Node Id
Books 283155
Movies and TV 2625373011
TV and Video 1266092011
Toys and Games 165793011

Steps to execute the example

  1. Start JMeter and create a Thread Group. Right click on the Test Plan > Add > Threads (Users) > Thread Group
  2. In the Thread Properties set the Number of Threads to 4, Ramp up period to 5 seconds and Loop Count to 1.
  3. Right click on the Thread Group > Add > Sampler > HTTP Request
  4. Configure the JMeter HTTP Request Sampler as shown in the screenshot.
    1. Server Name: www.amazon.com
    2. Path: b/ref=nav_shopall_bty
    3. In the Parameters tab add “node” under the column “Name:” and ${nodevalue} under the value. This tells JMeter that we want to add a parameter called node and its value should be replaced by the value of the variable – nodevalue. We will create the variable in Step 5.
    4. JMeter will combine the values from Server Name, Path and the Parameters during execution and invoke the complete URL.
    5. Be sure to check the Follow Redirects checkbox since Amazon will redirect this URL to its preferred one.Pre-Processor_Tutorial_HTTP_Request
  5. Right click on the HTTP Request created earlier and choose Add > Pre-Processors > User Parameter. This will add the User Parameter element under the HTTP Request.
  6. Configure the JMeter User Parameter Pre-Processor element in the right pane.
    1. Click on Add Variable and enter “nodevalue” under the “Name:” in the table.
    2. This will add a variable called “nodevalue”.
    3. Double click on the empty cell under User_1 and enter 283155 – the node id for the category Books.
    4. Click on Add User and double click on the cell under User_2 to enter 2625373011, the node id value for the Movies and TV.
    5. Repeat Step d to go over the process time for User_3, User_4 and enter their node ids.JMeter_Pre-Processor_Tutorial_User_Parameter
  7. JMeter will substitute the variable “nodevalue” in the Sampler we created in Step 4 with the values we provided in the User Parameter, for each thread.
  8. Now that we have a way to send the request and add parameters to it, it is time to setup a way to read the response. We need to add a View Results Tree Listener in JMeter.
  9. Select the Thread Group, right click on the Thread Group and choose Add > Listener > View Results Tree. You can experiment with other Listener later but for this exercise we will choose the View Results Tree. You can also multiple Listeners to the same Thread Group to see the data presented by each Listener.
  10. Your Test Plan should be similar to the one in the screenshot below.JMeter_Pre-Processor_Tutorial_Test_Plan
  11. Save your Test Plan and run it by clicking on the Start button. Select the View Results Tree Listener so that you can view the result while it is recorded.
  12. You will see the HTTP Request being listed in the left pane of the View Results Tree element. Clicking on the HTTP Request listing will show you the details of the request in the right pane.
  13. You can click on each of the HTTP Request objects in the left pane and check the data in the right pane to verify that JMeter had added different User Parameter to each thread.JMeter_Pre-Processor_Tutorial_Results

The above example involved redirection of the request, so it is not the ideal way to test a category page. However, it served our purpose of showing how the User Parameter Pre-Processor can be used. Ideally it would be preferable to hit the category page directly, when testing its performance.

You can download the Pre-Processor JMeter Test plan and run it on your machine to understand it better.

In this topic you learnt about Pre-Processors. The next topic will teach how to use of Post-Processors in JMeter.