Skip to content

alshakibeelahi/k6-load-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

📚 Full Guide: Install & Use k6 for REST API Load Testing


1. What is k6?

k6 is an open-source load testing tool built in Go, designed for testing the performance of APIs, microservices, and websites.


Installation

Windows (using winget)

Open PowerShell or Command Prompt and run:

winget install k6

macOS (using Homebrew)

brew install k6

Linux (using apt for Debian/Ubuntu)

sudo 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 k6

Verify Installation

Run:

k6 version

You should see the installed k6 version printed.


Configuration

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 });
}

Running the Test

Run the test with:

k6 run course-load-test.js

Useful k6 Commands

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

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published