Think Time In Grafana K6

black twin bell alarm desk clock on table
Reading Time: 3 minutes

Hi folks, In this short blog, we will explore more about Think Time in our favorite performance testing tool, Grafana K6. However, you may be asking what the circumstances are and why we use them. So let’s start with some core fundamentals of “Think Time.” It is also known as “waiting time” and is sometimes called “delay.”

Think time is the amount of time a script stops during test execution to replicate delays experienced by real users while using an application.

Most probably you have got an idea about think time. If not, don’t be concerned. We will go through this briefly in the upcoming handing.

Why should we use think time in our k6 script?

You should consider adding think time in the following situations:

  • Your test follows a user flow, like accessing different parts of the application in a certain order.
  • You want to simulate actions that take some time to carry out, like reading text on a page or filling out a form.
  • Your load generator, or the machine you’re running k6 from, displays high (> 80%) CPU utilization during test execution.

When shouldn’t we use think time?

Think time is unnecessary in the following situations, so kindly keep in mind the below situation to avoid adding invalid code to the k6 test script:

  • You want to do a stress test to find out how many requests per second your application can handle.
  • The API endpoint you’re testing experiences a high number of requests per second in production that occur without delays.
  • Your load generator can run your test script without crossing the 80% CPU utilization mark.

As you can see, whether or not to use think time is determined by your testing objectives. When in doubt, take some time to think.

Let us define a word that will allow us to add think time to our script and boost its efficiency.

Sleep

In k6, you can add think time using sleep(). To use it, you’ll need to import sleep and then add the sleep to the part of the script within the default function that you want the test execution to pause.

For a better understanding, let us take a simple example,


let url1 = 'http://uitestingplayground.com/';

  let response_1 = http.get(url1, 'Quality is not an act, it is a habit.');

  check(response_1, {

      'status code of the first request, 200': (r) => r.status === 200,    

      'conform the quotation': (r) => r.body.includes('Quality is not an act, it is a habit.')

  });

// This is a format of think time.

  sleep(2);

  let url2 = 'https://the-internet.herokuapp.com/';

  let response_2 = http.get(url2, 'Welcome to the-internet');

  check(response_2, {

    ' status code of the second request, 200': (r) => r.status === 200,   
   
    'verify the text': (r) => r.body.includes('Welcome to the-internet')

});

sleep(2); which means that the script will pause for 2 seconds when it is executed.

Including sleep does not affect the response time (http_req_duration); the response time is always reported with sleep removed. Sleep is, however, included in the iteration duration.

What is dynamic think time?

The issue with hard-coding a delay into your script is that it inserts an artificial pattern into your test, which may make the demand on your application more predictable than it would be in production.

A dynamic think time is more realistic, and simulates real users more accurately, in turn improving the accuracy and reliability of your test results.

Random sleep

The JavaScript Math.random() method may be used to provide dynamic think time:

sleep(Math.random() * 5);

For a better understanding, let us take a simple example,

export default function () {

  http.get('https://the-internet.herokuapp.com/');
  
sleep(randomIntBetween(20, 30));

  http.get('http://uitestingplayground.com/');

}

The line above instructs k6 to sleep for a random amount of time between 0 and 5, inclusive of 0 but not of 5. The value selected is not guaranteed to be an integer.

Random sleep between

If you’d prefer to define your think time in integers, try the randomIntBetween function from the k6 library of valuable functions called jslib.

First, import the relevant function:

import { randomIntBetween } from "https://jslib.k6.io/k6-

utils/1.0.0/index.js";

Then, add this within your default function:

sleep(randomIntBetween(1,5));

export default function () {

  http.get('https://the-internet.herokuapp.com/');

  sleep(randomIntBetween(20, 30));

  http.get('http://uitestingplayground.com/');

}

The script will pause for a number of seconds between 1 and 5, inclusive of both 1 and 5.

Conclusion

In this blog, we studied K6’s fundamental features, which come in handy when we need to extend the request’s execution time. Next time, we’ll go into more detail about K6.

Till then, Happy learning, happy sharing!

You can discover more about this kind of blog by clicking on this straightforward link. here

Written by 

I am a self-driven self-starter with strong analytical, problem-solving, and communication skills, and I'm always looking to pick up new skills and develop my interpersonal skills. I am currently employed as a quality assurance software consultant. I am well-versed in creating automation frameworks and am familiar with key QA ideas. I've worked with automation technologies in the past, including postman.io, cypress.io, gatling.io, selenium (java), Appium, Grafana-K6, and core Java development tools, among others. Maven, Version Control GIT (GitLab Project Management Tool).