mcpField
T
testing-perf

testing-perf

github.com/yonatangross测试与质量299114 次安装今天更新
代码仓库

关于 testing-perf

面向测试工程师和开发者的性能与负载测试实践指南,涵盖 k6、Locust 和 pytest 执行优化。提供测试类型、并行执行、数据库隔离及测试清单等模式。

主要功能

  1. 01k6 阈值、阶段与自定义指标
  2. 02Locust 负载测试模式
  3. 03负载、压力、突增和浸泡测试分类
  4. 04pytest-xdist 并行执行与标记
  5. 05并行测试的工作进程数据库隔离
  6. 06性能与 pytest 检查清单

适用场景

  1. 01为 API 配置分阶段负载测试
  2. 02根据流量特征选择负载测试类型
  3. 03用 pytest-xdist 加速测试套件
  4. 04隔离并行测试的数据库工作进程

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/yonatangross/orchestkit --skill testing-perf

安装后在 Claude Code 中输入/testing-perf确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

test-cases.json
checklists/
performance-checklist.md
pytest-production-checklist.md
references/
custom-plugins.md
k6-patterns.md
xdist-parallel.md
rules/
_sections.md
execution.md
perf-k6.md
perf-locust.md
perf-types.md
pytest-execution.md
pytest-plugins.md
scripts/
test-case-template.md

详细介绍

性能与负载测试模式

专注于性能测试、负载测试和 pytest 执行优化。涵盖 k6、Locust、pytest-xdist 并行执行、自定义插件和测试类型分类。

快速参考

领域文件用途
k6 负载测试rules/perf-k6.md阈值、阶段、自定义指标、CI 集成
Locust 测试rules/perf-locust.mdPython 负载测试、任务权重、身份验证流程
测试类型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.mdconftest 插件、可安装插件、钩子参考
性能检查清单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 响应时间< 500msk6
p99 响应时间< 1000msk6
错误率< 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 fixture
  • ork:testing-e2e — 使用 Playwright 进行端到端性能测试
  • ork:performance — Core Web Vitals 和优化模式

评论

支持 Markdown