Claude Skills Guide

Claude Code for Load Testing with K6: Script Generation and Result Analysis

Load testing is critical for building resilient applications, but writing comprehensive K6 test scripts from scratch and making sense of the output can be time-consuming. This guide focuses on using Claude Code as an AI-assisted authoring and analysis tool—covering how to prompt Claude Code to generate K6 scripts, create realistic user scenarios, and interpret test results to pinpoint performance bottlenecks.

Why Combine Claude Code with K6?

K6 is a modern, developer-friendly load testing tool that lets you write tests in JavaScript. Claude Code amplifies K6’s capabilities by:

The combination reduces the time needed to create robust load tests while improving test coverage through AI-assisted scenario generation.

Setting Up Your Environment

Before integrating Claude Code with K6, ensure both tools are installed:

# Install K6
brew install k6

# Verify Claude Code installation
claude --version

Create a project directory for your load tests:

mkdir k6-load-tests && cd k6-load-tests

A common pattern is to keep load tests alongside your other test types using a dedicated tests/load directory:

your-project/
├── src/
├── tests/
│   ├── unit/
│   ├── integration/
│   └── load/          # K6 load tests here
├── k6/                # Alternative location
└── scripts/

This separation keeps load tests organized and easy to run independently from unit and integration test suites.

Creating K6 Test Scripts with Claude Code

One of Claude Code’s most valuable contributions is generating K6 test scripts from natural language descriptions. Here’s how to use this capability effectively.

Example: API Endpoint Testing

When you need to test an API endpoint under load, ask Claude Code to generate a script:

Prompt:

Create a K6 load test script for testing a REST API endpoint that handles user authentication. The script should:
1. POST to /api/auth/login with username and password
2. Extract the JWT token from the response
3. Use the token for authenticated GET requests to /api/users
4. Test with virtual users ramping from 10 to 100 over 2 minutes
5. Include custom metrics for response time and error rate

Claude Code will generate a comprehensive script like this:

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

const errorRate = new Rate('errors');

export const options = {
  stages: [
    { duration: '1m', target: 10 },
    { duration: '2m', target: 100 },
    { duration: '1m', target: 0 },
  ],
  thresholds: {
    http_req_duration: ['p(95)<500'],
    errors: ['rate<0.1'],
  },
};

const BASE_URL = __ENV.BASE_URL || 'https://api.example.com';

export default function () {
  const loginPayload = JSON.stringify({
    username: `user${Math.floor(Math.random() * 1000)}`,
    password: 'testpass123',
  });

  const loginParams = {
    headers: {
      'Content-Type': 'application/json',
    },
  };

  const loginRes = http.post(`${BASE_URL}/api/auth/login`, loginPayload, loginParams);

  const loginSuccess = check(loginRes, {
    'login status is 200': (r) => r.status === 200,
    'response has token': (r) => r.json('token') !== undefined,
  });

  errorRate.add(!loginSuccess);

  if (!loginSuccess) {
    sleep(1);
    return;
  }

  const token = loginRes.json('token');
  
  const userParams = {
    headers: {
      'Authorization': `Bearer ${token}`,
    },
  };

  const userRes = http.get(`${BASE_URL}/api/users`, userParams);

  const userSuccess = check(userRes, {
    'users status is 200': (r) => r.status === 200,
    'response has users array': (r) => Array.isArray(r.json()),
  });

  errorRate.add(!userSuccess);

  sleep(1);
}

Advanced Scenario: E-Commerce User Journey

For more complex scenarios, Claude Code can generate multi-step user journeys:

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

export const options = {
  stages: [
    { duration: '30s', target: 20 },
    { duration: '1m', target: 50 },
    { duration: '30s', target: 0 },
  ],
};

const BASE_URL = __ENV.BASE_URL || 'https://shop.example.com';

function getAuthToken() {
  const loginRes = http.post(`${BASE_URL}/api/login`, 
    JSON.stringify({ email: 'test@example.com', password: 'password' }),
    { headers: { 'Content-Type': 'application/json' } }
  );
  return loginRes.json('token');
}

