Skip to main content

quantik_core/bench/
bundle.rs

1//! Reproducible benchmark result bundles (port of `benchmarks/bundle.py`).
2
3use serde_json::{json, Map, Value};
4use std::path::Path;
5use std::process::Command;
6
7pub const SCHEMA_VERSION: u64 = 1;
8
9fn git_sha() -> String {
10    Command::new("git")
11        .args(["rev-parse", "HEAD"])
12        .output()
13        .ok()
14        .filter(|out| out.status.success())
15        .map(|out| String::from_utf8_lossy(&out.stdout).trim().to_string())
16        .unwrap_or_else(|| "unknown".into())
17}
18
19/// Actual compiler version used at run time, e.g. `1.82.0` — mirrors the
20/// Python `sys.version.split()[0]` bare-version convention.
21/// `CARGO_PKG_RUST_VERSION` is the crate's declared MSRV, not the toolchain
22/// actually compiling it, and is an empty string (not absent) when the
23/// `rust-version` Cargo.toml field is unset, so it is not usable here.
24fn rustc_version() -> String {
25    Command::new("rustc")
26        .arg("--version")
27        .output()
28        .ok()
29        .filter(|out| out.status.success())
30        .and_then(|out| {
31            String::from_utf8_lossy(&out.stdout)
32                .split_whitespace()
33                .nth(1)
34                .map(str::to_string)
35        })
36        .unwrap_or_else(|| "unknown".into())
37}
38
39/// Host and software fingerprint stored in result bundles. Mirrors the
40/// Python `collect_environment`, with `rust_version` in place of
41/// `python_version`.
42pub fn collect_environment() -> Value {
43    json!({
44        "quantik_core_version": env!("CARGO_PKG_VERSION"),
45        "git_sha": git_sha(),
46        "rust_version": rustc_version(),
47        "platform": format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH),
48        "processor": std::env::consts::ARCH,
49        "cpu_count": std::thread::available_parallelism().map(|n| n.get()).unwrap_or(0),
50        "total_memory_bytes": Value::Null,
51    })
52}
53
54/// Assemble a JSON-serializable, self-describing benchmark result bundle.
55pub fn make_bundle(
56    config: Value,
57    dataset_payload: &Value,
58    observations: Vec<Value>,
59    head_to_head: Value,
60    aggregates: Value,
61) -> Value {
62    let positions = dataset_payload["positions"]
63        .as_array()
64        .cloned()
65        .unwrap_or_default();
66    let mut phases: Map<String, Value> = Map::new();
67    for position in &positions {
68        let phase = position["phase"].as_str().unwrap_or_default().to_string();
69        let count = phases.get(&phase).and_then(Value::as_u64).unwrap_or(0);
70        phases.insert(phase, json!(count + 1));
71    }
72
73    // Local time with UTC offset, `%Y-%m-%dT%H:%M:%S%z` like Python.
74    let started_at = chrono::Local::now()
75        .format("%Y-%m-%dT%H:%M:%S%z")
76        .to_string();
77
78    json!({
79        "schema_version": SCHEMA_VERSION,
80        "started_at": started_at,
81        "environment": collect_environment(),
82        "config": config,
83        "dataset": {
84            "checksum": dataset_payload.get("checksum").cloned().unwrap_or(Value::Null),
85            "generator": dataset_payload["generator"],
86            "seed": dataset_payload["seed"],
87            "schema_version": dataset_payload["schema_version"],
88            "positions": positions.len(),
89            "phases": phases,
90        },
91        "observations": observations,
92        "head_to_head": head_to_head,
93        "aggregates": aggregates,
94    })
95}
96
97/// Write a result bundle as JSON, creating parent directories as needed.
98pub fn save_bundle(bundle: &Value, path: &Path) -> Result<(), String> {
99    if let Some(parent) = path.parent() {
100        std::fs::create_dir_all(parent).map_err(|e| format!("mkdir {parent:?}: {e}"))?;
101    }
102    let text =
103        serde_json::to_string_pretty(bundle).map_err(|e| format!("serialize bundle: {e}"))?;
104    std::fs::write(path, text + "\n").map_err(|e| format!("write {path:?}: {e}"))
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn bundle_shape_and_save() {
113        let dataset = json!({
114            "checksum": "abc",
115            "generator": "g/v1",
116            "seed": 1,
117            "schema_version": 1,
118            "positions": [
119                {"phase": "opening"}, {"phase": "opening"}, {"phase": "endgame"},
120            ],
121        });
122        let bundle = make_bundle(
123            json!({"family": "fixed"}),
124            &dataset,
125            vec![json!({"engine": "random"})],
126            json!({"records": [], "aggregates": []}),
127            json!({"agreement": [], "cost": [], "stability": []}),
128        );
129        assert_eq!(bundle["schema_version"], json!(1));
130        assert_eq!(bundle["dataset"]["positions"], json!(3));
131        assert_eq!(bundle["dataset"]["phases"]["opening"], json!(2));
132        assert_eq!(bundle["config"]["family"], json!("fixed"));
133        assert_eq!(bundle["observations"].as_array().unwrap().len(), 1);
134        assert!(bundle["environment"]["quantik_core_version"].is_string());
135        // Timestamp format sanity: 2026-07-12T01:23:45+0200.
136        let ts = bundle["started_at"].as_str().unwrap();
137        assert_eq!(ts.len(), 24, "{ts}");
138        assert_eq!(&ts[4..5], "-");
139        assert_eq!(&ts[10..11], "T");
140
141        let dir = std::env::temp_dir().join(format!("quantik-bundle-{}", std::process::id()));
142        let path = dir.join("nested").join("bundle.json");
143        save_bundle(&bundle, &path).unwrap();
144        let loaded: Value = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
145        assert_eq!(loaded["dataset"]["checksum"], json!("abc"));
146        std::fs::remove_dir_all(&dir).ok();
147    }
148}