Document JSON measurements and omit unavailable memory metrics

This commit is contained in:
David Peter committed 2026-10-05 16:01:30 +02:00
1 parent 21bbcc5490
commit 4cd0e24586
9 files changed
+147 -44

No files matched your search

+47
View File
@@ -32,3 +32,50 @@ impl Exporter for JsonExporter {
Ok(output?)
}
}
#[test]
fn test_json_optional_memory() {
use crate::benchmark::measurement::{Measurement, Measurements};
use crate::quantity::{byte, Information};
use serde_json::json;
for (memory, expected_memory) in [
(None, None),
(
Some(Information::new::<byte>(0.0)),
Some(json!({"value": 0.0, "unit": "byte"})),
),
(
Some(Information::new::<byte>(1024.0)),
Some(json!({"value": 1024.0, "unit": "byte"})),
),
] {
let result = BenchmarkResult {
command: "example".into(),
measurements: Measurements::new(vec![Measurement {
peak_memory_usage: memory,
..Measurement::default()
}]),
..BenchmarkResult::default()
};
let output = JsonExporter::default()
.serialize(&[result], None, SortOrder::Command)
.unwrap();
let actual: serde_json::Value = serde_json::from_slice(&output).unwrap();
let mut expected = json!({
"results": [{
"command": "example",
"measurements": [{
"time_wall_clock": {"value": 0.0, "unit": "second"},
"time_user": {"value": 0.0, "unit": "second"},
"time_system": {"value": 0.0, "unit": "second"},
"exit_code": 0
}]
}]
});
if let Some(memory) = expected_memory {
expected["results"][0]["measurements"][0]["peak_memory_usage"] = memory;
}
assert_eq!(actual, expected);
}
}