How to Handle Correlation in JMeter
If I were to test how a webserver would handle a static site I could simply follow the instructions from my previous post on JMeter Performance Testing, record a simple script, run it with multiple threads and that script would work just fine. However, most of the applications that we build and test are not that simple. There is always dynamic content that is returned by the web servers and JMeter needs to know how to capture that and reuse it in it subsequent requests. If you have used the HP’s Loadrunner then you will be familiar with the term Correlation where you use some built-in functions to capture a certain value using the left and right boundaries. In this post, I’m going to show you how to capture the content and use it in your requests.
For our demo purposes, I’m choosing the Drupal CMS which requires a form build ID and form token for adding a new article.
Prerequisite: Setup a Drupal environment on your local machine using Vagrant.
vagrant box add drupal http://devopera.com/node/14/download/centos6/doco6-d7-vagrant.box vagrant init drupal vagrant up
Step 1: Recording Script
Open a browser and navigate http://localhost:15080 and start recording the script. Follow steps from my previous post to record the script. The username password for the Drupal admin page is admin/admLn**
To begin, record a simple script with the following steps:
- Navigate to the home page and Login
- Select “Content Link” on the top nav bar
- Select “Add Content”
- Select “Add Article”
- Give the article a random title
- Enter some text for the body of the article
- Save the article
- Verify the article was saved by using an assertion
Once the script has been recorded successfully, add a Result Tree viewer listener and run the test.
Next, open the View Results tree by clicking it after the test finishes running.
– Select HTML from the drop down menu
– Click the Response /node/add/article?render=overlay&render=overlay
– Then click the Response Data Tab.
The text is not completely visible but if you copy and paste the Text from the Listener to a text editor it will read
The form has become outdated. Copy any unsaved work in the form below and then reload this page.
The reason you are seeing this error message is because JMeter is using the same session values from the recorded session. We need to update the script to use dynamic session values, which can be easily accomplished using a Post processing regular expression. This will capture the value needed for the variable for the next request.
Step 2: Identify the Parameters
The next step is to identify what piece of information needs to be replaced or handled. If you click and select the “ /node?destination=node” sampler you will notice in the parameter list that there is a form_build_id variable that has a unique value. Let’s try to capture that value.
Step 3: Testing identified Parameters
Use the View Results Tree Listeners RegExp Tester functionality to play around with the regular expression and test if your expression captures the required value.
We know we are looking for a piece of string that starts with the text “form-” We now have to determine a unique start location and end location for that string. The string that we need is highlighted in the screenshot below.
The left boundary will be value=”form- and the right boundary will be ” />. We need to capture the text between the left & right boundary.
The regular expression will look like this value=”form-(.+)” />. To see if this works, go to the Regular Expression Tester and search for value=”form-(.+)” />
The search returned “Match count: ” which means it was successful.
Step 4: Extract the Dynamic Value
Let us add a Regular Expression Extractor to the previous sampler. Right click the sampler and select Add -> Post Processor -> Regular Expression Extractor.
Enter the following values in the Regular Expression Extractor:
Name: Leave it as is or change it to whatever you feel like
Apply To: Main sample only
Field to Check: Body
Reference Name: form_build_id
Regular Expression: value=”form-(.+)” />
Match No: 1
Default value: form_build_id NOT FOUND
Step 5: Execution
Before we run the script to verify if we were able to capture form build id successfully, let’s add a Debug Sampler which will display the captured value. To add a Debug Sampler, right click the Thread Group and select Add > Sampler > Debug Sampler.
If you are successful, you should be able to see the captured value in the debug sampler.
Now let’s use the captured value and substitute all the parameters in the samplers where necessary.
If there are other values that need to be captured, use the same method and add the parameters. In this scenario, we can simply copy the Regular Expression Extractor and save it in the other sampler.
There is also another Value that we will need to extract: form_token. We can use the same technique, capture the value and use it in our request.
I used a Random Variable Generator for the Article Title because using the same title for new posts is not allowed. The variable name article_title can be then used in /node/add/article?render=overlay&render=overlay sampler
Save and run your test. You should now be able to see the newly added articles on your drupal dashboard.
The tricky part of identifying what needs to be captured is determining the exact piece of information that needs to be captured; once you figure that out, the rest is very easy. One technique that usually works well is recording two sessions with the same input values and comparing the two to see what changed. In our test application above, it was the form_build_id and the form_token that changed with every recording. Also, talking to the developers may give you some insight into what unique values are needed in your requests.
Questions or issues with the process? Leave a comment below or find me on Twitter.
A copy of the final script recorded is available as a gist here.