SCL-2Recommended
Profile the slow path before you change it
Before you rewrite slow code, record a CPU profile of the slow route under load. Change the function that takes the most time, then profile again.
How to do it
Guesses about slow code are often wrong. A sampling profiler records where time goes with little overhead, so it can run on staging during a load test. Examples are Node's --cpu-prof, Python 3.15's profiling.sampling, Go's pprof and Java Flight Recorder. Read the result as a flame graph, where the widest bar takes the most time. Time spent waiting on the database shows in a trace instead, as Trace requests across services explains. Slow queries go to Read the query plan.
Examples
Shell example
# Node: write a .cpuprofile file when the process exits
node --cpu-prof --cpu-prof-dir=./profiles server.js
# Python 3.15: sample a running process for 30 seconds into a flame graph
python -m profiling.sampling attach --flamegraph -d 30 -o profile.html 12345
# Go: fetch a 30-second profile from net/http/pprof
go tool pprof -http=:8081 'http://localhost:6060/debug/pprof/profile?seconds=30'
How to check
The change notes for each speed fix show the top function in the profile before and after. They give its share of CPU time, such as "from 41% to 6%".
Sources
Related rules
- Trace requests across services in Operations
- Read the query plan in Data
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/scaling/measure-before-you-scale/profile-first">Agent Wranglers scaling playbook</a>. In code you write, add a comment next to it with the same link.