Skip to content
Learni
View all tutorials
Performance & Tests

How to Perform Expert Load Testing with k6 in 2026

Lire en français

Introduction

Load testing is essential to ensure your applications handle real-world traffic without performance degradation. In 2026, tools like k6 let you simulate thousands of virtual users using simple yet powerful JavaScript code. This tutorial guides you through expert scenarios including gradual ramp-up, strict performance thresholds, and custom metrics. You will learn to identify bottlenecks before they affect production users.

Prerequisites

  • Node.js 20+ and k6 installed
  • Solid knowledge of JavaScript and HTTP
  • A target application exposed (REST API or frontend)
  • Docker for distributed testing
  • Access to an isolated test environment

Basic Installation and Configuration

terminal
brew install k6
k6 version
mkdir load-tests && cd load-tests

Install k6 via Homebrew on macOS. Verify the version and create a dedicated folder to organize your load testing scripts.

Basic Load Test Script with Ramp-up

basic-load.js
import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
  stages: [
    { duration: '2m', target: 100 },
    { duration: '5m', target: 500 },
    { duration: '2m', target: 0 },
  ],
  thresholds: {
    http_req_duration: ['p(95)<250'],
    http_req_failed: ['rate<0.01'],
  },
};

export default function () {
  const res = http.get('https://votre-api.com/health');
  check(res, { 'status is 200': (r) => r.status === 200 });
  sleep(1);
}

This script defines a ramp-up to 500 VUs over 9 minutes with strict thresholds. Checks validate responses and sleep simulates realistic user behavior.

Advanced Scenario with Custom Metrics

advanced-metrics.js
import http from 'k6/http';
import { Trend } from 'k6/metrics';

const apiLatency = new Trend('api_latency');

export const options = {
  vus: 200,
  duration: '10m',
};

export default function () {
  const res = http.get('https://votre-api.com/users');
  apiLatency.add(res.timings.duration);
  if (res.status !== 200) {
    console.error(`Erreur: ${res.status}`);
  }
}

Adds a custom Trend metric to precisely track API latency. Useful for detailed reporting and percentile analysis beyond standard thresholds.

Distributed Testing with Docker

Dockerfile
FROM grafana/k6:latest
COPY . /scripts
WORKDIR /scripts
ENTRYPOINT ["k6", "run", "--out", "experimental-prometheus-rw", "advanced-metrics.js"]

Create a Docker image to run k6 in distributed mode. Output to Prometheus enables real-time visualization and metric aggregation across multiple nodes.

CI/CD Pipeline Integration

.github/workflows/load-test.yml
name: Load Test
on: [push]
jobs:
  load-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Run k6
        uses: grafana/k6-action@v0.3.0
        with:
          filename: advanced-metrics.js
          flags: --vus 100 --duration 5m

Integrate load tests into GitHub Actions. The workflow runs automatically on every push and fails if thresholds are not met.

Best Practices

  • Always define strict thresholds (p95, error rate) and fail the build on violation
  • Use dynamic data and environment variables to avoid caching
  • Run tests in an environment identical to production
  • Analyze traces with Prometheus/Grafana instead of raw logs
  • Limit test duration to 10-15 minutes for peak load tests

Common Mistakes to Avoid

  • Forgetting to validate HTTP responses (status 200 only) leads to false positives
  • Using too many VUs without ramp-up causes artificial spikes and timeouts
  • Ignoring network latency metrics and focusing only on server response time
  • Failing to isolate the test environment, which skews results due to other processes

Going Further

Deepen your skills with our advanced training on performance and load testing. Discover our Learni courses.

How to Perform Expert Load Testing with k6 in 2026 | Learni