Know More About Parallel Requests In Grafana K6

female software engineer coding on computer
Reading Time: 2 minutes

Hi folks, In this blog, we will explore more about parallel requests 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 “Parallel Requests.”

Parallel requests are requests that are sent at the same time. Parallel requests are sometimes called “concurrent” requests.

Why should we use parallel requests?

Parallel requests influence load testing results because they increase test throughput. Increased test throughput affects load testing in two ways.

  • First, because sending requests in parallel takes less time than sending them sequentially, the load generator may experience more resource consumption in a shorter period of time.
  • Second, concurrent requests reach the application server faster, potentially increasing server resource use.

We also used parallel requests in production, because parallel requests make a test more realistic in certain instances. Because modern browsers support parallelism by default, a load test for a website or web app should usually account for parallel requests.

API calls may not typically be triggered simultaneously, so it may be more realistic to send requests sequentially.

Batching in k6

By default, each k6 user sends each request in the script sequentially. But we can modify this scenario by using batching, which can be used to adjust consecutive behavior.



 export default function () {


    let responses = http.batch([


        ['GET', 'https://test.k6.io'],

        ['GET', "https://reqres.in/api/users?page=2"],

        ['GET', "https://jsonplaceholder.typicode.com/"],

        ['GET', "https://compendiumdev.co.uk/"],

    ])

above script sends five requests in parallel, Each of the responses is saved like an array in the variable responses.

We also add some checks to verify the send request, like

 check(responses[0], {


    'Homepage successfully loaded': (r) => r.body.includes("Collection of 

simple web-pages suitable for load testing"),

    'main page status was 200': (res) => res.status === 200,

The above check verifies that the very first element of that array, responses[0], the homepage, contains text that proves that the script successfully retrieved the HTML body.

we need some arguments in a particular order with each request when we specified request in array

POSITION NAME DESCRIPTION
Method string / HTTP URL Mandatory. The HTTP method of the request. One of GET,
POST, PUT, PATCH, DELETE, HEAD, or OPTION.
URL string / object /
ArrayBuffer
Mandatory. The URL to request.
Body string/object /
ArrayBuffer
The body of the request is relevant. Can be set to null if not
applicable but you want to set the last params argument.
Params object Params like auth, custom headers, and tags.

You can batch requests even if they are not all HTTP GET requests. For example, you could use HTTP GET and HTTP POST requests in the same batch

Batching with request objects

Objects can also be used to store information about a request. Here’s an example of how we do it to send a POST request and use custom HTTP headers by including a Params object in the request:

 export default function () {  


    const req1 = {

      method: 'GET',

      url: 'https://jsonplaceholder.typicode.com/',

  };

    const req2 = {

       method: 'GET',

       url: 'https://test.k6.io',

  };
     const req3 = {

       method: 'POST',

       url: 'https://reqres.in/api/register',

      body: {

          "email": "eve.holt@reqres.in",
  
          "password": "pistol"

    },

      params: {

       headers: { 'User-Agent': 'k6test' },

       tags: { k6test: 'yes' }   

    },

  };

We pass all the above requests in a batch as an array.

const responses = http.batch([req1, req2, req3]);

That concludes this blog. I hope you enjoyed and learned about parallel requests 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/

https://k6.io/docs/javascript-api/k6-http/batch/

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

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading