关于 performance-engineering
performance-engineering 是面向开发与运维团队的性能工程技能,涵盖测试、分析和优化流程。它提供测试类型选择框架及多语言分析和优化示例。
主要功能
- 01负载、压力、浸泡与峰值测试
- 02CPU、内存和 I/O 性能分析
- 03数据库与 API 优化策略
- 04前端 Core Web Vitals 优化
- 05性能 SLO 与预算设置
- 06CI/CD 性能测试集成
适用场景
- 01发布前验证系统承载能力
- 02定位慢响应与高延迟原因
- 03检测长期运行服务的内存泄漏
- 04在 CI/CD 中发现性能回归
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ ancoleman/ ai-design-components --skill performance-engineering安装后在 Claude Code 中输入/performance-engineering确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
outputs.yamlexamples/k6/load-test.jssoak-test.jsstress-test.jsexamples/locust/load_test.pyexamples/optimization/api_optimization.tsexamples/profiling/python/pyspy_example.shreferences/benchmarking.mdfrontend-performance.mdoptimization-strategies.mdprofiling-guide.mdslo-framework.mdtesting-types.md详细介绍
性能工程
目的
性能工程涵盖负载测试、性能分析和优化,旨在交付可靠且可扩展的系统。本技能提供了用于选择合适性能测试方法(负载、压力、浸泡、尖峰)的框架、用于识别瓶颈(CPU、内存、I/O)的性能分析技术,以及面向后端 API、数据库和前端应用的优化策略。
使用本技能可在发布前验证系统容量、在 CI/CD 流水线中检测性能回归、通过性能分析识别并解决瓶颈,以及优化整个技术栈中的应用响应能力。
何时使用本技能
常见触发场景:
- “验证 API 能否承载预期流量”
- “找出最大容量和故障临界点”
- “确定应用变慢的原因”
- “检测内存泄漏或资源耗尽”
- “优化 Core Web Vitals 以提升 SEO”
- “在 CI/CD 中设置性能测试”
- “降低云基础设施成本”
使用场景:
- 发布前的容量规划和负载验证
- 重构后的性能回归测试
- 调查响应时间过长或延迟过高的问题
- 检测长期运行服务中的内存泄漏
- 优化数据库查询性能
- 验证自动扩缩容配置
- 建立性能 SLO 和预算
性能测试类型
负载测试
验证系统在预期流量下的行为。
适用场景: 发布前的容量规划、重构后的回归测试、验证自动扩缩容。
压力测试
找出系统的容量上限和故障模式。
适用场景: 容量规划、了解故障行为、基础设施规格决策。
浸泡测试
识别内存泄漏、资源耗尽以及随时间推移出现的性能下降。
适用场景: 检测内存泄漏、验证连接池清理、测试长期运行的批处理作业。
尖峰测试
验证系统对流量突然激增的响应。
适用场景: 验证自动扩缩容、测试事件驱动系统(产品发布)、确保速率限制正常工作。
快速决策框架
应使用哪种测试类型?
What am I trying to learn?
├─ Can my system handle expected traffic? → LOAD TEST
├─ What's the maximum capacity? → STRESS TEST
├─ Will it stay stable over time? → SOAK TEST
└─ Can it handle traffic spikes? → SPIKE TEST
有关详细的测试模式、负载场景和结果解读,请参阅 references/testing-types.md。
负载测试快速入门
k6(JavaScript)
安装:
brew install k6 # macOS
sudo apt-get install k6 # Linux
基本负载测试:
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 },
{ duration: '1m', target: 20 },
{ duration: '30s', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const res = http.get('https://api.example.com/products');
check(res, {
'status is 200': (r) => r.status === 200,
});
sleep(1);
}
运行: k6 run script.js
有关压力、浸泡和尖峰测试示例,请参阅 examples/k6/。
Locust(Python)
安装:
pip install locust
基本负载测试:
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(1, 3)
host = "https://api.example.com"
@task(3)
def view_products(self):
self.client.get("/products")
@task(1)
def view_product_detail(self):
self.client.get("/products/123")
运行: locust -f locustfile.py --headless -u 100 -r 10 --run-time 10m
有关 REST API 测试和数据驱动测试,请参阅 examples/locust/。
性能分析快速入门
何时进行性能分析
| 症状 | 性能分析类型 | 工具 |
|---|---|---|
| CPU 使用率高(>70%) | CPU 性能分析 | py-spy、pprof、DevTools |
| 内存持续增长 | 内存性能分析 | memory_profiler、pprof heap |
| 响应缓慢,CPU 使用率低 | I/O 性能分析 | 查询日志、pprof block |
Python 性能分析
py-spy(适用于生产环境):
pip install py-spy
# Profile running process
py-spy record -o profile.svg --pid <PID> --duration 30
# Top-like view
py-spy top --pid <PID>
内存性能分析:
from memory_profiler import profile
@profile
def my_function():
a = [1] * (10 ** 6)
return a
# Run: python -m memory_profiler script.py
Go 性能分析
pprof(内置):
import (
"net/http"
_ "net/http/pprof"
)
func main() {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
startApp()
}
采集性能分析数据:
# CPU profile (30 seconds)
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
# Interactive analysis
(pprof) top
(pprof) web
TypeScript/JavaScript 性能分析
Chrome DevTools(浏览器/Node.js):
Node.js:
node --inspect app.js
# Open chrome://inspect
# Performance tab → Record
clinic.js(Node.js):
npm install -g clinic
clinic doctor -- node app.js
有关详细的性能分析工作流和分析方法,请参阅 references/profiling-guide.md 和 examples/profiling/。
优化策略
缓存
适合缓存的情况:
- 经常查询的数据(>100 次请求/分钟)
- 可容忍的数据新鲜度(接受超过 1 分钟的数据陈旧)
Redis 示例:
import redis
r = redis.Redis()
def get_cached_data(key, fn, ttl=300):
cached = r.get(key)
if cached:
return json.loads(cached)
data = fn()
r.setex(key, ttl, json.dumps(data))
return data
数据库查询优化
避免 N+1 查询:
# Bad: N+1 queries
users = User.query.all()
for user in users:
print(user.orders) # Separate query per user
# Good: Eager loading
users = User.query.options(joinedload(User.orders)).all()
索引:
CREATE INDEX idx_users_email ON users(email);
API 性能
基于游标的分页:
app.get('/api/products', async (req, res) => {
const { cursor, limit = 20 } = req.query;
const products = await db.query(
'SELECT * FROM products WHERE id > ? ORDER BY id LIMIT ?',
[cursor || 0, limit]
);
res.json({
data: products,
next_cursor: products[products.length - 1]?.id,
});
});
前端性能(Core Web Vitals)
关键指标:
- LCP(最大内容绘制): < 2.5s
- INP(交互到下一次绘制): < 200ms
- CLS(累积布局偏移): < 0.1
优化技巧:
- 代码拆分(延迟加载)
- 图像优化(WebP、响应式、延迟加载)
- 预加载关键资源
- 尽量减少阻塞渲染的资源
如需了解详细的优化策略,请参阅 references/optimization-strategies.md 和 references/frontend-performance.md。
性能 SLO
按服务类型推荐的 SLO
| 服务类型 | p95 延迟 | p99 延迟 | 可用性 |
|---|---|---|---|
| 面向用户的 API | < 200ms | < 500ms | 99.9% |
| 内部 API | < 100ms | < 300ms | 99.5% |
| 数据库查询 | < 50ms | < 100ms | 99.99% |
| 后台作业 | < 5s | < 10s | 99% |
| 实时 API | < 50ms | < 100ms | 99.95% |
SLO 选择流程
- 测量基准性能
- 确定用户预期
- 设定可实现的目标(比基准性能好 10-20%)
- 随着系统成熟不断迭代
如需了解详细的 SLO 框架和性能预算,请参阅 references/slo-framework.md。
CI/CD 集成
在流水线中进行性能测试
GitHub Actions 示例:
name: Performance Tests
on:
pull_request:
branches: [main]
jobs:
load-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install k6
run: |
curl https://github.com/grafana/k6/releases/download/v0.48.0/k6-v0.48.0-linux-amd64.tar.gz -L | tar xvz
sudo mv k6-v0.48.0-linux-amd64/k6 /usr/local/bin/
- name: Run load test
run: k6 run tests/load/api-test.js
性能预算:
// k6 test with thresholds (fail build if violated)
export const options = {
thresholds: {
http_req_duration: ['p(95)<500'],
http_req_failed: ['rate<0.01'],
},
};
性能分析工作流
标准流程:
- 观察症状(CPU 使用率高、内存增长、响应缓慢)
- 假设瓶颈所在(CPU?内存?I/O?)
- 根据假设选择性能分析类型
- 在真实负载下运行性能分析器
- 分析性能报告(火焰图、调用树)
- 找出热点(占用 80% 资源的前 20% 函数)
- 优化瓶颈
- 重新进行性能分析,验证改进效果
最佳实践:
- 在真实负载下进行性能分析(而不是在空闲系统上)
- 在生产环境中使用采样型性能分析器(py-spy、pprof)(开销低)
- 关注热点路径(优先优化最大的瓶颈)
- 通过优化前后的对比验证优化效果
工具推荐
负载测试
首选:k6(基于 JavaScript,由 Grafana 提供支持)
- 现代化架构,云原生
- JavaScript DSL(ES6+)
- 与 Grafana/Prometheus 集成
- 支持多种协议(HTTP/1.1、HTTP/2、WebSocket、gRPC)
适用场景: 现代 API、微服务、CI/CD 集成。
替代方案:Locust(基于 Python)
- 原生支持 Python(使用 Python 编写测试)
- 提供用于实时监控的 Web 界面
- 适用于复杂用户场景,灵活性高
适用场景: 以 Python 为主的团队、复杂用户流程。
性能分析
Python:
- py-spy(采样型,适用于生产环境)
- cProfile(确定性,详细)
- memory_profiler(内存泄漏检测)
Go:
- pprof(内置,支持 CPU/堆/协程/阻塞性能分析)
TypeScript/JavaScript:
- Chrome DevTools(浏览器/Node.js)
- clinic.js(Node.js 性能套件)
如需了解详细的工具对比,请参阅 references/testing-types.md 和 references/profiling-guide.md。
参考文档
详细指南:
references/testing-types.md- 负载、压力、浸泡、突增测试模式references/profiling-guide.md- 跨语言的 CPU、内存、I/O 性能分析references/optimization-strategies.md- 缓存、数据库、API 优化references/frontend-performance.md- Core Web Vitals、打包优化references/slo-framework.md- 设置 SLO、性能预算references/benchmarking.md- 基准测试最佳实践
示例:
examples/k6/- 负载、压力、浸泡、突增测试examples/locust/- 基于 Python 的负载测试examples/profiling/- 性能分析示例(Python、Go、TypeScript)examples/optimization/- 缓存、查询、API 优化
相关技能
如需了解全面的测试策略,请参阅 testing-strategies 技能。
如需了解 CI/CD 集成模式,请参阅 building-ci-pipelines 技能。
如需了解如何根据负载测试确定基础设施规模,请参阅 infrastructure-as-code 技能。
如需了解 Kubernetes 性能测试,请参阅 kubernetes-operations 技能。