Skip to content

性能 Monitoring

Measure and optimize application 性能使用 APM, profiling, and golden signals.

你将学到

  • four golden signals 的 monitoring
  • APM tools: 新 Relic, Datadog, OpenTelemetry
  • Profiling 到查找 bottlenecks
  • 设置 and tracking 性能 budgets

概念讲解

四个黄金信号

Google's SRE book defines four golden signals for monitoring any system. Latency — how long requests take, tracked as percentiles (p50, p95, p99) not just averages. Traffic — how many requests are coming in, measured in requests per second or concurrent connections. Errors — the rate of failed requests, including both explicit errors and internal errors. Saturation — how full your system is, like CPU usage, memory pressure, or connection pool utilization. These four signals tell you if your system is healthy and performing well.

APM Tools

Application Performance Monitoring (APM) tools instrument your code to trace requests through your system. New Relic and Datadog are commercial leaders. OpenTelemetry is an open standard for instrumentation that works with many backends. These tools show you where time is spent in each request — database queries, external API calls, rendering — and help you find slow endpoints. They also provide service maps, showing how your services call each other.

Profiling

When a service is slow, you need to know *why*. Profiling samples the program's execution to show where CPU time is spent. In Node.js, use --prof or the built-in profiler. In Python, use cProfile. In Go, use pprof. Flame graphs visualize profiling data, making it easy to see which functions dominate. Profiling in production is essential for finding real-world bottlenecks that don't appear in synthetic tests.

Performance Budgets

A performance budget sets limits on metrics like page load time, bundle size, and API response time. When a change exceeds the budget, the CI pipeline fails. This prevents performance from degrading over time. Track the p95 and p99 latency, not just the average — averages hide tail latency that affects real users. Set budgets based on user expectations: if your API should respond in 200ms, alert when p95 exceeds that. Review and adjust budgets as your system evolves.

示例代码

              # Measure API response time with curl
curl -o /dev/null -s -w "Time: %{time_total}s\n" \
  https://api.example.com/users

# Load testing with Apache Bench
ab -n 1000 -c 10 https://api.example.com/users
#   -n 1000  = 1000 total requests
#   -c 10    = 10 concurrent requests

# Node.js: enable the profiler
node --prof app.js
#   generates a profile file, analyze with:
node --prof-process isolate-*.log > profile.txt

# Docker: check container resource usage
docker stats --no-stream
#   shows CPU%, memory, and network I/O per container
            

curl measures 响应时间, ab 加载测试使用 concurrent 请求, 节点 --prof generates CPU profiles, and docker stats shows 真实-时间 resource usage 每容器.

动手试一试

常见错误

错误写法

仅 tracking 平均 latency

正确做法

Track percentiles (p95, p99). 平均 hide tail latency — 如果 1% 的请求 take 10 seconds, 平均 looks fine 但那些用户有 terrible experience.

错误写法

Profiling 仅 in development

正确做法

Production traffic reveals bottlenecks 那个 synthetic 测试 miss. Use continuous profiling tools 那个 sample production traffic 使用低 overhead, 如 eBPF-based profilers.

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误