Loading...
Loading...
Compare original and translation side by side
# 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# 响应时间
curl -w "@curl-format.txt" -o /dev/null -s https://example.com/users
# 数据库查询耗时
# 给查询添加计时日志
# 内存使用情况
# 使用性能剖析工具# Node.js
node --prof app.js
# Python
python -m cProfile app.py
# Go
go test -cpuprofile=cpu.prof| Layer | Common Issues |
|---|---|
| Database | N+1 queries, missing indexes, large result sets |
| API | Over-fetching, no caching, serial requests |
| Application | Inefficient algorithms, excessive logging |
| Frontend | Large bundles, re-renders, no lazy loading |
| Network | Too many requests, large payloads, no compression |
| 层级 | 常见问题 |
|---|---|
| 数据库 | N+1 查询、索引缺失、结果集过大 |
| API | 过度拉取、无缓存、串行请求 |
| 应用层 | 算法效率低、日志输出过多 |
| 前端 | 包体积过大、不必要重渲染、未使用懒加载 |
| 网络 | 请求次数过多、 payload 过大、未开启压缩 |
// 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);// 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' }]
});-- 给高频查询的字段添加索引
CREATE INDEX idx_user_email ON users(email);
CREATE INDEX idx_post_user_id ON posts(user_id);// 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());// 大结果集必须做分页
const users = await User.findAll({
limit: 100,
offset: page * 100
});// 仅选择需要的字段
const users = await User.findAll({
attributes: ['id', 'name', 'email']
});// 开启gzip压缩
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]
);// 路由懒加载
const Dashboard = lazy(() => import('./Dashboard'));// 高开销计算使用useMemo缓存
const filtered = useMemo(() =>
items.filter(item => item.active),
[items]
);| Metric | Target | Critical Threshold |
|---|---|---|
| API Response (p50) | < 100ms | < 500ms |
| API Response (p95) | < 500ms | < 1s |
| API Response (p99) | < 1s | < 2s |
| Database Query | < 50ms | < 200ms |
| Page Load (FMP) | < 2s | < 3s |
| Time to Interactive | < 3s | < 5s |
| Memory Usage | < 512MB | < 1GB |
| 指标 | 目标值 | 临界阈值 |
|---|---|---|
| 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;
}// 缓存高开销计算结果
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);
// 5分钟后失效
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);// Bad: 单次请求逐个查询
for (const id of userIds) {
await fetchUser(id);
}
// Good: 批量请求
await fetchUsers(userIds);// Debounce search input
const debouncedSearch = debounce(search, 300);
// Throttle scroll events
const throttledScroll = throttle(handleScroll, 100);// 搜索输入防抖
const debouncedSearch = debounce(search, 300);
// 滚动事件节流
const throttledScroll = throttle(handleScroll, 100);| Tool | Purpose |
|---|---|
| Lighthouse | Frontend performance |
| New Relic | APM monitoring |
| Datadog | Infrastructure monitoring |
| Prometheus | Metrics collection |
| 工具 | 用途 |
|---|---|
| Lighthouse | 前端性能检测 |
| New Relic | APM性能监控 |
| Datadog | 基础设施监控 |
| Prometheus | 指标采集 |
python scripts/profile.pypython scripts/perf_report.pypython scripts/profile.pypython scripts/perf_report.pyreferences/optimization.mdreferences/monitoring.mdreferences/checklist.mdreferences/optimization.mdreferences/monitoring.mdreferences/checklist.md