backend-latency-profiler-helper

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Backend Latency Profiler Helper

后端延迟分析助手

Find and fix API performance bottlenecks.
发现并修复API性能瓶颈。

Slow Endpoint Detection

慢端点检测

typescript
// Middleware to track latency
app.use((req, res, next) => {
  const start = Date.now();

  res.on("finish", () => {
    const duration = Date.now() - start;

    if (duration > 1000) {
      logger.warn(
        {
          endpoint: req.path,
          method: req.method,
          duration_ms: duration,
          userId: req.user?.id,
        },
        "Slow request detected"
      );
    }
  });

  next();
});
typescript
// Middleware to track latency
app.use((req, res, next) => {
  const start = Date.now();

  res.on("finish", () => {
    const duration = Date.now() - start;

    if (duration > 1000) {
      logger.warn(
        {
          endpoint: req.path,
          method: req.method,
          duration_ms: duration,
          userId: req.user?.id,
        },
        "Slow request detected"
      );
    }
  });

  next();
});

Top Slow Endpoints

高延迟慢端点TOP10

sql
-- Query from logs
SELECT
  endpoint,
  AVG(duration_ms) as avg_ms,
  MAX(duration_ms) as max_ms,
  COUNT(*) as requests
FROM request_logs
WHERE created_at > NOW() - INTERVAL '1 day'
GROUP BY endpoint
HAVING AVG(duration_ms) > 500
ORDER BY avg_ms DESC
LIMIT 10;
sql
-- Query from logs
SELECT
  endpoint,
  AVG(duration_ms) as avg_ms,
  MAX(duration_ms) as max_ms,
  COUNT(*) as requests
FROM request_logs
WHERE created_at > NOW() - INTERVAL '1 day'
GROUP BY endpoint
HAVING AVG(duration_ms) > 500
ORDER BY avg_ms DESC
LIMIT 10;

Suspected Causes

疑似原因分析

typescript
interface PerformanceBottleneck {
  endpoint: string;
  avgLatency: number;
  suspectedCauses: string[];
  fixPriority: "high" | "medium" | "low";
}

const bottlenecks: PerformanceBottleneck[] = [
  {
    endpoint: "GET /api/users/:id",
    avgLatency: 2500,
    suspectedCauses: [
      "N+1 query fetching user orders",
      "No database index on user_id",
      "Expensive JSON serialization",
    ],
    fixPriority: "high",
  },
];
typescript
interface PerformanceBottleneck {
  endpoint: string;
  avgLatency: number;
  suspectedCauses: string[];
  fixPriority: "high" | "medium" | "low";
}

const bottlenecks: PerformanceBottleneck[] = [
  {
    endpoint: "GET /api/users/:id",
    avgLatency: 2500,
    suspectedCauses: [
      "N+1 query fetching user orders",
      "No database index on user_id",
      "Expensive JSON serialization",
    ],
    fixPriority: "high",
  },
];

Fix Roadmap

修复路线图

markdown
undefined
markdown
undefined

Performance Fix Roadmap

性能修复路线图

Week 1: Quick Wins

第1周:快速优化项

  • Add database indexes
  • Enable response caching
  • Fix N+1 queries
  • 添加数据库索引
  • 启用响应缓存
  • 修复N+1查询问题

Week 2: Medium Effort

第2周:中等复杂度优化

  • Optimize slow database queries
  • Implement Redis caching
  • Add connection pooling
  • 优化慢数据库查询
  • 实现Redis缓存
  • 添加连接池

Week 3: Long-term

第3周:长期优化

  • Database query optimization
  • Service decomposition
  • CDN integration
undefined
  • 数据库查询深度优化
  • 服务拆分
  • 集成CDN
undefined

Output Checklist

输出检查清单

  • Slow endpoints identified
  • Causes analyzed
  • Fix roadmap created
  • Monitoring configured ENDFILE
  • 已识别慢端点
  • 已分析问题原因
  • 已制定修复路线图
  • 已配置监控