Debian Codegraph @

docs/src/performance.md

raw
1 <!-- spell-checker:ignore taskset usize -->
2
3 # Performance Profiling Tutorial
4
5 ## Effective Benchmarking with Hyperfine
6
7 [Hyperfine](https://github.com/sharkdp/hyperfine) is a powerful command-line benchmarking tool that allows you to measure and compare execution times of commands with statistical rigor.
8
9 ### Benchmarking Best Practices
10
11 When evaluating performance improvements, always set up your benchmarks to compare:
12
13 1. The GNU implementation as reference
14 2. The implementation without the change
15 3. The implementation with your change
16
17 This three-way comparison provides clear insights into:
18 - How your implementation compares to the standard (GNU)
19 - The actual performance impact of your specific change
20
21 ### Example Benchmark
22
23 First, you will need to build the binary in release mode. Debug builds are significantly slower:
24
25 ```bash
26 cargo build --features unix --profile profiling
27 ```
28
29 ```bash
30 # Three-way comparison benchmark
31 hyperfine \
32 --warmup 3 \
33 "/usr/bin/ls -R ." \
34 "./target/profiling/coreutils.prev ls -R ." \
35 "./target/profiling/coreutils ls -R ."
36
37 # can be simplified with:
38 hyperfine \
39 --warmup 3 \
40 -L ls /usr/bin/ls,"./target/profiling/coreutils.prev ls","./target/profiling/coreutils ls" \
41 "{ls} -R ."
42 ```
43
44 For Ubuntu 25.10 and other distributions that use uutils by default, replace `bin/ls` with `bin/gnuls`. Also:
45
46 ```
47 # to improve the reproducibility of the results:
48 taskset -c 0
49 ```
50
51 ### Interpreting Results
52
53 Hyperfine provides summary statistics including:
54 - Mean execution time
55 - Standard deviation
56 - Min/max times
57 - Relative performance comparison
58
59 Look for consistent patterns rather than focusing on individual runs, and be aware of system noise that might affect results.
60
61 ## Integrated Benchmarking
62
63 Utilities include integrated benchmarks in `src/uu/*/benches/*` using [CodSpeed](https://codspeed.io/) and [Divan](https://github.com/nvzqz/divan).
64
65 **Important**: Before starting performance optimization work, you should add a benchmark for the utility. This provides a baseline for measuring improvements and ensures changes have measurable impact.
66
67 ### Running Benchmarks
68
69 ```bash
70 # Build and run benchmarks for a specific utility
71 cargo codspeed build -p uu_expand
72 cargo codspeed run -p uu_expand
73 ```
74
75 ### Writing Benchmarks
76
77 Use common functions from `src/uucore/src/lib/features/benchmark.rs`:
78
79 ```rust
80 use divan::{Bencher, black_box};
81 use uu_expand::uumain;
82 use uucore::benchmark::{create_test_file, run_util_function, text_data};
83
84 #[divan::bench(args = [10_000, 100_000])]
85 fn bench_expand(bencher: Bencher, num_lines: usize) {
86 let data = text_data::generate_ascii_data(num_lines);
87 let temp_dir = tempfile::tempdir().unwrap();
88 let file_path = create_test_file(&data, temp_dir.path());
89
90 bencher.bench(|| {
91 black_box(run_util_function(uumain, &[file_path.to_str().unwrap()]));
92 });
93 }
94
95 fn main() {
96 divan::main();
97 }
98 ```
99
100 Common helpers include `text_data::generate_*()` for test data and `fs_tree::create_*()` for directory structures.
101
102 ## Using Samply for Profiling
103
104 [Samply](https://github.com/mstange/samply) is a sampling profiler that helps you identify performance bottlenecks in your code.
105
106 ### Basic Profiling
107
108 ```bash
109 # Generate a flame graph for your application
110 samply record ./target/debug/coreutils ls -R
111
112 # Profile with higher sampling frequency
113 samply record --rate 1000 ./target/debug/coreutils seq 1 1000
114 ```
115
116 The output using the `debug` profile might be easier to understand, but the performance characteristics may be somewhat different from `release` profile that we _actually_ care about.
117
118 Consider using the `profiling` profile, that compiles in `release` mode but with debug symbols. For example:
119 ```bash
120 cargo build --profile profiling -p uu_ls
121 samply record -r 10000 target/profiling/ls -lR /var .git .git .git > /dev/null
122 ```
123
124 ## Workflow: Measuring Performance Improvements
125
126 1. **Establish baselines**:
127 ```bash
128 hyperfine --warmup 3 \
129 "/usr/bin/sort large_file.txt" \
130 "our-sort-v1 large_file.txt"
131 ```
132
133 2. **Identify bottlenecks**:
134 ```bash
135 samply record ./our-sort-v1 large_file.txt
136 ```
137
138 3. **Make targeted improvements** based on profiling data
139
140 4. **Verify improvements**:
141 ```bash
142 hyperfine --warmup 3 \
143 "/usr/bin/sort large_file.txt" \
144 "our-sort-v1 large_file.txt" \
145 "our-sort-v2 large_file.txt"
146 ```
147
148 5. **Document performance changes** with concrete numbers
149 ```bash
150 hyperfine --export-markdown file.md [...]
151 ```
152
153 ## Profile-Guided Optimization
154
155 Our released binaries are built with PGO, driven by `util/build-pgo.sh` (Linux,
156 macOS and Windows alike). When comparing against a release build, keep in mind
157 that a plain `cargo build --release` is *not* what we ship. To reproduce it:
158
159 ```bash
160 rustup component add llvm-tools
161 ./util/build-pgo.sh --features unix
162 # -> target/coreutils-pgo/<target>/release/coreutils
163 ```
164
165 See [packaging](packaging.md#profile-guided-optimization-pgo) for the details
166 and the options.
167
168 How much it buys varies a lot by utility. Measured on x86_64 with `hyperfine`
169 against a plain `cargo build --release`, the text-processing utilities gain the
170 most, while utilities dominated by syscalls or by a single hand-tuned loop
171 barely move:
172
173 | Workload | Change |
174 | -------- | ------ |
175 | `cat -n`, `uniq -c`, `nl`, `fold -w` | -22% to -29% |
176 | `sort`, `sort -n` | -17% |
177 | `wc`, `sort -k` | -11% to -15% |
178 | `ls -lR`, `head` | -4% to -8% |
179 | `cut`, `seq`, `sha256sum`, `base64`, process startup | no change |
180
181 Two things to keep in mind when benchmarking a PGO build:
182
183 - The workloads in `util/build-pgo.sh` are what the profile is trained on. A
184 utility or a mode that is not exercised there gets no benefit, and measuring
185 one tells you little about the utilities that are.
186 - Wall-clock and instruction counts can disagree, since PGO changes code layout
187 as well as the code itself. `wc` gets 15% faster while executing *more*
188 instructions. If you use `valgrind --tool=cachegrind` for a noise-free
189 comparison, confirm the result with `hyperfine` before believing it.