Sub topic 5
Choosing a workload worth profiling
This chapter assumes you know how a sampling profiler works (it records the call stack at a fixed rate, such as 99 times per second). You should also be able to build and run a Rust program with Cargo. A flamegraph only describes the run you profiled. If that run does not resemble real use, the graph points you at the wrong code.
Why the workload decides the result
A profiler does not know which code matters to you. It reports where the program spent time during one execution. Profile a tiny input and you may see startup costs. Profile an unrealistic input and you may optimize a path that real users never hit.
Three questions help you choose a workload:
- Does it exercise the code path you care about?
- Does it run long enough to collect enough samples?
- Does it give the same result each time you run it?
Run long enough to collect samples
Each sample is one observation. With a rate of 99 Hz, the number of samples is roughly the run time in seconds times 99.
| Run time | Samples at 99 Hz | What you can trust |
|---|---|---|
| 0.1 s | about 10 | Almost nothing. Widths are noise. |
| 1 s | about 100 | Large hotspots only. |
| 10 s | about 1000 | Frames down to roughly 1% of the time. |
Aim for several seconds of work in the code you care about. If a single run is too short, increase the input size or loop over the work inside the program. Do not rely on starting the process many times. Each start adds process startup and loading to the profile.
Keep the workload focused
Startup, argument parsing and printing can crowd out the real work. Make the input a parameter so you can scale it without editing code.
use std::env;
use std::hint::black_box;
fn is_prime(n: u64) -> bool {
if n < 2 {
return false;
}
(2..n).all(|d| n % d != 0)
}
fn count_primes(limit: u64) -> usize {
(0..limit).filter(|&n| is_prime(n)).count()
}
fn main() {
let limit: u64 = env::args()
.nth(1)
.and_then(|s| s.parse().ok())
.unwrap_or(100_000);
let count = count_primes(black_box(limit));
println!("{count}");
}Two details matter here. The limit comes from the command line, so you can raise it until the run lasts long enough. black_box hides the input value from the optimizer, so the compiler cannot precompute the answer at build time. If it did, the profile would show almost nothing.
With cargo flamegraph, arguments for your program go after --. For example, cargo flamegraph --bin myapp -- 200000 profiles a larger run.
Make it representative
Use inputs shaped like production data, not the smallest case that works. Some common mismatches:
- Size. A 10-line test file may fit in cache. A 2 GB file may be limited by memory and disk instead.
- Shape. Sorted data, uniform keys, or empty values can take different code paths than real data.
- Mix. A server handles many kinds of requests. Profile a mix that matches real traffic, or profile one request type at a time on purpose.
- Warm state. If the program normally runs for hours, a cold start of one second is not typical. Do some warm-up work first, or run long enough that startup becomes a small slice.
Avoid workloads where the program mostly waits, for example on a network call or a sleep. A CPU flamegraph shows only time spent running on the CPU, so waiting is invisible. A later chapter covers this limit.
Make it repeatable
Fix random seeds, read input from a file rather than a live source, and avoid timing-dependent behavior. Repeatable input lets you compare profiles before and after a change. Run the workload twice and check that the graphs look alike. If they differ a lot, the workload is the problem, not the code.
Exercise
You profile a program for 0.2 seconds at 99 Hz, and one function shows as a 30% wide frame. Why should you not conclude it is a hotspot yet, and what two changes would give a more trustworthy result?
Show answer
0.2 seconds at 99 Hz is only about 20 samples, so 30% is about 6 samples and could easily be noise. Make the run longer, for example by increasing the input size so it lasts several seconds. Keep the input fixed or seeded, then profile again and check that the same frame is wide in repeated runs.