Advanced Load Testing Scenarios with JMeter - Part 1: Correlations
Carrying forward dynamic data is an important part of load testing. Read on to find out how to use correlations in JMeter to assist with this.
Join the DZone community and get the full member experience.
Join For FreeDynamic data is data that is changed when information is updated. For example, session variables connected to authentication mechanisms. This affects website recording, as JMeter needs to know how to capture this dynamic data and reuse it in subsequent requests. Otherwise, playing back the recording will show many errors.
This is where Correlations come in. Correlations is the fetching of dynamic data from preceding requests and posting it to subsequent requests.
How Does It Work?
Record the test and export it to JMeter through the jmx button.
As mentioned, dynamic values change for each subsequent page request. Here we are looking at the Token value from a flight travel agency demo (if you follow us you know this is the same demo we use all the time). If the same Token is used, the script will fail.
Add the View Results Tree Listener to see every request and response in detail.
Run the script.
The script has indeed failed because of the Token:
To correlate the value that is captured from the first page, we want to go back and find the value when it’s issued by the server.
In this example, you can find the hidden input in the form:
This variable needs to be captured in the script for reuse as a variable for the rest of the script.
Copy the variable name and check it in the Regular Expression Tester and make sure you have the name right:
To turn the name into an expression that can handle dynamic data, se parentheses and matching operators. Test to make sure it works:
Go to the page where you found the value. Add a Regular Expression Extractor as a Post Processor.
Fill out the fields:
- Reference Name - What you choose to call the variable when referencing in the script.
- Regular Expression - The expression
- Template - How to name the variable. If we had multiple matches we could use a template that included each of them.
- Match - Which match we should use. 0 is any of the matches that could happen.
- Default Value - The output when the variable isn’t found.
Now, we need to find the value in the subsequent samplers in each of the pages.
Replace the name with the value you found. Do the same for each value on each page:
We also need to add a debug sampler to the script. The debug sampler shows the variables found as used in the script. If there is a problem with finding the match or verifying we found it right - it shows us that.
Now, replay the script. Don’t forget to clear out and change back to ‘text’.
Hurray! The errors are gone now and the samplers are successful because the value was found.
Notice it is a new value:
Also shown in the debugger:
Congratulations! You now know how to use correlations. For automated scripting, check out BlazeMeter’s ‘SmartJMX’ feature. SmartJMX automatically correlates every variable, by finding the source of every URL, GET parameter, POST parameter, or JSON POST field. When finding a candidate, SmartJMX attaches the variable extractor to the corresponding sampler and uses the extracted variable instead of the constant value.
Stay tuned for part 2 - Data-driven testing on JMeter
Published at DZone with permission of Noga Cohen, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments