关于 profiling-optimization
profiling-optimization 是一项指导性能分析与优化的技能,适用于需要定位瓶颈并提升代码性能的开发人员。它提供多种语言和工具的参考指南,并强调先测量再优化。
主要功能
- 01分析代码执行并定位瓶颈
- 02聚焦 CPU 瓶颈与热点路径
- 03参考 Node.js、Chrome DevTools 和 Python 分析
- 04分析数据库查询并生成火焰图
- 05通过基准测试衡量优化效果
适用场景
- 01调查缓慢请求并降低延迟
- 02定位 CPU 密集型代码路径
- 03提升应用吞吐量
- 04验证性能改动的实际影响
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ aj-geddes/ useful-ai-prompts --skill profiling-optimization安装后在 Claude Code 中输入/profiling-optimization确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/benchmarking.mdchrome-devtools-cpu-profile.mddatabase-query-profiling.mdflame-graph-generation.mdnodejs-profiling.mdpython-cprofile.mdscripts/validate-api.shtemplates/api-scaffold.yaml详细介绍
性能分析与优化
目录
概述
对代码执行进行性能分析,找出性能瓶颈,并采用数据驱动的方法优化关键路径。
适用场景
- 性能优化
- 找出 CPU 瓶颈
- 优化热点路径
- 调查缓慢的请求
- 降低延迟
- 提高吞吐量
快速开始
最简可运行示例:
import { performance, PerformanceObserver } from "perf_hooks";
class Profiler {
private marks = new Map<string, number>();
mark(name: string): void {
this.marks.set(name, performance.now());
}
measure(name: string, startMark: string): number {
const start = this.marks.get(startMark);
if (!start) throw new Error(`Mark ${startMark} not found`);
const duration = performance.now() - start;
console.log(`${name}: ${duration.toFixed(2)}ms`);
return duration;
}
async profile<T>(name: string, fn: () => Promise<T>): Promise<T> {
const start = performance.now();
try {
return await fn();
} finally {
// ... (see reference guides for full implementation)
参考指南
references/ 目录中的详细实现:
| 指南 | 内容 |
|---|---|
| Node.js 性能分析 | Node.js 性能分析 |
| Chrome DevTools CPU 性能分析 | Chrome DevTools CPU 性能分析 |
| Python cProfile | Python cProfile |
| 基准测试 | 基准测试 |
| 数据库查询性能分析 | 数据库查询性能分析 |
| 火焰图生成 | 火焰图生成 |
最佳实践
✅ 应该
- 优化前先进行性能分析
- 聚焦热点路径
- 衡量变更的影响
- 使用接近生产环境的数据
- 权衡内存与速度
- 记录优化原因
❌ 不应该
- 未经性能分析就进行优化
- 为了微小的收益牺牲可读性
- 跳过基准测试
- 优化冷路径
- 未经测量就进行变更
评论
支持 Markdown