关于 performance-engineer
面向开发者的应用性能分析与优化技能,覆盖指标测量、性能剖析、瓶颈定位、改进和验证。
主要功能
- 01指标基线与性能剖析
- 02跨应用层定位瓶颈
- 03数据库、API 和前端优化
- 04优化后性能验证
- 05性能剖析与报告脚本
适用场景
- 01排查 API 响应缓慢
- 02定位数据库查询瓶颈
- 03减少前端加载延迟
- 04优化后检查性能回退
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ zhaono1/ agent-playbook --skill performance-engineer安装后在 Claude Code 中输入/performance-engineer确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
README.mdreferences/checklist.mdmonitoring.mdoptimization.mdscripts/perf_report.pyprofile.py详细介绍
性能工程师
专门分析和优化应用性能、识别瓶颈并实施效率改进。
此技能的激活条件
在以下情况下激活:
- 报告性能问题
- 需要性能优化
- 提及“慢”或“延迟”
- 希望提高效率
性能分析流程
阶段 1:识别问题
-
定义指标
- 基线是什么?
- 目标是什么?
- 可接受的范围是什么?
-
测量当前性能
# Response time curl -w "@curl-format.txt" -o /dev/null -s https://example.com/users # Database query time # Add timing logs to queries # Memory usage # Use profiler -
分析应用
# Node.js node --prof app.js # Python python -m cProfile app.py # Go go test -cpuprofile=cpu.prof
阶段 2:查找瓶颈
常见的瓶颈位置:
| 层级 | 常见问题 |
|---|---|
| 数据库 | N+1 查询、缺少索引、结果集过大 |
| API | 获取过多数据、没有缓存、串行请求 |
| 应用 | 算法效率低、日志过多 |
| 前端 | 包体积大、重复渲染、未启用延迟加载 |
| 网络 | 请求过多、负载过大、未启用压缩 |
阶段 3:优化
数据库优化
N+1 查询:
// Bad: N+1 queries
const users = await User.findAll();
for (const user of users) {
user.posts = await Post.findAll({ where: { userId: user.id } });
}
// Good: Eager loading
const users = await User.findAll({
include: [{ model: Post, as: 'posts' }]
});
缺少索引:
-- Add index on frequently queried columns
CREATE INDEX idx_user_email ON users(email);
CREATE INDEX idx_post_user_id ON posts(user_id);
API 优化
分页:
// Always paginate large result sets
const users = await User.findAll({
limit: 100,
offset: page * 100
});
字段选择:
// Select only needed fields
const users = await User.findAll({
attributes: ['id', 'name', 'email']
});
压缩:
// Enable gzip compression
app.use(compression());
前端优化
代码拆分:
// Lazy load routes
const Dashboard = lazy(() => import('./Dashboard'));
记忆化:
// Use useMemo for expensive calculations
const filtered = useMemo(() =>
items.filter(item => item.active),
[items]
);
图像优化:
- 使用 WebP 格式
- 延迟加载图像
- 使用响应式图像
- 压缩图像
阶段 4:验证
- 再次测量
- 与基线对比
- 确保没有性能回退
- 记录改进情况
性能目标
根据服务 SLO、当前基线、工作负载特征、成本预算和关键用户旅程制定目标。下表仅为示例起点;没有证据或负责人同意时,切勿将其视为系统的验收标准。
| 指标 | 目标 | 临界阈值 |
|---|---|---|
| API 响应(p50) | < 100ms | < 500ms |
| API 响应(p95) | < 500ms | < 1s |
| API 响应(p99) | < 1s | < 2s |
| 数据库查询 | < 50ms | < 200ms |
| 页面加载(FMP) | < 2s | < 3s |
| 可交互时间 | < 3s | < 5s |
| 内存使用量 | < 512MB | < 1GB |
常见优化
缓存策略
// Cache expensive computations
const cache = new Map();
async function getUserStats(userId: string) {
if (cache.has(userId)) {
return cache.get(userId);
}
const stats = await calculateUserStats(userId);
cache.set(userId, stats);
// Invalidate after 5 minutes
setTimeout(() => cache.delete(userId), 5 * 60 * 1000);
return stats;
}
批处理
// Bad: Individual requests
for (const id of userIds) {
await fetchUser(id);
}
// Good: Batch request
await fetchUsers(userIds);
防抖/节流
// Debounce search input
const debouncedSearch = debounce(search, 300);
// Throttle scroll events
const throttledScroll = throttle(handleScroll, 100);
性能监控
关键指标
- 响应时间:处理请求所需的时间
- 吞吐量:每秒请求数
- 错误率:失败请求的百分比
- 内存使用量:已使用的堆内存/RAM
- CPU 使用率:处理器利用率
监控工具
| 工具 | 用途 |
|---|---|
| Lighthouse | 前端性能 |
| New Relic | APM 监控 |
| Datadog | 基础设施监控 |
| Prometheus | 指标收集 |
脚本
分析应用:
python3 scripts/profile.py --name <service-name> --output perf-profile.txt
生成性能报告:
python3 scripts/perf_report.py --name <service-name> --output perf-report.md
参考资料
references/optimization.md- 优化技术references/monitoring.md- 监控设置references/checklist.md- 性能检查清单
评论
支持 Markdown