k6-performance-testing
Installation
Review the source and dependencies with a prompt. A package is available to download.
Install with an AI assistant
Copy the prompt so your assistant can review the pinned revision and dependencies first.
View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "k6-performance-testing" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-6ddabff065c510e9af178c361616a0ad83ceae25f795ae20cceb5b2755da861f. Verify the upstream source and version against https://skillhub.cn/skills/user_c886063e/k6-performance-testing-skill before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.
Source and setup
skillhub.cn
Setup guidance pending
See the original source for supported tools, dependencies, and permissions.
Installation test pending
k6 performance-testing script generator: write k6 scripts for website/API performance and load testing, offering four production-grade templates (smoke test, step ramp to find tipping points, full business flow, mixed traffic plus bursts). Intended for scenarios requiring k6 scripting, load testing, finding performance inflection points, and report generation; not intended for trivial single‑machine GET tests or protocol testing (gRPC/Kafka/WebSocket) that require extra extensions.
Introduction source: third_party · user_c886063e · 1.0.0 Reference
SkillPi interpretation
- Audience
- Translation pending; see source documentation.
- Inputs and outputs
- Translation pending; see source documentation.
- Testing
- Not tested
Browse files
Frontmatter
name: k6-performance-testing
version: 1.0.0
license: MIT
description: >-
用 k6 编写网站/API 性能与负载压测脚本。提供 4 个生产级模板:冒烟测试、阶梯加压找拐点、完整业务流(登录→浏览→下单)、多接口混合流量+突发流量。Use when 用户要求写 k6 脚本、做压力测试、负载测试、性能压测、找性能拐点、压测网站、压测 API、生成压测报告、模拟登录链路、模拟流量分布、上线前压测、大促/秒杀前压测、CI 性能卡门。NOT for 单机 ab/wrk 就能搞定的极简 GET 压测、JMeter GUI 拖拽式场景、gRPC/Kafka/WebSocket 协议层压测(k6 需额外扩展)。
compatibility: Requires k6 v0.45+ (https://k6.io) and Node.js not required. Works on Windows / macOS / Linux.
metadata:
author: k6-performance-testing
category: dev-testing
tags:
- k6
- performance-testing
- load-testing
- stress-testing
- benchmarking
- devops
platforms:
- windows
- macos
- linuxk6 网站/API 性能压测
Grafana 出品的开源压测工具,单机能模拟数万并发虚拟用户,用 JavaScript 写场景。本 skill 提供 4 个生产级 k6 压测模板,覆盖从冒烟到找拐点、从业务流到混合流量的常见场景。
适用场景
- 上线前 / 改动后做性能回归
- 大促 / 秒杀前做容量评估和压测
- 想知道系统的最大承载量(找性能拐点)
- 模拟真实用户行为链路(登录→浏览→下单)
- 模拟真实流量分布(多接口按权重 + 突发流量)
- CI 卡门(threshold 不达标让 PR 失败)
不适用(NOT for)
- 单机 ab/wrk 就能搞定的极简 GET 压测 —— k6 大材小用
- 需要 GUI 拖拽的场景编排 —— 用 JMeter
- 协议层压测(gRPC / Kafka / WebSocket)—— 这种要
k6/net/grpc等扩展,需单独处理 - 想压垮对手网站的场景 —— 违法,只测自己的服务
快速开始
1. 装 k6
# Windows 推荐
winget install --id GrafanaLabs.k6
# macOS
brew install k6
# 验证
k6 version
2. 跑冒烟测试
先跑 smoke 验证脚本能跑通,不通过别往下走:
k6 run --env URL=https://example.com examples\smoke.js
30 秒跑完,只验证脚本能跑、接口能返回 200。
3. 阶梯加压找拐点
k6 run --env URL=https://你的网站.com examples\stress.js
约 4 分钟逐步加压到 200 并发,看哪里开始掉链子。
4. 跑业务流 / 混合流量
# 完整业务流(登录→浏览→下单)
k6 run --env URL=https://你的网站.com examples\login-flow.js
# 多接口混合 + 突发流量
k6 run --env URL=https://api.你的网站.com examples\api-mix.js
模板选择指南
按"想测什么"挑模板,不要无脑用最复杂的:
| 场景 | 模板 | 原因 |
|---|---|---|
| 改动后快速验证脚本能跑 | smoke.js |
5 VU 30s,不压垮系统 |
| 想知道最大承载量 | stress.js |
阶梯加压,p95/failed 突变点就是拐点 |
| 测登录→下单完整链路 | login-flow.js |
模拟真实用户行为,看 token 状态保持 |
| 模拟真实流量分布 | api-mix.js |
按权重混合多个接口 + 突发场景 |
| 跑 CI 卡门 | smoke.js |
threshold 不达标就让 PR 失败 |
| 秒杀/大促前压测 | stress.js + api-mix.js |
找拐点 + 测突发 |
工作流程
接到压测需求时,按这个顺序推进:
阶段 1:摸底(冒烟)
k6 run --env URL=https://目标.com examples\smoke.js
如果 smoke 都过不了,先排查(详见 references/troubleshooting.md):
- URL 是否对(curl 测一下)
- 网络通不通(Test-NetConnection)
- k6 是否装好
阶段 2:找拐点(阶梯加压)
k6 run --env URL=https://目标.com examples\stress.js
看输出里 p(95) 和 http_req_failed 陡增的那个时点,就是系统极限。
阶段 3:业务流验证
如果是登录型业务,把 login-flow.js 里的 /api/login、/api/products、/api/orders 改成真实接口路径。
如果是非登录业务(首页/详情/搜索),用 api-mix.js 模板。
阶段 4:报告与基线
k6 run --env URL=https://目标.com examples\stress.js `
--summary-export=summary.json `
--linger 30s
记录 p95/p99/错误率作为性能基线,改动后回归对比。
输出格式
跑完 k6 默认输出到 stdout,关键指标会按 指标名: 值 格式打印:
http_req_duration...........: avg=245ms p(95)=890ms p(99)=1.2s
http_req_failed.............: 0.12%
http_reqs...................: 12,453 207/s
vus........................: 80
iterations.................: 1,245
threshold 不达标时,k6 会输出 thresholds on metrics 'xxx' have been crossed 并让退出码非 0(CI 里 PR 直接失败)。
加 --summary-export=summary.json 可以导出每个指标的 p50/p90/p95/p99 完整数据。
核心概念
虚拟用户(VU)
模拟一个真实用户,每个 VU 独立跑 default function(),循环执行。__VU 是当前 VU 编号(从 1 开始)。
加压模式
stages 模式(默认)—— 阶梯加压:
stages: [
{ duration: '30s', target: 20 }, // 暖机
{ duration: '1m', target: 50 }, // 加压
{ duration: '1m', target: 200 }, // 峰值
{ duration: '30s', target: 0 }, // 收尾
]
scenarios 模式(多场景独立)—— 复杂编排:
scenarios: {
steady: { executor: 'constant-vus', vus: 50, duration: '2m' },
burst: { executor: 'ramping-vus', startTime: '2m30s', stages: [...] },
}
阈值(Thresholds)
跑完后自动判定,不达标会让 k6 退出码非 0(CI 里 PR 直接失败):
thresholds: {
http_req_duration: ['p(95)<3000'],
http_req_failed: ['rate<0.01'],
'ttfb_waiting_ms': ['p(95)<800'],
}
check vs threshold
- check:断言,不会让脚本退出(用于收集数据)
- threshold:失败条件,会让 k6 exit 非 0(用于 CI 卡门)
关键指标解读
| 指标 | 含义 | 健康值 |
|---|---|---|
http_req_duration p(95) |
95% 请求耗时 | < 1.5s |
http_req_duration p(99) |
99% 请求耗时 | < 3s |
http_req_failed |
HTTP 错误率(4xx/5xx) | < 1% |
http_reqs |
总请求数 / 速率 | 看曲线趋势 |
http_req_waiting |
TTFB(服务端耗时) | 看是不是服务端慢 |
http_req_connecting |
建连耗时 | 看是不是网络/建连慢 |
iterations |
default 函数完成次数 | 看用户完成度 |
更细的指标解读(怎么定位服务端慢 vs 网络慢)+ 拐点识别,参见 references/metrics.md。
⚠️ Gotchas(常见坑)
- 永远先跑 smoke —— smoke 过不了就别上 stress,否则报错定位不到
- VU 起步别太大 —— 一上来 500+ 容易直接打挂服务。从 10-20 开始阶梯加
- sleep 别太短 —— sleep 0.01s 约等于 DDoS 自己,压测数据失真。一般 0.5-3s
- 第一段 stages 至少 30s 暖机 —— 不然前 30s 数据是失真的(连接池在建立、JIT 在编译)
- 跑 2 次取稳定值 —— 第一次跑往往比第二次慢(缓存没预热)
- 永远不要压生产 —— 先在 staging 压;要压生产必须先和运维对齐
- Ctrl+C 准备好 —— threshold 不达标立刻停,避免误伤
- WAF 拦截 —— 压测流量经常被 Cloudflare/阿里云 WAF 当攻击拦,需要加白名单
- threshold 严到全挂 —— 设 p95 < 100ms 这种"系统最佳值"会让所有 CI 都过不了,按"业务能接受"的标准设
错误排查
按报错类型查表(详见 references/troubleshooting.md):
| 错误关键词 | 类型 | 速查 |
|---|---|---|
thresholds on metrics 'xxx' have been crossed |
阈值不达标 | 看哪个指标,先放宽 threshold 跑一遍 |
dial tcp: i/o timeout / connection refused / no such host |
网络问题 | Test-NetConnection 测连通性 |
context deadline exceeded / out of memory |
客户端资源 | 降 VU 数或上分布式 |
502/503/504 大量出现 |
服务端问题 | 看服务端日志,可能限流/熔断 |
401 大量出现 |
登录态问题 | 检查 token 提取和 header 传递 |
429 Too Many Requests |
限流 | 降 VU 或加白名单 |
x509: certificate signed by unknown authority |
证书问题 | 自签证书或过期 |
k6 命令找不到 |
环境问题 | 重装或刷新 PATH |
通用排查命令(导出数据 + 详细日志):
k6 run `
--env URL=https://你的网站.com `
--summary-export=summary.json `
--linger 30s `
--console-output=stdout `
examples\stress.js
跑完看 summary.json 里每个指标的 p50/p90/p95/p99,能直接定位问题。
VU/时长/threshold 调优指南见 references/tuning.md。
文件清单
k6-performance-testing/
├── SKILL.md # 本文件
├── examples/ # 4 个生产级 k6 脚本模板
│ ├── smoke.js # 冒烟测试(5 VU,30s,验证脚本能跑通)
│ ├── stress.js # 阶梯加压(找性能拐点,含自定义指标)
│ ├── login-flow.js # 完整业务流(登录→浏览→下单,账号池 200)
│ └── api-mix.js # 多接口混合 + 突发流量(scenarios 模式)
└── references/ # 详细参考(按需加载)
├── metrics.md # 指标详解 + 怎么读 + 找拐点
├── troubleshooting.md # 错误排查(A-H 分类 + 流程图)
└── tuning.md # VU/时长/threshold 调优指南
所有脚本都通过 URL 环境变量指定目标:
k6 run --env URL=https://example.com <script>
进阶
- 接 CI:GitHub Actions 跑 smoke.js,PR 不达标不让合
- 长期监控:k6 + InfluxDB + Grafana,画性能趋势图
- 分布式:单扛不住上万 VU,用
k6-operator或 Grafana Cloud - 协议扩展:
k6/net/grpc、k6/experimental/websockets等 - 场景编排:用
scenarios字段同时跑多个独立流量(api-mix.js 是范例)
变更日志
1.0.0 (2026-07-15)
- 初始发布
- 提供 4 个生产级模板:smoke / stress / login-flow / api-mix
- 配套 3 个参考文档:metrics / troubleshooting / tuning
Current listing version
1.0.0
Safety and assessment details
Quality assessment
Out of 100 · Assessment report from SkillPi LAB.
Highlights
- 提供 4 个生产级 k6 压测模板,覆盖冒烟、阶梯加压、业务流、混合流量等核心场景
- 明确区分适用与不适用场景,避免误用
- 支持 CI 卡门和性能基线管理,具备工程落地价值
Areas to improve
-
description写错位置 Reported severity: Minor
顶层 frontmatter 中 description 字段为空,实际描述内容位于 skill_body 内部
Recommendation: 将触发描述移至顶层 frontmatter 的 description 字段,确保平台能正确解析
Highlights
- Use when 和 NOT for 清晰界定边界,覆盖多种压测场景变体
Areas to improve
-
缺少When to Use显式章节 Reported severity: Minor
文档中未设置显式的 'When to Use' 章节,但相关内容已分散在 '适用场景' 和 '模板选择指南' 中
Recommendation: 建议增加独立的 'When to Use' 章节以提升可读性
Highlights
- Phase 清晰(摸底→找拐点→业务流→报告),有明确的出口条件
- 提供端到端示例和模板选择指南,frontmatter 完整
Areas to improve
-
部分异常未在脚本中自动处理 Reported severity: Minor
WAF 拦截、限流等异常依赖人工加白名单或降 VU,脚本层面未内置自动重试或规避机制
Recommendation: 可在脚本中增加基础 retry 逻辑或提示用户配置 WAF 白名单
Highlights
- 工具来源明确(k6 v0.45+),参数正确,覆盖主要异常类型
- 对压生产等高风险操作有明确警告和确认机制
The arithmetic mean of the first four original scores, rounded to a whole number.
Static text checks
100 / 100 · skillpi-20261123-no-r3-v1
No current rules matched the text.
This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.
Permissions and checks before use
Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.
-
Install or fetch external dependencies
Use official sources and pin versions. Check install scripts in an isolated environment. This document check does not cover code downloaded later.
Mentioned in main document · lines 53
Domains or addresses referenced in the document
These addresses may be documentation links or examples, not an approved network allowlist. Verify actual destinations and outgoing data before use.
- example.com
- k6.io
Security rating and access scope
safe · Static evidence review; not a runtime safety guarantee.
Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.
User discussion
No verified reviews yet.