
关于
全面的 k6 负载测试技能,涵盖 API、浏览器和可扩展性测试。编写真实负载场景、分析结果并集成 CI/CD。
name: k6-load-testing description: "全面的 k6 负载测试技能,用于 API、浏览器和可扩展性测试。编写真实的负载场景、分析结果并集成到 CI/CD。" category: testing risk: safe source: community date_added: "2026-03-13" author: Kairo Official tags: [k6, load-testing, performance, api-testing, ci-cd] tools: [claude, cursor, gemini]
k6 负载测试
概述
k6 是一个现代的、以开发者为中心的负载测试工具,帮助你编写和执行 HTTP API、WebSocket 端点和浏览器场景的性能测试。本技能提供编写真实负载测试、配置测试场景(冒烟测试、负载测试、压力测试、尖峰测试、浸泡测试)、分析结果以及集成 CI/CD 管道的全面指导。
当你需要验证系统性能、识别瓶颈、确保 SLA 合规或在部署前捕获性能回归时使用此技能。
何时使用此技能
- 需要对 HTTP API、WebSocket 端点或浏览器场景进行负载测试时
- 在 CI/CD 中设置性能回归测试时
- 分析系统在各种负载条件下的行为时
- 比较代码变更之间的性能差异时
- 验证 SLA 要求和性能预算时
k6 基础
安装
# macOS
brew install k6
# Windows
choco install k6
# Linux
sudo gpg -k
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
sudo apt-get update
sudo apt-get install k6
快速开始
// simple-test.js
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
vus: 10,
duration: '30s',
};
export default function () {
const res = http.get('https://httpbin.test.k6.io/get');
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 500ms': (r) => r.timings.duration < 500,
});
sleep(1);
}
运行命令:k6 run simple-test.js
测试配置
常用选项
export const options = {
// 虚拟用户(并发用户数)
vus: 100,
// 测试持续时间
duration: '5m',
// 或使用阶段进行递增/递减
stages: [
{ duration: '30s', target: 20 }, // 递增
{ duration: '1m', target: 100 }, // 保持 100
{ duration: '30s', target: 0 }, // 递减
],
// 阈值(SLA)
thresholds: {
http_req_duration: ['p(95)<500'], // 95% 请求 < 500ms
http_req_failed: ['rate<0.01'], // 错误率 < 1%
},
// 负载区域(分布式测试)
ext: {
loadimpact: {
name: 'My Load Test',
distribution: {
'amazon:us:ashburn': { weight: 50 },
'amazon:eu:Dublin': { weight: 50 },
},
},
},
};
测试类型
| 类型 | 使用场景 | 配置 | |------|----------|---------------| | 冒烟测试 | 验证基本功能 | 低 VU(1-5),短时间 | | 负载测试 | 正常预期负载 | 基于流量的目标 VU | | 压力测试 | 找到断裂点 | 超出容量递增 | | 尖峰测试 | 突发流量高峰 | 快速增加/减少 | | 浸泡测试 | 长期稳定性 | 延长持续时间 |
HTTP 测试
基本请求
import http from 'k6/http';
import { check, sleep } from 'k6';
export default function () {
// GET 请求
const getRes = http.get('https://api.example.com/users');
check(getRes, {
'GET succeeded': (r) => r.status === 200,
'has users': (r) => r.json('data.length') > 0,
});
// POST 请求(JSON 请求体)
const postRes = http.post('https://api.example.com/users',
JSON.stringify({ name: 'Test User', email: 'test@example.com' }),
{
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + __ENV.API_TOKEN,
},
}
);
check(postRes, {
'POST succeeded': (r) => r.status === 201,
'user created': (r) => r.json('id') !== undefined,
});
sleep(1);
}
请求链
import http from 'k6/http';
import { check } from 'k6';
export default function () {
// 登录并提取令牌
const loginRes = http.post('https://api.example.com/login',
JSON.stringify({ email: 'test@example.com', password: 'password123' })
);
const token = loginRes.json('access_token');
// 在后续请求中使用令牌
const headers = {
'Authorization': \`Bearer \${token}\`,
'Content-Type': 'application/json',
};
const profileRes = http.get('https://api.example.com/profile', {
headers: headers,
});
check(profileRes, {
'profile loaded': (r) => r.status === 200,
});
}
参数化测试
import http from 'k6/http';
import { SharedArray } from 'k6/data';
const users = new SharedArray('users', function () {
return JSON.parse(open('./users.json'));
});
export default function () {
const user = users[__VU % users.length];
http.post('https://api.example.com/login',
JSON.stringify({ email: user.email, password: user.password })
);
}
兼容工具
Claude CodeCursor
标签
测试

