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
//! Shared bench helpers (M14 #119).
//!
//! The five existing benches under `crates/obj/benches/` each inline
//! the same tempdir + open-Db boilerplate plus a near-identical
//! `OBJ_BENCH_ENFORCE=1` env-var probe. This module consolidates the
//! duplicated parts so the new `perf_table` bench (and the existing
//! ones) call into one definition.
//!
//! Each `benches/*.rs` is its own independent binary crate per
//! Cargo's bench layout, so this module is compiled separately per
//! consumer. Different consumers exercise different helpers; the
//! `#![allow(dead_code)]` below keeps unused-fn warnings from firing
//! on the consumers that only need a subset.
//!
//! Power-of-ten posture:
//! - Rule 2: every helper that takes a count argument is bounded by
//! that argument; nothing here loops without a caller-supplied cap.
//! - Rule 4: each helper is ≤ 60 lines.
//! - Rule 7: every `Result` is propagated or `expect`-ed with a
//! diagnostic message — bench code is allowed to panic on setup
//! failure (it is not a production path).
// Each bench binary picks a different subset of these helpers; the
// unused ones would otherwise warn under workspace `-D warnings`.
use PathBuf;
use Db;
use TempDir;
/// RAII wrapper around a `(TempDir, Db)` pair so the caller can hold
/// onto the database without juggling two bindings; dropping the
/// wrapper drops the `Db` first (releasing file locks) and then the
/// `TempDir` (which deletes the on-disk file).
///
/// Field order matters: `Db` must drop before `TempDir` so the file
/// handles release their advisory locks before the directory is
/// torn down. Rust drops struct fields in declaration order.
/// Open a fresh on-disk `Db` inside a tempdir. The bench owns the
/// returned `BenchDb`; dropping it deletes the underlying file.
///
/// `name` is used for the database file basename so multi-bench
/// runs (e.g. `cargo bench` over the whole crate) don't collide on
/// criterion's output directory listings.
///
/// Panics on tempdir / open failure: bench setup is not a production
/// path. Power-of-ten Rule 7: documented `.expect` with diagnostic.
/// Drop the supplied `Db` and re-open the same path. Forces the WAL
/// to flush via the open-time recovery checkpoint — the resulting
/// `Db` sees a quiescent file with no in-flight WAL frames.
///
/// Caller passes the `BenchDb` by value; the returned `BenchDb`
/// shares the same `TempDir` so the file path survives the
/// drop/reopen cycle. Power-of-ten Rule 4: single responsibility.
/// `OBJ_BENCH_ENFORCE=1` (or any non-empty, non-"0" value) upgrades
/// each bench's informational target check to a hard failure. CI's
/// `bench.yml` workflow sets this on the Linux x86-64 runner; local
/// `cargo bench` keeps the informational mode by default.
///
/// Power-of-ten Rule 7: the `Result` from `env::var` is filtered
/// explicitly — neither absence nor an empty value should enable
/// the gate.