Hello there, everyone. In this blog, we’ll talk about and learn more about dynamic correlation in Grafana K6, our favorite performance testing tool. You might be wondering what the circumstances are and why we utilize them. So, let’s start with basic correlation principles in performance testing.
The technique of retrieving a value from a prior HTTP response and using it in a further HTTP request is known as correlation.
After reading the definition, I hope you have a basic knowledge of correlation. In this blog, we’ll discover more about it. But first, let’s go through each of the K6 essential elements one by one.
Correlation in k6
Correlation in a load testing scenario refers to retrieving one or more values from one request’s response and reusing them in subsequent requests. Obtaining a token or other form of identification is frequently required to complete a series of steps in a user journey.
Why do we use correlation in k6?
When utilizing the Chrome Extension or the HAR converter to produce your test script, correlation is frequently required. This is due to the fact that those tools will record session IDs, CSRF tokens, ViewState, wpnonce, and other dynamic data from your particular session. These tokens usually have a short lifespan. This means you’ll have to deal with extracting this data from the HTML/form in order to use it in subsequent requests. This is a fairly common problem with any site that uses forms, and it can be solved with some scripting.
Basically, we have three types of techniques, so let’s explore these scripting techniques one by one.
1. Extracting values/tokens from a JSON response
Basically, we use this technique when we have an output response in JSON format, like the below:
{"page":2,"per_page":6,"total":12,"total_pages":2,"data":
[{"id":7,"email":"michael.lawson@reqres.in","first_name":"Michael","last_name
":"Lawson","avatar":"https://reqres.in/img/faces/7-image.jpg"},
{"id":8,"email":"lindsay.ferguson@reqres.in","first_name":"Lindsay","last_nam
e":"Ferguson","avatar":"https://reqres.in/img/faces/8-image.jpg"},
{"id":9,"email":"tobias.funke@reqres.in","first_name":"Tobias",
javascript k6 script:-
export default function () {
// Make a request that returns some JSON data
const res = http.get('https://reqres.in/api/users?page=2');
// Extract data from that JSON data by first parsing it
// using a call to "json()" and then accessing properties by
// navigating the JSON data as a JS object with dot notation.
const ListUser0 = res.json().data[0];
check(ListUser0, {
'first_name in list is correct' : (s) => s.first_name === 'Michael',
'last_name in list is correct': (s) => s.last_name === 'Lawson',
'email in list is correct': (s) => s.email === 'michael.lawson@reqres.in'
});
// Now we could use the "ListUser0" variable in subsequent requests...
}
Related output:-

2. Extracting values/tokens from form fields
Basically, we use this technique when we have an output response in form fields format like the below:



When we inspect the page or particular element, then we get the below scenario.



Basically, we exact one value in the following fields:
javascript k6 script:-
export default function () {
// Request the page containing a form and save the response. This gives you
access
//to the response object, `res`.
const res = http.get('https://www.mantissa.co.uk/main/IEForm/contact.php');
// Query the HTML for an input field named "Firstname","Email". We want the
value of "maxlength" and "size"
const element1 =res.html().find('input[name=Firstname]');
const element2 = res.html().find('input[name=Email]');
// Get the value of the attribute "value" and save it to a variable.
const val1 = element1.attr('size');
const val2 = element2.attr('maxlength');
// Now you can concatenate this extracted value in subsequent requests that
require it.
// console.log() works when executing k6 scripts locally and is handy for
debugging purposes
console.log('The value of field size is: ' + val1);
console.log('The value of name field is:' + val2);
sleep(1);
}
Related output:-



Generic extraction of values/tokens
We use this technique when responses may be neither JSON nor HTML at times, in which case the above extraction methods would not be applicable. In these cases, you should probably work directly on the Response. we can handle this type of situation by using FindBetween inbuilt function.
The jslib utils library contains an example of this kind of function, called findBetween. The function makes use of JavaScript’s built-in String.indexOf and therefore does not depend on the use of potentially expensive regular expression operations.
Let’s take the XML response example for a better understanding
<travelers>
<Travelerinformation>
<id>1403</id>
<name>Mayank Narayana</name>
<email>mayank-leo@hotmail.com</email>
<adderes>USA</adderes>
<createdat>2020-12-16T09:57:10.9389781</createdat>
</Travelerinformation>
</travelers>
javascript k6 script:-
import { findBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
export default function () {
// This request returns XML:
const res = http.get('http://restapi.adequateshop.com/api/Traveler?
page=6');
// Use findBetween to extract the first title encountered:
const name = findBetween(res.body, '<name>','</name>');
const email= findBetween(res.body, '<email>','</email>');
check(name, {
'name is correct': (ast) => ast === 'Mayank Narayana',
});
check(email, {
'email is correct': (ast) => ast === 'mayank-leo@hotmail.com',
});
console.log('The value of name field is:' + name);
console.log('The value of name field is:' + email);
}
Related output:-



That concludes this blog. I hope you enjoyed and learned about correlation in K6. If you require the above code, please feel free to reach out to us at knoldus techub.com.
Thank you!!
References:
https://k6.io/docs/examples/correlation-and-dynamic-data/