CPU profiling measures where program execution spends processor time, exposing algorithmic inefficiencies, expensive computations, unexpectedly frequent calls, and hot-path work that may be cached. It turns a vague slowdown into an execution-time investigation. section 7.2 section 7.2.3
Profilers commonly sample execution at intervals or record every function call. They present flame graphs, call trees, or timing tables showing costly functions, callers, and call-stack depth. Trace the widest regions downward, then distinguish work performed directly from work performed by callees. section 7.2.3
A practical profiling workflow:
- Profile realistic workloads during development, including production-sized data or representative concurrency where possible. section 7.2
- Find functions that consume excessive time, execute too often, or keep important threads busy unnecessarily. section 7.2.3 section 7.4.2
- Use line-level profiling after locating a slow function, and investigate operating-system traces when application-level evidence is insufficient. section 7.2.3 section 7.4.2
Reading cumulative and self time
A Python report shows serialize_response with 400 ms of cumulative time but only 5 ms of total self time. The function is mainly a gateway to expensive callees; investigation should continue down its call chain. A function with high self time is performing the costly work directly. section 7.2.3
Table 7.2 Backend profiling concerns, tools, and diagnostic patterns
|
Concern
|
Tools by language
|
What to look for
|
|
CPU hotspots
|
Python: cProfile[1], py-spy[2]
Node.js: --prof[3], clinic[4]
Go: pprof[5]
Java: async-profiler[6],
Java Flight Recorder[7]
|
Functions taking longer than expected, functions call with
unexpected frequency, work on hot paths that could be cached
|
|
Memory behavior
|
Python: tracemalloc[8],
memory_profiler[9]
Node.js: heap snapshots[10]
Go: pprof heap
Java: VisualVM[11], Eclipse
MAT[12]
|
Continuous growth without leveling off, objects that
accumulate across requests, container memory approaching limits
|
|
Database queries
|
Query logging, EXPLAIN/EXPLAIN ANALYZE, Object-relational
mapper (ORM) debug modes
|
N+1 patterns, sequential scans on large tables, missing
indexes, queries returning more data than needed
|
|
Connection management
|
Pool metrics from your database driver
|
Pool exhaustion under load, connections held too long,
connection leaks from error paths
|
Use the backend profiling matrix to choose language-appropriate CPU tools and distinguish CPU hotspots from memory, database, and connection investigations. section 7.2

