k6 is an open-source load testing tool built in Go, designed for testing the performance of APIs, microservices, and websites.
Open PowerShell or Command Prompt and run:
winget install k6brew install k6sudo apt update
sudo apt install -y gnupg software-properties-common
wget -q -O - https://dl.k6.io/key.gpg | sudo apt-key add -
echo "deb https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
sudo apt update
sudo apt install k6Run:
k6 versionYou should see the installed k6 version printed.
Create a test script file, for example course-load-test.js:
import http from 'k6/http';
import { check } from 'k6';
export const options = {
vus: 100,
duration: '30s',
thresholds: {
http_req_duration: ['p(95)<1000'],
http_req_failed: ['rate<0.01'],
},
};
const BASE_URL = 'http://localhost:8080/api/v1';
export default function () {
let res = http.get(`${BASE_URL}/courses`);
check(res, { 'GET /courses - status 200': (r) => r.status === 200 });
res = http.get(`${BASE_URL}/courses/1`);
check(res, { 'GET /courses/1 - status 200': (r) => r.status === 200 });
res = http.post(`${BASE_URL}/courses`, JSON.stringify({
name: `Course ${Math.random().toString(36).substring(2, 6)}`,
duration: Math.floor(Math.random() * 10) + 1,
}), { headers: { 'Content-Type': 'application/json' } });
check(res, { 'POST /courses - status 200': (r) => r.status === 200 });
}Run the test with:
k6 run course-load-test.js-
Run a script:
k6 run script.js
-
Show k6 help:
k6 --help
-
Run test with 50 virtual users for 1 minute:
k6 run --vus 50 --duration 1m script.js
-
Output results to JSON:
k6 run --out json=output.json script.js
That’s it! You now have k6 installed and can run your load tests.
If you want me to include extra tips or examples, just ask!