Skip to content
Myr

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

4 files+100 added−5 removed

added:src/export/json/metadata.rs+63 added
1use serde::Serialize;23pub(super) const JSON_SCHEMA_VERSION: u32 = 2;45#[derive(Debug, Serialize)]6pub(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}1314impl 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}2425fn utc_now() -> Option<String> {26 use std::{fmt::Write, time::SystemTime};2728 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}3435#[derive(Debug, Default, Serialize)]36pub(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}4243impl Platform {44 fn detect() -> Self {45 use platform_info::{PlatformInfo, PlatformInfoAPI, UNameAPI};4647 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}5657#[test]58fn 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 lines
7use crate::quantity::TimeUnit;89use anyhow::Result;1011mod metadata;12use metadata::Metadata;1314#[derive(Serialize, Debug)]15struct HyperfineSummary<'a> {13 schema_version: u32,16 metadata: &'a Metadata,17 results: &'a [BenchmarkResult],18}1920#[derive(Default)]18pub struct JsonExporter {}21pub struct JsonExporter {22 metadata: Metadata,23}2425impl Exporter for JsonExporter {26 fn serialize(27 &self,
2 unmodified lines
30 _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 lines
66 ..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 lines
modified:Cargo.lock+18 added
368 unmodified lines
369source = "registry+https://github.com/rust-lang/crates.io-index"370checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"371372[[package]]373name = "humantime"374version = "2.4.0"375source = "registry+https://github.com/rust-lang/crates.io-index"376checksum = "15cdd26707701c53297e2fa6afb323d55fbc1d0810c3aec078ae3ef0424c3c15"377378[[package]]379name = "hyperfine"380version = "1.21.0"381dependencies = [382 "anyhow",
2 unmodified lines
385 "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 lines
539source = "registry+https://github.com/rust-lang/crates.io-index"540checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"541542[[package]]543name = "platform-info"544version = "2.2.0"545source = "registry+https://github.com/rust-lang/crates.io-index"546checksum = "ba7c3f5af00d3596a148cc80178ac690152b71c283d15a8bcea0cddfea759a36"547dependencies = [548 "libc",549 "windows-sys 0.61.2",550]551552[[package]]553name = "portable-atomic"554version = "1.15.0"555source = "registry+https://github.com/rust-lang/crates.io-index"556checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85"
644 unmodified lines
modified:Cargo.toml+2 added