API performance testing using K6

Panduka Wijekoon
5 min readJun 24, 2024

--

In this article, I’m discussing how to write a performance script for testing APIs using K6. K6 can be scripted using JavaScript and TypeScript, but in this project, I’m using JavaScript.

Step 1 -
Downloading K6.

Use this link to download the K6 as per your OS https://k6.io/docs/get-started/installation/.

Since I have a Windows OS, I’m using K6 official installer. You can choose other options as your wish.

Step 2 -
Installing K6.

Click on the downloaded file and go through the setup wizard by clicking ‘Next’ to complete the installation.

To ensure the K6 installation, open the command prompt and execute the ‘k6’ command.

Step 3 —
Create a repository on GitHub for the K6 project.

Step 4 -
Clone the repository to a local location

Step 5 -
Create the required Get/Post API request.

I’m using https://designer.mocky.io/ website to create the required API request. We can use this API as a Get request or a Post request.

After completing the API creation, you can copy the URL from here,

Step 6 -
Create a JavaScript file named “get_api.js” and write the code below.

import http from 'k6/http'
import { check } from 'k6'

export let options = {
vus: 10,
duration: '10s',
thresholds: {
'checks': ['rate>0.95']
}
}

export default function () {

let url = 'https://run.mocky.io/v3/47565135-e942-4c4d-9818-6e4812045f39'

let headerParams = {
headers: {
'Content-Type': 'application/json'
}
}

const response = http.get(url, headerParams)

check(response, {
'is status 200': (r) => r.status === 200,
'is message success': (res) => JSON.parse(res.body).message == "hello world"
})

let body = JSON.parse(response.body)
console.log('body is ', JSON.stringify(body))
console.log('message : ', body.message)
console.log('code : ', body.code)

}

In this script, I used the Get API URL created in the previous step as the calling API.
In that API request, I have mentioned that the content type of the request as ‘application/json’.
By using get function of the http, I’m calling the API and storing the API response in response variable.

I’m using checks to validate that the status code is 200 and the value of the message field in API response is equal to “hello world.

In the options section, we can specify the virtual users (vus) and the execution duration that we plan implement. For this test, I’m using 10 virtual users and the test runs for 10s.

I’m using thresholds to determine whether the test passes or fails based on the success rate of the checks. Here, the pass rate of the checks should be above 95% for the test to pass. The thresholds works as assertions in K6.

Finally I printed the values of the response body. To do that, first we have to convert the API response into JSON object since the response is received as a String.

Step 7-
Run the “get_api.js” script

In your terminal, execute the following K6 command to run the script.

k6 run get_api.js 

After the test execution is completed, all the matrics are listed in the terminal, including max VUs, duration, iterations, iteration durations, checks status, etc.

Step 8-
Create a JavaScript file named “post_api.js” and write the code below.

import http from 'k6/http'
import { check } from 'k6'

export let options = {
vus: 10,
duration: '10s',
thresholds: {
'checks': ['rate>0.95']
}
}

export default function () {

let url = 'https://run.mocky.io/v3/47565135-e942-4c4d-9818-6e4812045f39'

let headerParams = {
headers: {
'Content-Type': 'application/json'
}
}

let payload = JSON.stringify({
email: "test@gmail.com",
password: "1234@#$"
})

const response = http.post(url, payload, headerParams)

check(response, {
'is status 200': (r) => r.status === 200,
'is message success': (res) => JSON.parse(res.body).message == "hello world"
})

let body = JSON.parse(response.body)
console.log('body is ', JSON.stringify(body))
console.log('message : ', body.message)
console.log('code : ', body.code)

}

This Post API script is almost similar to the Get API script that we discussed previously. The only difference is that, I have added a payload for the Post request.

Here, I have created a JSON object containing email and password fields. Then, I have converted the JSON object into a String the using stringify function. If we don’t convert the payload object into a String, the Post request will not work.

Step 9 -
Run the “post_api.js” script

In your terminal, execute the following K6 command to run the script.

k6 run post_api.js

Similar to the get_api.js script, after the post_api.js test execution is completed, all the metrics are listed in the terminal including, max VUs, duration, iterations, iteration durations, checks status, etc.

Step 10 -
Commit the changes to the GitHub repository

--

--

Panduka Wijekoon
Panduka Wijekoon

Written by Panduka Wijekoon

Senior QA Engineer | Cypress | CircleCI | Jenkins | Selenium | Appium

No responses yet