Wednesday, October 12, 2022

API Load Testing with K6 & Postman

API Load Testing with K6 & Postman 




 We will explore how to use the postman collections to perform load testing using K6

Implementation

Pre-requisites 
  • Node js 
  • npm

Step 1:

Install k6 using the script 

npm install -g postman-to-k6



Step 2:

After installation of  k6 , next step is to export the postman collection 
 
postman-to-k6 collection.json -o demo.js

here collection.json is the  collection name 

This will help to convert the collection to javascript file 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 section of JavaScript code you can define the Duration of test and Users 

Final Script will be like this , here i have given duration for 1 minute and virtual user count is 50

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

import './libs/shim/core.js';
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
export let options = { maxRedirects: 4,
  duration: '1m',
  vus: 50,
   };
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);
      });
    }
  });
}
export function handleSummary(data) {
  return {
    "summary.html": htmlReport(data),
  };
}

Step 3:

Execution and Reporting 


For executing you need to install k6 using  "winget install k6" command if you using windows package manager

Execute the script using the command 


k6 run demo.js


You will get the html report after execution like this 















That's it , Thanks 

Reference : https://k6.io/blog/load-testing-with-postman-collections/

No comments:

Post a Comment