Parameterize your k6 scripts effectively
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
Key features of K6
- 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).
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.
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