Today we’ll briefly see how to load testing your API using K6 and loading data from a CSV file. K6 is an open-source tool recently bought by Grafana Labs used by many developers to do load testing. In this article, I’ll show you how to install, set up, and run it.
Although K6 has excellent documentation, sometimes it is easier to read a short straight-to-the-point article giving you something ready for a specific purpose instead of reading the complete documentation and look for what you need.
Our goal is to make multiple requests to an API endpoint. However, if the API implements any caching strategy, it doesn’t help you make multiples GET requests with the same parameters. You might have a fast response that will not necessarily correspond to the actual production behavior unless your client is a web browser with a user pressing the F5 button repeatedly.
Therefore, we will use K6 to make several requests to an endpoint and a CSV file with a list of ids instead of using the same id for all requests.
1. Install it
To install K6 in a macOS:
brew install k6
For other operational systems, check this page for more details https://k6.io/docs/getting-started/installation/
2. Set it up
When you execute K6, you provide a javascript file with essentially 5 parts:
Initialization (optional)
Here is the perfect place to load a CSV file with the example data.
Setup (optional)
This exported function is called once at the beginning of the test. The return of this function can be received as an argument in the default function. The main difference between setup and initialization is that inside setup function you can make requests using k6 library.
Default (main) function (required)
This is where all tests happen. This function is called over and over simulating an interaction of a VU, that is a virtual user. A VU represents a user interacting with a system. From the documentation:
“…they’re essentially glorified, parallel while(true) loops”
In other words, if you set on options that you will use 2 VU, K6 will roughly have 2 parallel loops calling the default function.
Teardown (optional)
Called once at the end of the test. This function can receive the return of setup function in a parameter.
Options
There are so many possibilities you can configure here. I strongly recommend that you take a look at the documentationĀ here. Since the purpose of this post is only to show a simple example, we will declare in this session that our test will run using 5 VU, meaning similar to 5 users making calls in parallel. Example:
export const options = {
thresholds: {
http_req_failed: ['rate<0.01'], // http errors should be less than 1%
http_req_duration: ['p(95)<900'], // 95% of requests should be below 200ms
},
vus: 30,
duration: '30s',
}
Simple example:
3. Run it
Supposing your CSV file is called data.csv (the same as in the js file) and has a list of valid Ids:
my_file_header_name
4f1e0335-c0fb-4db5-9dc3-8107e28b098f
871cea34-c923-40bd-bfcb-2527c1d5d83d
82b37713-efac-47ae-9cb5-d94d679f45fa
Folder structure:
.
āāā k6_test
ā āāā data.csv
ā āāā main_csv.js
Running tests:
K6 run main_csv.js
I hope this short article helped you get a quick overview of how to make requests using K6 and random data from CSV files. I tried not to go into too many details because, again, the documentation is extraordinary.