interplane.Get Omega
Read in your language

synapse

A component of the AienOS ecosystem.

Brought by drakestapleton

branch main Branches Tags History

synapse / crates / synapse-bench / src / lib.rs

122 lines · 4441 bytes Raw Blame History
1//! Gate 4 comparative qualification harness for Synapse ([aien-sovereign-core#515]).
2//!
3//! The harness measures Synapse changeset ingest and checkout-free queries against
4//! conventional Git workflows on a pinned, real repository fixture, runs neutral
5//! conformance suites (WAL recovery, Git materialization, Radicle canonicality), and
6//! records raw per-operation traces plus a host and toolchain manifest.
7//!
8//! It reports measurements only. No module contains an adoption verdict, and no test
9//! asserts that one arm is faster than another: tests assert correctness only.
10//!
11//! Process model: the `run` orchestrator ([`orchestrator::run_qualification`]) spawns
12//! one child process per measurement cell (`synapse-bench child ...`) so that CPU time
13//! (`getrusage` self plus waited children) and peak RSS are attributed per arm.
14//!
15//! [aien-sovereign-core#515]: https://github.com/aien-spark/aien-sovereign-core/issues/515
16
17pub mod bounds;
18pub mod c1;
19pub mod conflict;
20pub mod conformance;
21pub mod crypto_vectors;
22pub mod energy;
23pub mod fixture;
24pub mod floor;
25pub mod git_arm;
26pub mod gitcmd;
27pub mod host;
28pub mod merge_t1;
29pub mod orchestrator;
30pub mod pmu;
31pub mod pmu_diag;
32pub mod query;
33pub mod recovery;
34pub mod rng;
35pub mod rusage;
36pub mod segments;
37pub mod stats;
38pub mod synapse_arm;
39pub mod tree_parity;
40pub mod workload;
41pub mod write_phase;
42pub mod x6;
43
44use std::path::Path;
45
46/// Write `value` as pretty JSON to `path`, creating parent directories.
47pub fn write_json<T: serde::Serialize>(path: &Path, value: &T) -> Result<(), String> {
48 if let Some(parent) = path.parent() {
49 std::fs::create_dir_all(parent).map_err(|e| format!("create {}: {e}", parent.display()))?;
50 }
51 let bytes = serde_json::to_vec_pretty(value)
52 .map_err(|e| format!("serialize {}: {e}", path.display()))?;
53 std::fs::write(path, bytes).map_err(|e| format!("write {}: {e}", path.display()))
54}
55
56/// Read and deserialize a JSON file.
57pub fn read_json<T: serde::de::DeserializeOwned>(path: &Path) -> Result<T, String> {
58 let bytes = std::fs::read(path).map_err(|e| format!("read {}: {e}", path.display()))?;
59 serde_json::from_slice(&bytes).map_err(|e| format!("parse {}: {e}", path.display()))
60}
61
62/// Write one JSON object per line (JSONL) to `path`, creating parent directories.
63pub fn write_jsonl<T: serde::Serialize>(path: &Path, rows: &[T]) -> Result<(), String> {
64 if let Some(parent) = path.parent() {
65 std::fs::create_dir_all(parent).map_err(|e| format!("create {}: {e}", parent.display()))?;
66 }
67 let mut out = Vec::with_capacity(rows.len() * 160);
68 for row in rows {
69 serde_json::to_writer(&mut out, row)
70 .map_err(|e| format!("serialize trace row for {}: {e}", path.display()))?;
71 out.push(b'\n');
72 }
73 std::fs::write(path, out).map_err(|e| format!("write {}: {e}", path.display()))
74}
75
76/// Convert a path to UTF-8 for use as a command-line argument.
77pub fn path_str(path: &Path) -> Result<&str, String> {
78 path.to_str()
79 .ok_or_else(|| format!("path is not valid UTF-8: {}", path.display()))
80}
81
82/// Current wall-clock time in Unix milliseconds.
83pub fn unix_now_ms() -> u64 {
84 std::time::SystemTime::now()
85 .duration_since(std::time::UNIX_EPOCH)
86 .map(|d| d.as_millis() as u64)
87 .unwrap_or(0)
88}
89
90/// Current UTC timestamp formatted as `YYYY-MM-DDTHH:MM:SSZ` (civil-from-days, no external crate).
91pub fn utc_timestamp() -> String {
92 let secs = unix_now_ms() / 1000;
93 let days = (secs / 86_400) as i64;
94 let rem = secs % 86_400;
95 let (h, m, s) = (rem / 3600, (rem % 3600) / 60, rem % 60);
96 // Howard Hinnant's days-to-civil algorithm.
97 let z = days + 719_468;
98 let era = z.div_euclid(146_097);
99 let doe = z - era * 146_097;
100 let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
101 let y = yoe + era * 400;
102 let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
103 let mp = (5 * doy + 2) / 153;
104 let d = doy - (153 * mp + 2) / 5 + 1;
105 let mo = if mp < 10 { mp + 3 } else { mp - 9 };
106 let y = if mo <= 2 { y + 1 } else { y };
107 format!("{y:04}-{mo:02}-{d:02}T{h:02}:{m:02}:{s:02}Z")
108}
109
110#[cfg(test)]
111mod tests {
112 use super::*;
113
114 #[test]
115 fn utc_timestamp_has_iso_shape() {
116 let ts = utc_timestamp();
117 assert_eq!(ts.len(), 20);
118 assert!(ts.ends_with('Z'));
119 assert_eq!(&ts[4..5], "-");
120 assert_eq!(&ts[10..11], "T");
121 }
122}