export default function () {
  const token = getAuthToken();
  const headers = { 'Authorization': `Bearer ${token}` };

  // Browse products
  const productsRes = http.get(`${BASE_URL}/api/products?page=1`, { headers });
  check(productsRes, { 'products loaded': (r) => r.status === 200 });
  sleep(1);

  // View product details
  const productId = '12345';
  const detailRes = http.get(`${BASE_URL}/api/products/${productId}`, { headers });
  check(detailRes, { 'product detail loaded': (r) => r.status === 200 });
  sleep(1);

  // Add to cart
  const cartRes = http.post(`${BASE_URL}/api/cart`,
    JSON.stringify({ productId, quantity: 1 }),
    { headers: { ...headers, 'Content-Type': 'application/json' } }
  );
  check(cartRes, { 'added to cart': (r) => r.status === 201 });
  sleep(1);

  // Checkout
  const checkoutRes = http.post(`${BASE_URL}/api/checkout`,
    JSON.stringify({ paymentMethod: 'credit_card' }),
    { headers: { ...headers, 'Content-Type': 'application/json' } }
  );
  check(checkoutRes, { 'checkout complete': (r) => r.status === 200 });
  
  sleep(2);
}

Running Tests and Interpreting Results

Execute your K6 tests using the CLI:

k6 run script.js

For CI/CD integration with environment variables:

k6 run --env BASE_URL=https://staging.example.com script.js

For repeatable runs across environments, create a wrapper script that handles environment setup and output routing:

#!/bin/bash
# scripts/run-load-test.sh

export K6_CLOUD_TOKEN="${K6_CLOUD_TOKEN}"
export ENVIRONMENT="${1:-staging}"

echo "Running load tests against $ENVIRONMENT"

k6 run \
  --out cloud \
  --env BASE_URL="https://$ENVIRONMENT.example.com" \
  tests/load/api-tests.js

This script accepts an environment parameter, making it easy to test against staging, production-mirror, or local environments. The --out cloud option sends results to k6.io for visualization and historical tracking.

Analyzing Results with Claude Code

After running tests, feed the results to Claude Code for analysis:

Prompt:

Analyze these K6 test results and identify performance bottlenecks:

 http_req_duration..................: avg=245ms min=120ms med=230ms max=890ms p(95)=450ms
 http_req_failed....................: 2.34%
 checks.............................: 97.66% ```

The API endpoint is slower than our 200ms p(95) target. What’s causing this?


Claude Code can help identify:
- Which endpoints are slowest
- Whether failures correlate with specific load levels
- Suggestions for optimization

## Automating with Claude Code Skills

Create a custom Claude Code skill for consistent K6 workflows. Add to your skill configuration:

```yaml
name: k6-load-test
description: Run K6 load tests and analyze results

Automating Performance Gates in CI/CD

For teams that need strict performance requirements, integrate K6 into your CI/CD pipeline and fail builds when thresholds are exceeded. This prevents performance regressions from reaching production:

# Example GitHub Actions step
- name: Run Load Tests
  run: |
    k6 run tests/load/api-tests.js \
      --threshold 'http_req_duration=p(95)<1000' \
      --fail-on-threshold

Start with lenient thresholds and tighten them as you establish baseline performance. Overly aggressive thresholds can cause false positives during CI congestion or temporary external service issues.

Best Practices

  1. Start small: Begin with 10-50 virtual users before scaling up
  2. Use realistic data: Generate test data that mirrors production patterns
  3. Monitor external dependencies: Track API response times separately
  4. Set clear thresholds: Define acceptable p(95) latency and error rates
  5. Automate in CI: Run load tests on every major release

Conclusion

Integrating Claude Code with K6 transforms load testing from a manual, time-intensive process into an AI-assisted workflow. Use Claude Code to generate scripts rapidly, analyze results intelligently, and maintain comprehensive test coverage. Start with simple API tests and gradually build complex user scenarios as your confidence grows.

Built by theluckystrike — More at zovo.one