关于 testing-perf
面向测试工程师和开发者的性能与负载测试实践指南,涵盖 k6、Locust 和 pytest 执行优化。提供测试类型、并行执行、数据库隔离及测试清单等模式。
主要功能
- 01k6 阈值、阶段与自定义指标
- 02Locust 负载测试模式
- 03负载、压力、突增和浸泡测试分类
- 04pytest-xdist 并行执行与标记
- 05并行测试的工作进程数据库隔离
- 06性能与 pytest 检查清单
适用场景
- 01为 API 配置分阶段负载测试
- 02根据流量特征选择负载测试类型
- 03用 pytest-xdist 加速测试套件
- 04隔离并行测试的数据库工作进程
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ yonatangross/ orchestkit --skill testing-perf安装后在 Claude Code 中输入/testing-perf确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
test-cases.jsonchecklists/performance-checklist.mdpytest-production-checklist.mdreferences/custom-plugins.mdk6-patterns.mdxdist-parallel.mdrules/_sections.mdexecution.mdperf-k6.mdperf-locust.mdperf-types.mdpytest-execution.mdpytest-plugins.mdscripts/test-case-template.md详细介绍
性能与负载测试模式
专注于性能测试、负载测试和 pytest 执行优化。涵盖 k6、Locust、pytest-xdist 并行执行、自定义插件和测试类型分类。
快速参考
| 领域 | 文件 | 用途 |
|---|---|---|
| k6 负载测试 | rules/perf-k6.md | 阈值、阶段、自定义指标、CI 集成 |
| Locust 测试 | rules/perf-locust.md | Python 负载测试、任务权重、身份验证流程 |
| 测试类型 | rules/perf-types.md | 负载、压力、突增、浸泡测试模式 |
| 执行 | rules/execution.md | 覆盖率报告、并行执行、失败分析 |
| Pytest 标记 | rules/pytest-execution.md | 自定义标记、xdist 并行、工作进程隔离 |
| Pytest 插件 | rules/pytest-plugins.md | 工厂 fixture、插件钩子、反模式 |
| k6 模式 | references/k6-patterns.md | 分阶段递增、经过身份验证的请求、测试类型 |
| xdist 并行 | references/xdist-parallel.md | 分发模式、工作进程隔离、CI 配置 |
| 自定义插件 | references/custom-plugins.md | conftest 插件、可安装插件、钩子参考 |
| 性能检查清单 | checklists/performance-checklist.md | 规划、设置、指标、负载模式、分析 |
| Pytest 检查清单 | checklists/pytest-production-checklist.md | 配置、标记、并行、fixture、CI/CD |
| 测试模板 | scripts/test-case-template.md | 完整的测试用例文档模板 |
k6 快速入门
设置带阈值和分阶段递增的负载测试:
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 }, // Ramp up
{ duration: '1m', target: 20 }, // Steady state
{ duration: '30s', target: 0 }, // Ramp down
],
thresholds: {
http_req_duration: ['p(95)<500'], // 95th percentile under 500ms
http_req_failed: ['rate<0.01'], // Less than 1% error rate
},
};
export default function () {
const res = http.get('http://localhost:8000/api/health');
check(res, {
'status is 200': (r) => r.status === 200,
'response time < 200ms': (r) => r.timings.duration < 200,
});
sleep(1);
}
运行:k6 run --out json=results.json tests/load/api.js
k6 v1.0+(2025 年 5 月)——变更内容
- 原生 TypeScript:
k6 run tests/load/api.ts—— 无需编译步骤。 - 自动配置扩展:
k6 run会自动拉取所需扩展;对于大多数工作流,手动执行xk6 build已被取代。 - 浏览器模块导入:
import browser from 'k6/browser'—— 旧的k6/experimental/browser路径已在 v0.52+ 中移除。任何使用/experimental/的生成代码都会失败。 - 内置 OTLP 输出:
k6 run --out experimental-opentelemetry=...—— 将结果直接传输到跟踪后端。
import http from 'k6/http'
import browser from 'k6/browser'
import { check } from 'k6'
export const options = { vus: 5, duration: '30s' }
export default async function () {
const page = await browser.newPage()
await page.goto('https://example.com')
check(page, { 'title present': async p => (await p.title()).length > 0 })
await page.close()
}
性能测试类型
| 类型 | 时长 | VU 数 | 用途 | 使用时机 |
|---|---|---|---|---|
| 负载 | 5-10 分钟 | 预期流量 | 验证正常情况 | 每次发布 |
| 压力 | 10-20 分钟 | 预期流量的 2-3 倍 | 找出故障临界点 | 发布前 |
| 突增 | 5 分钟 | 突然激增至 10 倍 | 测试自动扩缩容 | 活动前 |
| 浸泡 | 4-12 小时 | 正常负载 | 检测内存泄漏 | 每周/每晚 |
pytest 并行执行
使用 pytest-xdist 加速测试套件:
# pyproject.toml
[tool.pytest.ini_options]
addopts = ["-n", "auto", "--dist", "loadscope"]
markers = [
"slow: marks tests as slow",
"smoke: critical path tests for CI/CD",
]
# Run with parallel workers and coverage
pytest -n auto --dist loadscope --cov=app --cov-report=term-missing --maxfail=3
# CI fast path — skip slow tests
pytest -m "not slow" -n auto
# Debug mode — single worker
pytest -n 0 -x --tb=long
工作进程数据库隔离
使用数据库运行并行测试时,按工作进程进行隔离:
@pytest.fixture(scope="session")
def db_engine(worker_id):
db_name = f"test_db_{worker_id}" if worker_id != "master" else "test_db"
engine = create_engine(f"postgresql://localhost/{db_name}")
yield engine
engine.dispose()
关键阈值
| 指标 | 目标 | 工具 |
|---|---|---|
| p95 响应时间 | < 500ms | k6 |
| p99 响应时间 | < 1000ms | k6 |
| 错误率 | < 1% | k6 / Locust |
| 业务逻辑覆盖率 | 90% | pytest-cov |
| 关键路径覆盖率 | 100% | pytest-cov |
决策指南
| 场景 | 建议 |
|---|---|
| JavaScript/TypeScript 团队 | 使用 k6 进行负载测试 |
| Python 团队 | 使用 Locust 进行负载测试 |
| 需要 CI 阈值 | 使用 k6(内置阈值支持) |
| 需要分布式测试 | 使用 Locust(内置分布式模式) |
| 测试套件运行缓慢 | 使用带 -n auto 的 pytest-xdist |
| 并行测试不稳定 | 使用 --dist loadscope 对 fixture 分组 |
| 数据库密集型测试 | 使用按工作进程隔离的数据库和 worker_id |
相关技能
ork:testing-unit— 单元测试模式、pytest fixtureork:testing-e2e— 使用 Playwright 进行端到端性能测试ork:performance— Core Web Vitals 和优化模式
评论
支持 Markdown