Parameterize your k6 scripts effectively

Kowcik R
3 min readFeb 16, 2022

In this article, we will see how to parameterize http requests in k6 performance test tool. Before diving in, lets familiarise with the basics of K6

Image credit: https://k6.io/

K6 is now Grafana k6, yet another performance test tool on the market that help the teams with performance testing. It is available in both open source and cloud version. The tool itself is written in GO for maximum performance, and it includes a javascript runtime for creating load test scripts

Key features of K6

  • Test written in JS : Functional automation tools webdrive.io, 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

Note: In the latest release, an extension to k6 adds support for browser automation via the Chrome Dev tools Protocol (CDP).

  • 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:

Now that we’ve covered the fundamentals, we’ll move on to the more interesting topics of parameterisation .If you want to learn more about K6 and its concepts apart from this article, watch the video.

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

When running the above script multiple times or multiple iteration, the http request will return a non-2xx series of response codes due to a duplicate user exist in the system. To get around this, we must pass unique data for each iteration.

In k6, With the help of sharedArray concept, we can easily create parameterized test scripts with json data

As seen, this way of creating test script solves the issue of hard-coded values. But, in my opinion, will still result in new Issue.

Cannot guarantee unique data for each iteration across VU’s

Prior to v0.34.0, there is no direct way to achieve the above mentioned problem. K6 has introduced iterationInTest property in v0.34.0 release where we can retrieve unique rows from data set.

Creating test data for these scenarios is time consuming if we need to run for a larger number of iteration.At the same time, a significant amount of time must be spent deleting resources from the system. To overcome this, we can utilise the faker library.

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

Note: K6 is not a node tool, as stated earlier in the article; it only contains a JS runtime for writing tests.With this knowledge, we may understand that the faker library cannot be imported directly through npm. We need to use bundles like browserify or webpack

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

The above command will generate faker.js and place it inside node_modules

As testers, we need to decide when to use faker.js and when to use test data to parameterize http requests.

— Kowcik R

--

--