Run load test with K6 and Postman

Run load test with K6 and Postman

What is K6?

Grafana k6 is an open-source load testing tool that makes performance testing easy and productive for engineering teams. Using k6, you can test the reliability and performance of your systems and catch performance regressions and problems earlier.

We can write tests directly using K6 and run them. But in this article, I will explain how to run k6 tests with the existing Postman collection. The process is straightforward to follow. Export your current Postman collection, convert them to k6 tests, and run them.

Steps to do:

  • Install k6
  • Create requests in Postman and export them
  • Convert Postman requests to k6 tests
  • Run k6 tests

Install k6

Following the link below to install k6:

https://k6.io/docs/get-started/installation/

Run the following command to install postman-to-k6:

npm install -g postman-to-k6

Create requests in Postman and export them

Create all the requests you want to run a load test with Postman and put them into a collection. And then export the collection and save it into the project directory.

If you use the environment variables in Postman, export it similarly.
After exporting, we will have two files (postman-requests.json for the requests and postman-env.json for the environment variables) in the project folder.

Convert Postman requests to k6 tests

Run the following command to convert the Postman collection to k6 tests:

postman-to-k6 postman-requests.json -e postman-env.json --separate -o k6-script.js

If you do not have the environment file, run the following command:

postman-to-k6 postman-requests.json --separate -o k6-script.js

After running the command to convert to k6 tests, the result file k6-script.js will be generated like below:

Run k6 tests

To run tests, use the following command:

k6 run k6-script.js
After running the test, the test results will be printed on the console. We can see statistics such as the total number of requests, the number of requests per second, the number of failed requests, etc.
We can adjust the options variable in the script to decrease/increase the number of concurrent users, the test duration, and many other conditions.
For example, to run tests with 100 concurrent users in 30 seconds.
export let options = {
  scenarios: {
    contacts: { 
       executor: "constant-vus", 
       vus: 100, 
       duration: "30s" 
    }
  }
};

For example, to run tests with 500 concurrent users in 60 seconds.
export let options = {
  scenarios: {
    contacts: { 
       executor: "constant-vus", 
       vus: 500, 
       duration: "60s" 
    }
  }
};

For example, to run tests with 10 concurrent users, each user executes 20 iterations in a maximum duration of 30 seconds.

export let options = {
  scenarios: {
    contacts: { 
       executor: "per-vu-iterations", 
       vus: 10, 
       iterations: 20, 
       maxDuration: "30s" 
    }
  }
};

In addition, there are many parameters and scenarios we can use to adjust to our needs. Please refer to the links below:
https://k6.io/docs/javascript-api/k6-http/params/
https://k6.io/docs/using-k6/scenarios/

All the requests under the default function() will be executed. If you want to run tests for a specific request, you can comment out the code of other requests. See the code snippet below:

Compare to Postman collection runner

We can also use the Postman collection runner to run load tests directly. With this function, there is no need to install extra libraries or convert code. We can create requests in Postman and run the tests. We can also change the parameters, such as Iterations/Delay, to adjust to our needs. The only difference is that we can not run concurrent tests, only consecutive ones. The test results are also less detailed than when running with k6. Otherwise, if you do not have the demand to run concurrent tests or see the detailed statistics, Postman is a perfect option.

References