K6

From G1Joshi/Agent-Skills · View interactive version

K6 is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert k6 load and performance testing assistance covering virtual users, thresholds, metrics, and scenario scripting. Use when benchmarking APIs, performing stress testing, or integrating load tests into CI/CD.

How to use this skill

Skill file

k6

k6 is a developer-centric, open-source load testing tool suitable for testing APIs, microservices, and websites. It is written in Go but you script tests in JavaScript.

When to Use

Quick Start

import http from "k6/http";
import { sleep, check } from "k6";

export const options = {
  vus: 10,
  duration: "30s",
};

export default function () {
  const res = http.get("http://test.k6.io");
  check(res, {
    "status was 200": (r) => r.status == 200,
  });
  sleep(1);
}

Run with k6 run script.js.

Core Concepts

Go-Powered Virtual Users (VUs) with JS Runtimes

Tests are written in JavaScript, but executed by a multi-threaded Go engine with zero NodeJS overhead:

// load-test.js
import http from "k6/http";
import { check, sleep } from "k6";

export const options = {
  stages: [
    { duration: "30s", target: 50 }, // Ramp up to 50 users
    { duration: "1m", target: 50 }, // Stay at 50 users
    { duration: "20s", target: 0 }, // Ramp down
  ],
  thresholds: {
    http_req_duration: ["p(95)<300"], // 95% of requests must complete below 300ms
    http_req_failed: ["rate<0.01"], // Error rate must be less than 1%
  },
};

export default function () {
  const res = http.get("https://api.staging.example.com/v1/items");
  check(res, {
    "status is 200": (r) => r.status === 200,
    "transaction time OK": (r) => r.timings.duration < 300,
  });
  sleep(1);
}

Custom Metrics (Counters, Gauges, Trends)

Tracks domain-specific operational metrics alongside standard HTTP timings:

import { Trend, Counter } from "k6/metrics";

const checkoutLatency = new Trend("checkout_duration");
const completedOrders = new Counter("successful_checkouts");

export default function () {
  const start = Date.now();
  const res = http.post(
    "https://api.example.com/checkout",
    JSON.stringify({ cartId: 415 }),
    {
      headers: { "Content-Type": "application/json" },
    },
  );

  if (res.status === 201) {
    completedOrders.add(1);
    checkoutLatency.add(Date.now() - start);
  }
}

Tagging & Scenarios

Isolates different user behaviors (e.g. 80% readers, 20% writers) in a single test run:

export const options = {
  scenarios: {
    readers: {
      executor: "constant-vus",
      vus: 100,
      duration: "5m",
      exec: "browseCatalog",
    },
  },
};

Common Patterns

Staged Load Test with Strict SLA Thresholds

Problem: Performance regressions slip into production when build pipelines lack automated latency gatechecks.

Solution:

Define ramp-up stages and SLO thresholds in k6 script options:

import http from "k6/http";
import { check, sleep } from "k6";

export const options = {
  stages: [
    { duration: "30s", target: 50 },
    { duration: "1m", target: 50 },
    { duration: "20s", target: 0 },
  ],
  thresholds: {
    http_req_duration: ["p(95)<300"],
    http_req_failed: ["rate<0.01"],
  },
};

export default function () {
  const res = http.get("https://test-api.k6.io/public/crocodiles/");
  check(res, { "status is 200": (r) => r.status === 200 });
  sleep(1);
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------------------ | :---------------------------------------------------------------- | :------------------------------------------------------------------------ |

| dial tcp: lookup ...: no such host | DNS resolution failure or target URL incorrect. | Verify endpoint connectivity and ensure VPN or internal DNS is reachable. |

| Threshold failed: p(95)<300 [current: 450ms] | Target service latency exceeded predefined performance threshold. | Profile database query latency, server CPU/memory, and caching layers. |

| warn: Request Failed: context deadline exceeded | Target server timing out under load. | Increase request timeout in k6 or scale upstream application workers. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.