agentleFS
MathiasPaulenkoMathiasPaulenko/ai-toolkit/skills/k6-load-testing/SKILL.md

k6-load-testing

Skill2 starsChanged 4 months agoMITView on GitHub

Modern load testing with k6: JavaScript scripting, Grafana dashboards, cloud execution, thresholds, extensions, and CI/CD integration.

  • Installs packages

The file

Files
SKILL.md

k6 Load Testing

Invoke when user asks about k6, modern load testing, or JavaScript-based performance scripting.

Core Principles

  • JavaScript syntax: Familiar to frontend/backend teams.
  • Built-in metrics: HTTP timing, custom trends, checks, thresholds.
  • Grafana ecosystem: Native dashboards, cloud SaaS, InfluxDB output.

Script Structure

import http from 'k6/http';
import { check, sleep, group } from 'k6';
import { Rate, Trend } from 'k6/metrics';

// Custom metrics
const failRate = new Rate('failed_requests');
const responseTime = new Trend('response_time');

export const options = {
  stages: [
    { duration: '5m', target: 100 },   // Ramp-up
    { duration: '10m', target: 100 }, // Steady
    { duration: '5m', target: 200 },  // Spike
    { duration: '5m', target: 0 },    // Ramp-down
  ],
  thresholds: {
    http_req_duration: ['p(95)<500'],
    http_req_failed: ['rate<0.01'],
    failed_requests: ['rate<0.05'],
  },
};

export default function () {
  group('Login', () => {
    const res = http.post(`${__ENV.BASE_URL}/auth/login`, JSON.stringify({
      email: `user${__VU}@test.com`,
      password: 'password123',
    }), { headers: { 'Content-Type': 'application/json' } });

    const success = check(res, {
      'status is 200': (r) => r.status === 200,
      'token present': (r) => r.json('access_token') !== undefined,
    });

    failRate.add(!success);
    responseTime.add(res.timings.duration);
  });

  sleep(1);
}

Execution Modes

# Local
cd k6 run script.js

# Cloud (Grafana k6 Cloud)
k6 cloud run script.js

# Docker
docker run -i grafana/k6 run --vus 10 --duration 30s - <script.js

# Output to InfluxDB
k6 run --out influxdb=http://localhost:8086/k6 script.js

Scenarios (Advanced Load Profiles)

export const options = {
  scenarios: {
    constant_load: {
      executor: 'constant-vus',
      vus: 50,
      duration: '10m',
    },
    ramping_load: {
      executor: 'ramping-vus',
      startVUs: 0,
      stages: [
        { duration: '5m', target: 100 },
        { duration: '10m', target: 100 },
        { duration: '5m', target: 0 },
      ],
    },
    per_vu_iterations: {
      executor: 'per-vu-iterations',
      vus: 100,
      iterations: 10,
      maxDuration: '1h',
    },
  },
};

Checks vs Thresholds

FeaturePurposeWhen It Fails
CheckValidate response contentLogged, does not abort
ThresholdEnforce SLAAborts test if breached
// Check (validation)
check(res, {
  'status is 200': (r) => r.status === 200,
});

// Threshold (SLA enforcement)
thresholds: {
  http_req_duration: ['p(95)<500'], // Fail test if > 500ms
}

Reusable Modules

// lib/auth.js
import http from 'k6/http';

export function login(email, password) {
  const res = http.post(`${__ENV.BASE_URL}/auth/login`, JSON.stringify({
    email, password,
  }), { headers: { 'Content-Type': 'application/json' } });
  return res.json('access_token');
}
// main script
import { login } from './lib/auth.js';

export default function () {
  const token = login(`user${__VU}@test.com`, 'password123');
  http.get(`${__ENV.BASE_URL}/orders`, {
    headers: { Authorization: `Bearer ${token}` },
  });
}

CI/CD Integration

# .github/workflows/load-test.yml
name: Load Test
on:
  schedule:
    - cron: '0 2 * * 1'  # Weekly Monday 2am

jobs:
  k6:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: grafana/k6-action@v0.3.1
        with:
          filename: tests/load.js
          flags: --out json=results.json
      - uses: actions/upload-artifact@v4
        with:
          name: k6-results
          path: results.json

Extensions (xk6)

# Build k6 with extensions
go install go.k6.io/xk6/cmd/xk6@latest
xk6 build --with github.com/grafana/xk6-sql

# SQL extension usage
import sql from 'k6/x/sql';
const db = sql.open('postgres', 'postgres://user:pass@localhost/db');

Anti-Patterns

Anti-PatternFix
Hardcoded URLsUse __ENV.BASE_URL
No sleep between requestsAdd sleep() to simulate think time
Ignoring failed checksSet thresholds to enforce SLAs
Single large scriptSplit into modules: auth, cart, checkout
Testing against prod directlyUse dedicated performance environment

Quick Reference

MetricDescription
http_req_durationTotal request time
http_req_waitingTime to first byte
http_req_connectingTCP connection time
http_req_tls_handshakingTLS handshake time
vusCurrent virtual users
iterationsTotal iterations

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.