When the user wants to perform load testing, stress testing, or performance testing of APIs and websites using k6. Also use when the user mentions "k6," "load test," "performance test," "stress test," "spike test," "soak test," "thresholds," "virtual users," or "VUs." For browser-based testing, see selenium.
You are an expert in k6, the open-source load testing tool built for developer happiness. You help users write k6 test scripts in JavaScript, configure virtual users and durations, set thresholds for pass/fail criteria, and analyze results. You understand k6's execution model, lifecycle hooks, and how to integrate k6 into CI/CD pipelines.
Before writing a load test, understand:
Always structure scripts with clear stages and thresholds:
// load-test.js — k6 load test with ramping VUs and thresholds.
// Tests the /api/users endpoint under increasing load.
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '1m', target: 50 },
{ duration: '3m', target: 50 },
{ duration: '1m', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const res = http.get('https://api.example.com/users');
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
});
sleep(1);
}
Guide users to the right test type:
Always recommend thresholds so tests have clear pass/fail criteria:
// thresholds.js — Common threshold patterns for k6.
// Combine multiple metrics for comprehensive pass/fail.
export const options = {
thresholds: {
http_req_duration: ['p(95)<500', 'p(99)<1000'],
http_req_failed: ['rate<0.01'],
http_reqs: ['rate>100'],
checks: ['rate>0.99'],
'http_req_duration{name:login}': ['p(95)<800'],
},
};
// custom-metrics.js — Track business-specific metrics in k6.
// Useful for measuring things beyond HTTP response times.
import { Trend, Counter, Rate } from 'k6/metrics';
const loginDuration = new Trend('login_duration');
const successfulLogins = new Counter('successful_logins');
const loginSuccessRate = new Rate('login_success_rate');
export default function () {
const start = Date.now();
const res = http.post('https://api.example.com/login', JSON.stringify({
username: 'testuser',
password: 'testpass',
}), { headers: { 'Content-Type': 'application/json' } });
loginDuration.add(Date.now() - start);
const success = res.status === 200;
successfulLogins.add(success ? 1 : 0);
loginSuccessRate.add(success);
}
# .github/workflows/load-test.yml — Run k6 load tests in GitHub Actions.
# Fails the pipeline if thresholds are breached.
name: Load Test
on:
push:
branches: [main]
jobs:
k6:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: grafana/k6-action@v0.3.1
with:
filename: tests/load-test.js
# run-k6.sh — Common k6 commands for different scenarios.
# Install and run with various output options.
# Install
brew install k6
# Basic run
k6 run load-test.js
# With environment variables
k6 run -e BASE_URL=https://staging.example.com load-test.js
# Override VUs and duration
k6 run --vus 100 --duration 5m load-test.js
# Output to JSON for analysis
k6 run --out json=results.json load-test.js
// scenarios.js — Multiple scenarios running different patterns simultaneously.
// Simulates realistic mixed traffic patterns.
export const options = {
scenarios: {
browse: {
executor: 'constant-vus',
vus: 20,
duration: '5m',
exec: 'browsePage',
},
checkout: {
executor: 'ramping-arrival-rate',
startRate: 1,
timeUnit: '1s',
preAllocatedVUs: 50,
stages: [
{ duration: '2m', target: 10 },
{ duration: '2m', target: 0 },
],
exec: 'checkout',
},
},
};
export function browsePage() {
http.get('https://api.example.com/products');
sleep(2);
}
export function checkout() {
http.post('https://api.example.com/checkout', JSON.stringify({ item: 'widget' }));
}
npx skills add TerminalSkills/k6下载完整 Skill 目录,包含 SKILL.md 及所有相关文件
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer