How to Handle Correlation in JMeter


December 7, 2015
9990024683_4c203d261e_o (1)

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:

  1. Navigate to the home page and Login
  2. Select “Content Link” on the top nav bar
  3. Select “Add Content”
  4. Select “Add Article”
  5. Give the article a random title
  6. Enter some text for the body of the article
  7. Save the article
  8. Verify the article was saved by using an assertion
  9. Logout

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.

image001

The text is not completely visible but if you copy and paste the Text from the Listener to a text editor it will read

Error message
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.

image002

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.

image003

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.

image004

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.

image005

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.

image006

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-(.+)” />
Template: $1$
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.

image007

If you are successful, you should be able to see the captured value in the debug sampler.

image008

Now let’s use the captured value and substitute all the parameters in the samplers where necessary.

image009

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.

image010

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.

image011

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

image012

image013

Save and run your test. You should now be able to see the newly added articles on your drupal dashboard.

image014

Conclusion:

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.

* * *

Featured “data.path” image at beginning of article provided by r2hox under cc license.


Bharath
Bharath Khambadkone loves to automate and break systems by writing automated functional and performance tests. His favourite test frameworks/tools are cucumber, watir-webdriver, selenium, and JMeter. When Bharath is away from the keyboard he loves to spend time with family, hit the links, and aspires to be a Champions Tour Player. During his time with Modus Create, Bharath was a Sr. QA/DevOps Engineer.

  • Dmitri T

    Yet another great post. I would just add that sometimes using Regular Expression Extractor is not that good option as it is very sensitive to markup and if server response will change to multiline it will be a headache to develop a regex (as it evidenced by this SO comment. So it is better to use CSS/JQuery Extractor for HTML, XPath Extractor for XHTML, XML and HTML and JSON Path Extractor for JSON.

    • Bharath Khambadkone

      Thanks @dmitri_t:disqus ! Using regex & JSON extractors have worked well for me so far. I will definitely give the rest a try next time.

  • Arumugam Sriraman

    Very Useful Post…

  • rahul

    how do correlate in headers


What We Do

We’ll work closely with your team to instill Lean practices for ideation, strategy, design and delivery — practices that are adaptable to every part of your business.

See what Modus can do for you.

LET'S GET STARTED

We're Hiring!

Join our awesome team of dedicated engineers.

Loading...

APPLY NOW