关于 performance-profiler
面向开发者的性能分析技能,用于排查 Web、React、Node.js 和数据库瓶颈。内容涵盖测量流程、诊断工具与优化检查项。
主要功能
- 01识别网络、渲染、内存和计算瓶颈
- 02测量优化前后的性能基线
- 03指导 Web 指标和打包分析
- 04涵盖 React 与 Node.js 性能分析
- 05提供数据库查询检查与优化清单
适用场景
- 01排查页面加载慢和 Web 指标不佳
- 02查找耗时的 React 组件或过大的打包文件
- 03诊断 Node.js CPU 或内存问题
- 04检查慢速数据库查询和索引
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ onewave-ai/ claude-skills --skill performance-profiler安装后在 Claude Code 中输入/performance-profiler确认已加载。
详细介绍
性能分析器
使用说明
进行性能分析时:
- 确定瓶颈类型:网络、渲染、内存或计算
- 优化前测量基线
- 使用合适的工具进行分析
- 应用优化
- 测量改进效果
Web 性能
Core Web Vitals
# Lighthouse CLI
npx lighthouse https://yoursite.com --view
# With specific metrics
npx lighthouse https://yoursite.com --only-categories=performance
目标指标:
| 指标 | 良好 | 需要改进 | 较差 |
|---|---|---|---|
| LCP(最大内容绘制) | < 2.5s | 2.5-4s | > 4s |
| INP(下一次绘制交互) | < 200ms | 200-500ms | > 500ms |
| CLS(累积布局偏移) | < 0.1 | 0.1-0.25 | > 0.25 |
Bundle 分析
# Next.js
ANALYZE=true npm run build
# Webpack
npx webpack-bundle-analyzer stats.json
# Vite
npx vite-bundle-visualizer
React 性能
React DevTools Profiler
- 安装 React DevTools 浏览器扩展
- 打开 DevTools → Profiler 标签页
- 点击 Record,与应用交互,然后停止录制
- 分析火焰图,找出运行缓慢的组件
常见 React 优化
// 1. Memoize expensive components
const MemoizedList = React.memo(function List({ items }) {
return items.map(item => <Item key={item.id} {...item} />);
});
// 2. Use useMemo for expensive calculations
const sortedItems = useMemo(() => {
return [...items].sort((a, b) => a.name.localeCompare(b.name));
}, [items]);
// 3. Use useCallback for stable function references
const handleClick = useCallback((id: string) => {
setSelected(id);
}, []);
// 4. Virtualize long lists
import { FixedSizeList } from 'react-window';
function VirtualList({ items }) {
return (
<FixedSizeList
height={400}
itemCount={items.length}
itemSize={50}
width="100%"
>
{({ index, style }) => (
<div style={style}>{items[index].name}</div>
)}
</FixedSizeList>
);
}
// 5. Lazy load components
const HeavyComponent = React.lazy(() => import('./HeavyComponent'));
function App() {
return (
<Suspense fallback={<Loading />}>
<HeavyComponent />
</Suspense>
);
}
Node.js 性能
性能分析
# CPU profile
node --prof app.js
node --prof-process isolate-*.log > profile.txt
# Heap snapshot
node --inspect app.js
# Then use Chrome DevTools Memory tab
# Clinic.js (comprehensive)
npx clinic doctor -- node app.js
npx clinic flame -- node app.js
npx clinic bubbleprof -- node app.js
内存泄漏检测
// Add to app for debugging
const used = process.memoryUsage();
console.log({
heapUsed: `${Math.round(used.heapUsed / 1024 / 1024)} MB`,
heapTotal: `${Math.round(used.heapTotal / 1024 / 1024)} MB`,
external: `${Math.round(used.external / 1024 / 1024)} MB`,
});
数据库性能
-- PostgreSQL: Analyze slow queries
EXPLAIN ANALYZE SELECT * FROM users WHERE email = '[email protected]';
-- Find missing indexes
SELECT relname, seq_scan, idx_scan
FROM pg_stat_user_tables
WHERE seq_scan > idx_scan;
查询优化
// Bad: N+1 query
const users = await db.user.findMany();
for (const user of users) {
const posts = await db.post.findMany({ where: { userId: user.id } });
}
// Good: Single query with include
const users = await db.user.findMany({
include: { posts: true }
});
// Good: Select only needed fields
const users = await db.user.findMany({
select: { id: true, name: true, email: true }
});
快速见效检查清单
- 启用 gzip/brotli 压缩
- 添加缓存标头
- 延迟加载图像(
loading="lazy") - 预连接到外部域名
- 使用 CDN 提供静态资源
- 精简 JavaScript 包
- 延迟加载非关键 JS
- 优化图像(WebP、合适的尺寸)
- 添加数据库索引
- 使用连接池
评论
支持 Markdown