With Grafana K6, a Quick Overview Of Dynamic Correlation In Performance Testing

Reading Time: 4 minutes

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:







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

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 


  //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);



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




<name>Mayank Narayana</name>





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?


  // 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);


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!!




Written by 

I am a Software Consultant in knoldus software limited, I have 6 months past experience and I am a quick learner, self-motivated, and etc

Leave a Reply