mcpField
K
k6-docs

k6-docs

github.com/takuan-osho测试与质量058 次安装15 周前更新
代码仓库

关于 k6-docs

k6-docs 是面向 k6 负载测试脚本编写与调试的文档访问技能,通过在线获取 Grafana k6 最新文档,提供 API 和测试概念参考。

主要功能

  1. 01访问最新 k6 官方文档
  2. 02查找 JavaScript API 参考
  3. 03查看示例和测试指南
  4. 04查询生命周期、指标与阈值

适用场景

  1. 01编写新的 k6 负载测试脚本
  2. 02排查现有测试代码问题
  3. 03查找 API 方法及参数
  4. 04了解检查项和自定义指标

安装

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

$npx skills add https://github.com/takuan-osho/ccmarketplace --skill k6-docs

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

详细介绍

Grafana k6 文档访问

概述

此技能可访问最新的官方 Grafana k6 文档,帮助编写和调试负载测试脚本。k6 是一款现代化的负载测试工具,专为 API、微服务和网站的性能测试而构建。

何时使用此技能

在以下情况下使用此技能:

  • 编写新的 k6 负载测试脚本
  • 调试现有的 k6 测试代码
  • 查找 k6 API 方法及其参数
  • 了解 k6 测试生命周期钩子
  • 学习 k6 指标和阈值
  • 实现 k6 检查和自定义指标
  • 使用 k6 扩展或模块
  • 排查 k6 测试执行问题

核心功能

1. 访问文档

使用 WebFetch 工具访问最新的 k6 文档:

主要文档 URL:

常用 API 参考 URL:

使用模式:

Use WebFetch with the appropriate documentation URL and a focused prompt like:
- "Show me the API reference for http.post method"
- "Explain how to use checks in k6"
- "Show examples of custom metrics"
2. 常见 k6 模式

基本 HTTP GET 测试:

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://test.k6.io');
  check(res, {
    'status is 200': (r) => r.status === 200,
  });
  sleep(1);
}

使用 JSON 的 HTTP POST:

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

export default function () {
  const url = 'https://httpbin.test.k6.io/post';
  const payload = JSON.stringify({
    name: 'test',
  });
  const params = {
    headers: {
      'Content-Type': 'application/json',
    },
  };

  const res = http.post(url, payload, params);
  check(res, {
    'status is 200': (r) => r.status === 200,
  });
}

带阈值的渐增 + 稳定 + 渐减阶段:

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

export const options = {
  stages: [
    { duration: '1m', target: 100 }, // ramp up to 100 VUs
    { duration: '3m', target: 100 }, // steady at 100 VUs
    { duration: '1m', target: 0 },   // ramp down to 0
  ],
  thresholds: {
    http_req_duration: ['p(95)<500'], // p95 latency < 500ms
    http_req_failed: ['rate<0.01'],   // error rate < 1%
    checks: ['rate>0.99'],            // 99%+ checks pass
  },
};

export default function () {
  const res = http.get(`${__ENV.BASE_URL || 'https://test.k6.io'}/api/users`);
  check(res, { 'status is 200': (r) => r.status === 200 });
  sleep(1);
}

阈值语法速查(最常用的指标):

  • http_req_duration: ['p(95)<500'] — 第 95 百分位低于 500 毫秒
  • http_req_failed: ['rate<0.01'] — 失败率低于 1%
  • checks: ['rate>0.99'] — 至少 99% 的 check() 调用通过
  • iteration_duration: ['avg<1000'] — 平均迭代时长低于 1 秒

未通过的 check() 调用不会增加 http_req_failed;该指标仅跟踪 HTTP 层级的失败(网络错误、5xx)。使用 checks 阈值来确保断言成功率。

3. 文档搜索策略

搜索特定 k6 功能时:

  1. 先广泛搜索:使用 WebSearch 查找相关文档页面

    • 示例:"k6 负载测试自定义指标文档"
  2. 再具体查找:使用 WebFetch 获取最相关的文档 URL

  3. 查找 API 方法:前往 JavaScript API 部分

  4. 查找操作指南:查看“使用 k6”部分

4. k6 核心概念

测试生命周期:

  • init 上下文:加载时运行的代码(导入、选项)
  • setup():在测试前运行一次
  • default function():VU 代码,重复运行
  • teardown():在测试后运行一次

负载选项:

  • vus:虚拟用户数
  • duration:测试时长
  • iterations:所有 VU 的总迭代次数
  • stages:递增模式
  • thresholds:通过/失败判定条件

HTTP 方法:

  • http.get()、http.post()、http.put()、http.delete()
  • 使用 http.batch() 发送并行请求

检查与阈值:

  • check():验证条件,但不会停止测试
  • thresholds:定义通过/失败判定条件,可中止测试

工作流

  1. 确定需求:判断需要哪种 k6 功能
  2. 搜索文档:使用 WebSearch,或直接访问已知文档 URL
  3. 获取特定页面:使用 WebFetch 获取详细信息
  4. 编写代码:根据文档编写 k6 测试代码
  5. 验证:对照文档中的示例和最佳实践进行检查

最佳实践

  • 始终检查最新的文档 URL 结构(grafana.com/docs/k6/latest/)
  • 对于复杂场景,请在 Examples 部分查找示例
  • 排查问题时,请同时查看 API 参考和 Using k6 指南
  • 不确定要获取哪个文档页面时,请先使用 WebSearch
  • 实现复杂功能时,请参考多个文档页面

常见文档部分

主题URL 模式
主要文档https://grafana.com/docs/k6/latest/
JavaScript APIhttps://grafana.com/docs/k6/latest/javascript-api/{module}/
HTTP 模块https://grafana.com/docs/k6/latest/javascript-api/k6-http/
示例https://grafana.com/docs/k6/latest/examples/
测试生命周期https://grafana.com/docs/k6/latest/using-k6/test-lifecycle/
阈值https://grafana.com/docs/k6/latest/using-k6/thresholds/
选项https://grafana.com/docs/k6/latest/using-k6/k6-options/
指标https://grafana.com/docs/k6/latest/using-k6/metrics/
检查项https://grafana.com/docs/k6/latest/javascript-api/k6/check/

备注

  • 此技能不会在本地捆绑 k6 文档;它会在线获取最新版本
  • 始终通过检查 URL 是否包含 /latest/ 来确认获取的文档是最新的
  • 对于特定版本的文档,请将 /latest/ 替换为具体的版本号
  • k6 文档内容全面且组织有序;请使用目录进行导航

评论

支持 Markdown