1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! kevy-bench — a pure-Rust micro-benchmark harness. Zero dependencies.
//!
//! kevy is 0-dependency by policy, so we cannot pull `criterion`. This is the
//! self-hosted equivalent, used by two callers:
//!
//! * per-crate exploration benches (`examples/bench_*.rs`) — compare two
//! implementations head-to-head and print a speedup ratio. This ratio is the
//! evidence the dep/std self-host evaluation is gated on ("if the candidate
//! isn't ≥ N× the incumbent, don't self-host it").
//! * per-crate regression gates (`tests/perf_gate.rs`) — assert a hot path stays
//! under a documented budget (see [`time_median`]).
//!
//! # Why the ratio survives a loaded host
//!
//! The campaign that birthed this crate could not get a clean full-system
//! throughput number because the dev host is permanently busy. The escape is to
//! measure *components*, not the whole server: run the variants **back-to-back
//! in one process**. Both pay the same contention in the same window, so the
//! absolute ns drifts with load but the **ratio between them holds**. Treat
//! `median_ns` as relative, not absolute, unless the host is known idle.
//!
//! # Method
//!
//! Each *sample* times `inner` iterations of the closure and divides, so the
//! per-op figure isn't swamped by `Instant::now()` overhead (~tens of ns) for
//! sub-microsecond work. We report the **median** sample (robust to the
//! occasional scheduler hiccup) plus p95 and min.
//!
//! ```
//! use kevy_bench::{bench, black_box};
//!
//! let s = bench(50, 1000, || { black_box(1u64 + black_box(2)); });
//! assert!(s.median_ns < 1_000); // trivial add is nanoseconds
//! ```
use ;
/// Re-exported so benches can fence the optimizer without importing `std::hint`.
pub use black_box;
/// Per-operation timing summary, in nanoseconds. See [`bench()`].
/// # Examples
///
/// ```
/// let s = kevy_bench::bench(5, 100, || { std::hint::black_box(1u64 + 1); });
/// assert_eq!((s.samples, s.inner), (5, 100));
/// // The order the summary always holds, whatever the machine did.
/// assert!(s.min_ns <= s.median_ns && s.median_ns <= s.p95_ns);
/// ```
/// Run `op` and return per-operation timing stats.
///
/// `samples` outer repetitions, each timing `inner` calls of `op` and dividing.
/// One untimed warm-up sample primes caches/branch predictors first. Pick
/// `inner` large enough that one sample is comfortably above `Instant`
/// resolution (≥ a few µs); for ns-scale ops use `inner` in the thousands.
/// # Examples
///
/// ```
/// // `inner` is the divisor: each sample times `inner` iterations and the
/// // reported figure is per-iteration, so a cheap op is still measurable.
/// let s = kevy_bench::bench(3, 1_000, || { std::hint::black_box(0u8); });
/// assert_eq!(s.samples, 3);
/// assert_eq!(s.inner, 1_000);
/// ```
/// Median wall-clock time of a single `op` call over `iters` runs.
///
/// The shape `tests/perf_gate.rs` wants: time each call individually, take the
/// median, assert it under a budget. Budgets should carry generous headroom —
/// dev (unoptimised) is typically 5–25× slower than release, and a loaded host
/// adds more — so size the budget off the *release* number times a safety factor
/// and document the observed dev figure alongside it.
/// # Examples
///
/// ```
/// use std::time::Duration;
/// // The median of `iters` whole-op timings — for one expensive thing,
/// // where `bench`'s inner loop would be the wrong shape.
/// let d = kevy_bench::time_median(3, || std::thread::sleep(Duration::from_millis(1)));
/// assert!(d >= Duration::from_millis(1));
/// ```
/// Print one labelled timing line.
///
/// # Examples
///
/// ```
/// let s = kevy_bench::bench(3, 100, || { std::hint::black_box(1u32); });
/// kevy_bench::report("noop", s); // median / p95 / min, one line
/// ```
/// Print both timings and the candidate's speedup over the baseline (by median).
///
/// `ratio = baseline.median / candidate.median`; > 1 means the candidate is
/// faster. This is the number the self-host decision is gated on.
/// # Examples
///
/// ```
/// let a = kevy_bench::bench(3, 100, || { std::hint::black_box(1u32); });
/// let b = kevy_bench::bench(3, 100, || { std::hint::black_box(1u32); });
/// let ratio = kevy_bench::compare("base", a, "cand", b);
/// // > 1 means the candidate is faster; two runs of the same work sit
/// // near 1, and how near is the noise band that any claim must clear.
/// assert!(ratio > 0.0 && ratio.is_finite());
/// ```