Add benchmark session metadata to JSON exports
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent d60eba4 · Browse files · 4 files·+100 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+100 added−5 removed
- other:+100 added−5 removed
Provenance
- Commit
6d0ae4f- Tree
cc8befb- Author
David Peter <mail@david-peter.de>- Committer
David Peter <mail@david-peter.de>- Parent
d60eba4
4 files changed+100 added−5 removed
added:src/export/json/metadata.rs+63 added
1+use serde::Serialize;2+3+pub(super) const JSON_SCHEMA_VERSION: u32 = 2;4+5+#[derive(Debug, Serialize)]6+pub(super) struct Metadata {7+ json_schema_version: u32,8+ hyperfine_version: &'static str,9+ #[serde(skip_serializing_if = "Option::is_none")]10+ pub start_time: Option<String>,11+ pub platform: Platform,12+}13+14+impl Default for Metadata {15+ fn default() -> Self {16+ Self {17+ json_schema_version: JSON_SCHEMA_VERSION,18+ hyperfine_version: env!("CARGO_PKG_VERSION"),19+ start_time: utc_now(),20+ platform: Platform::detect(),21+ }22+ }23+}24+25+fn utc_now() -> Option<String> {26+ use std::{fmt::Write, time::SystemTime};27+28+ let now = SystemTime::now();29+ now.duration_since(SystemTime::UNIX_EPOCH).ok()?;30+ let mut timestamp = String::new();31+ write!(&mut timestamp, "{}", humantime::format_rfc3339_seconds(now)).ok()?;32+ Some(timestamp)33+}34+35+#[derive(Debug, Default, Serialize)]36+pub(super) struct Platform {37+ #[serde(skip_serializing_if = "Option::is_none")]38+ os: Option<String>,39+ #[serde(skip_serializing_if = "Option::is_none")]40+ architecture: Option<String>,41+}42+43+impl Platform {44+ fn detect() -> Self {45+ use platform_info::{PlatformInfo, PlatformInfoAPI, UNameAPI};46+47+ let Ok(info) = PlatformInfo::new() else {48+ return Self::default();49+ };50+ Self {51+ os: Some(info.sysname().to_string_lossy().into_owned()),52+ architecture: Some(info.machine().to_string_lossy().into_owned()),53+ }54+ }55+}56+57+#[test]58+fn unavailable_platform_fields_are_omitted() {59+ assert_eq!(60+ serde_json::to_value(Platform::default()).unwrap(),61+ serde_json::json!({})62+ );63+}modified:src/export/json.rs+17 added-5 removed
6 unmodified lines7 use crate::quantity::TimeUnit;8 9 use anyhow::Result;10 11+mod metadata;12+use metadata::Metadata;13+14 #[derive(Serialize, Debug)]15 struct HyperfineSummary<'a> {13- schema_version: u32,16+ metadata: &'a Metadata,17 results: &'a [BenchmarkResult],18 }19 20 #[derive(Default)]18-pub struct JsonExporter {}21+pub struct JsonExporter {22+ metadata: Metadata,23+}24 25 impl Exporter for JsonExporter {26 fn serialize(27 &self,2 unmodified lines30 _sort_order: SortOrder,31 _reference_index: Option<usize>,32 ) -> Result<Vec<u8>> {33 let mut output = to_vec_pretty(&HyperfineSummary {29- schema_version: 2,34+ metadata: &self.metadata,35 results,36 });37 if let Ok(ref mut content) = output {38 content.push(b'\n');27 unmodified lines66 ..Measurement::default()67 }]),68 ..BenchmarkResult::default()69 };65- let output = JsonExporter::default()70+ let mut exporter = JsonExporter::default();71+ exporter.metadata.start_time = Some("2000-02-29T12:34:56Z".to_owned());72+ let output = exporter73 .serialize(&[result], None, SortOrder::Command, None)74 .unwrap();75 let actual: serde_json::Value = serde_json::from_slice(&output).unwrap();76 let mut expected = json!({70- "schema_version": 2,77+ "metadata": {78+ "json_schema_version": metadata::JSON_SCHEMA_VERSION,79+ "hyperfine_version": env!("CARGO_PKG_VERSION"),80+ "start_time": "2000-02-29T12:34:56Z",81+ "platform": exporter.metadata.platform82+ },83 "results": [{84 "command": "example",85 "measurements": [{86 "time_wall_clock": {"value": 0.0, "unit": "second"},12 unmodified linesmodified:Cargo.lock+18 added
368 unmodified lines369 source = "registry+https://github.com/rust-lang/crates.io-index"370 checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"371 372 [[package]]373+name = "humantime"374+version = "2.4.0"375+source = "registry+https://github.com/rust-lang/crates.io-index"376+checksum = "15cdd26707701c53297e2fa6afb323d55fbc1d0810c3aec078ae3ef0424c3c15"377+378+[[package]]379 name = "hyperfine"380 version = "1.21.0"381 dependencies = [382 "anyhow",2 unmodified lines385 "clap",386 "clap_complete",387 "colored",388 "csv",389+ "humantime",390 "indicatif",391 "insta",392 "insta-cmd",393 "libc",394 "nix",395 "once_cell",396+ "platform-info",397 "predicates",398 "rand 0.10.3",399 "rust_decimal",400 "serde",138 unmodified lines539 source = "registry+https://github.com/rust-lang/crates.io-index"540 checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"541 542 [[package]]543+name = "platform-info"544+version = "2.2.0"545+source = "registry+https://github.com/rust-lang/crates.io-index"546+checksum = "ba7c3f5af00d3596a148cc80178ac690152b71c283d15a8bcea0cddfea759a36"547+dependencies = [548+ "libc",549+ "windows-sys 0.61.2",550+]551+552+[[package]]553 name = "portable-atomic"554 version = "1.15.0"555 source = "registry+https://github.com/rust-lang/crates.io-index"556 checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"644 unmodified linesmodified:Cargo.toml+2 added
27 unmodified lines2829303132333435363748 unmodified lines27 unmodified linesshell-words = "1.1"thiserror = "2.0"anyhow = "1.0"uom = { version = "0.36.0", default-features = false, features = ["std", "si", "f64"] }humantime = "2"platform-info = "2.2"
[target.'cfg(not(windows))'.dependencies]libc = "0.2"
48 unmodified lines