Debian Codegraph @

src/uu/factor/BENCHMARKING.md

raw
1 # Benchmarking `factor`
2
3 <!-- spell-checker:ignore (names) Daniel Lemire * Lemire's ; (misc) nohz -->
4
5 The benchmarks for `factor` are located under `tests/benches/factor`
6 and can be invoked with `cargo bench` in that directory.
7
8 They are located outside the `uu_factor` crate, as they do not comply
9 with the project's minimum supported Rust version, *i.e.* may require
10 a newer version of `rustc`.
11
12 ## Microbenchmarking deterministic functions
13
14 We currently use [`criterion`] to benchmark deterministic functions,
15 such as `gcd` and `table::factor`.
16
17 However, microbenchmarks are by nature unstable: not only are they specific to
18 the hardware, operating system version, etc., but they are noisy and affected
19 by other tasks on the system (browser, compile jobs, etc.), which can cause
20 `criterion` to report spurious performance improvements and regressions.
21
22 This can be mitigated by getting as close to [idealized conditions][lemire]
23 as possible:
24
25 - minimize the amount of computation and I/O running concurrently to the
26 benchmark, *i.e.* close your browser and IM clients, don't compile at the
27 same time, etc. ;
28 - ensure the CPU's [frequency stays constant] during the benchmark ;
29 - [isolate a **physical** core], set it to `nohz_full`, and pin the benchmark
30 to it, so it won't be preempted in the middle of a measurement ;
31 - disable ASLR by running `setarch -R cargo bench`, so we can compare results
32 across multiple executions.
33
34 [`criterion`]: https://bheisler.github.io/criterion.rs/book/index.html
35 [lemire]: https://lemire.me/blog/2018/01/16/microbenchmarking-calls-for-idealized-conditions/
36 [isolate a **physical** core]: https://pyperf.readthedocs.io/en/latest/system.html#isolate-cpus-on-linux
37 [frequency stays constant]: ... <!-- ToDO -->
38
39 ### Guidance for designing microbenchmarks
40
41 *Note:* this guidance is specific to `factor` and takes its application domain
42 into account; do not expect it to generalize to other projects. It is based
43 on Daniel Lemire's [*Microbenchmarking calls for idealized conditions*][lemire],
44 which I recommend reading if you want to add benchmarks to `factor`.
45
46 1. Select a small, self-contained, deterministic component
47 (`gcd` and `table::factor` are good examples):
48
49 - no I/O or access to external data structures ;
50 - no call into other components ;
51 - behavior is deterministic: no RNG, no concurrency, ... ;
52 - the test's body is *fast* (~100ns for `gcd`, ~10µs for `factor::table`),
53 so each sample takes a very short time, minimizing variability and
54 maximizing the numbers of samples we can take in a given time.
55
56 1. Benchmarks are immutable (once merged in `uutils`)
57
58 Modifying a benchmark means previously-collected values cannot meaningfully
59 be compared, silently giving nonsensical results. If you must modify an
60 existing benchmark, rename it.
61
62 1. Test common cases
63
64 We are interested in overall performance, rather than specific edge-cases;
65 use **reproducibly-randomized inputs**, sampling from either all possible
66 input values or some subset of interest.
67
68 1. Use [`criterion`], `criterion::black_box`, ...
69
70 `criterion` isn't perfect, but it is also much better than ad-hoc
71 solutions in each benchmark.
72
73 ## Wishlist
74
75 ### Configurable statistical estimators
76
77 `criterion` always uses the arithmetic average as estimator; in microbenchmarks,
78 where the code under test is fully deterministic and the measurements are
79 subject to additive, positive noise, [the minimum is more appropriate][lemire].
80
81 ### CI & reproducible performance testing
82
83 Measuring performance on real hardware is important, as it relates directly
84 to what users of `factor` experience; however, such measurements are subject
85 to the constraints of the real-world, and aren't perfectly reproducible.
86 Moreover, the mitigation for it (described above) isn't achievable in
87 virtualized, multi-tenant environments such as CI.
88
89 Instead, we could run the microbenchmarks in a simulated CPU with [`cachegrind`],
90 measure execution “time” in that model (in CI), and use it to detect and report
91 performance improvements and regressions.
92
93 [`iai`] is an implementation of this idea for Rust.
94
95 [`cachegrind`]: https://www.valgrind.org/docs/manual/cg-manual.html
96 [`iai`]: https://bheisler.github.io/criterion.rs/book/iai/iai.html
97
98 ### Comparing randomized implementations across multiple inputs
99
100 `factor` is a challenging target for system benchmarks as it combines two
101 characteristics:
102
103 1. integer factoring algorithms are randomized, with large variance in
104 execution time ;
105
106 1. various inputs also have large differences in factoring time, that
107 corresponds to no natural, linear ordering of the inputs.
108
109 If (1) was untrue (i.e. if execution time wasn't random), we could faithfully
110 compare 2 implementations (2 successive versions, or `uutils` and GNU) using
111 a scatter plot, where each axis corresponds to the perf. of one implementation.
112
113 Similarly, without (2) we could plot numbers on the X axis and their factoring
114 time on the Y axis, using multiple lines for various quantiles. The large
115 differences in factoring times for successive numbers, mean that such a plot
116 would be unreadable.