Parameterize your k6 scripts effectively

Image credit:

Key features of K6

  • Test written in JS : Functional automation tools, cypress adopts javascript due to its ease of scripting. K6 is one of the load test tools which allow us to create tests using javascript. This eliminates the need to learn a new programming language in order to utilise a tool
  • Test Builders: In my opinion It’s one of K6’s commendable features. On the fly, postman collections can be simply converted to k6 scripts and one can utilise recorder to create load test scripts
  • CLI: CLI feature are easy-to-use, flexible and powerful. One can configure the total Virtual users and Iteration using simplified commands
  • Assertions : It is simple and straightforward to configure both hard and soft assertions in the BDD manner.
  • Protocol: Supports majorly used protocols like HTTP/1.1 HTTP/2, GraphQL running over HTTP, WebSockets and gRPC

What K6 is not

  • Native Browsers : By default, k6 does not render web pages in the same way as a browser.Browsers can consume a significant amount of system resources. More load can be run on a single machine by omitting the browser
  • Not a NodeJS : JavaScript isn’t well suited to high performance in general.The tool is written in Go for maximum performance, and it includes a JavaScript runtime for easy test scripting. One can bundle npm modules with webpack or browserify and import them in test

Hello World in K6:

Data parameterization

We know that concept of Parameterization allows us to change hard-coded values in the script with test data. In performance testing, we need to test the application with different sets of values to define the SLA with accuracy. We will see the benefits of parameterization with below code snippet

Generate test data using Faker.js

Faker is an open-source js library that allows you to create your own dataset. Now lets see how to create a load test with parameterized data on faker.js

npm install browserify faker./node_modules/.bin/browserify ./node_modules/faker/ -s faker > faker.js



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store