Load Testing with Postman and k6

Reading Time: 4 minutes

Hi folks,

In this short blog, we will explore how we can use our functional postman collections to perform load testing. You may think that we already have a postman runner and we can run the collections by increasing the iteration. But this approach has a major flaw in it, we’ll explore it later. So without much ado, let’s get started.

Load Testing

I’m pretty sure that most of you already know what load testing is. For those, who are new to this concept, let me explain it quickly in a single sentence.

Load Testing is a non-functional software testing process in which the performance of a software application is tested under a specific expected load. It determines how the software application behaves while being accessed by multiple users simultaneously.

So this part is done and dusted. Now, let’s quickly move to load testing using Postman.

How to do it?

Load testing with the postman is not something that we usually do and as I mentioned earlier, one may advocate that we already have the postman runner. On top of that, the postman runner comes by default when we install the postman application, so why do we need to use something else? My answer is, I agree with this rationalization if we overlook one flaw. Let me explain below.

Why not use Postman Runner?

The only reason for this is parallel execution. Suppose, you are using postman runner to perform load testing by keeping the run iteration to be, say, 100. In this scenario, you are making a request for each test 100 times sequentially only. So in technical terms, this is not pure load testing.

The performance of an application can be best measured when you create virtual users who may try to access the application parallel. In this case, you can measure the actual robustness of your application. I hope, I was successful to apprise you for what I’m trying to explain.

Moving on, let’ see how we can use the k6 library to perform load testing via the postman.

How to use k6 with the Postman?

Installtion

Before moving on to the implementation part, the main pre-requisites are listed below.

  • NodeJs
  • npm

Since k6 is based on javaScript, it makes sense to have JavaScript run-time environment on our machine. Once we have these pre-requisites satisfied, we can proceed with installing k6 on our machine.

Installing k6

Just like many other tools, for example, Gatling, k6 is k6 is an open-source load testing tool for testing the performance of APIs, microservices, and websites. This is written in javaScript and need npm and nodes js to work smoothly.

Moving on to the installation, open up the terminal and execute the following commands, assuming that you already have npm configured.

npm install -g postman-to-k6

And voila, that’s it. You have now k6 installed on your machine.

Continuing ahead, now let’s move to the implementation part.

Implementation.

Considering that once you have all the things installed correctly, now first step would be to export your postman collection. Lets us suppose, the collection that you have imported is named test.json. So, the command that you need to execute now is given below.

postman-to-k6 test-api.json -o k6-script.js

This will convert your postman collection to a k6-script.js javaScript file. This javaScript file will have all your tests and assertions and you can further modify it to have virtual users and the duration of the test as per your requirement. These tests, as I mentioned earlier, will run in parallel.

The converted file will look something like this.

// Auto-generated by the postman-to-k6 converter

import "./libs/shim/core.js";

export let options = { maxRedirects: 4 };

const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
  options
});

export default function() {
  postman[Request]({
    name: "Dummy GET call",
    id: "7e00f6d1-af40-4162-a4a7-42bacb37f163",
    method: "GET",
    address: "https://reqres.in/api/users?page=2",
    post(response) {
      pm.test("Successful response code 200.", function() {
        pm.response.to.have.status(200);
      });
    }
  });
}

In the options, you can define the load that you want to create. For example

export let options = { maxRedirects: 4,
  duration: '1m',
  vus: 250,
   };

Here, the test duration is 1 minute and we have 250 virtual users accessing the API parallel. We are now ready to move to run the load tests.

Execution

Now we have the test script ready, we can run the load test. Use the following command to do so.

k6 run k6-script.js

And we are done, we have successfully validated the performance of the API for 250 virtual users accessing the API parallel for a duration of 1 minute.

Upon successful execution, it will look something like this.

In this image you can see that it has all the statistics, be it average response time and whatnot.

So that’s it, just a note, if you want your environment file to be a part of the js script, use the following command.

postman-to-k6 test-api.json -e env.json -o k6-script.js

Just one final and crucial thing is left before we wrap up the blog tutorial. Reporting!

Reporting

This is a very important thing while working on a project. Thorugh reporting we can communicate the performance and test results to all the concerned people. In k6, we cannot just copy-paste the console log to communicate the test results. For this, we need to modify our js test script.

Add the following line to the .js script. In the starting, before the test, just add

import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";

And after the tests, add this function to generate the report

export function handleSummary(data) {
  return {
    "summary.html": htmlReport(data),
  };
}

This is all that you need to convert your console log to a readable HTML file. Now simply run your test using the k6 command, as mentioned earlier, and you will have a report by the end of execution.

The report will look something like this,

That’s it, folks. I hope you liked the blogs. Thanks!

Refrences

https://k6.io/docs/

Scala Future

Written by 

Sparsh is a QA Consultant having experience of more than 2 years. He is familiar with the core concepts of manual and automation, Karate, Cypress, Gatling, Rest Assured and Selenium are the tools that Sparsh is familiar with. He is always eager to learn new and advanced concepts in order to upskill himself.