Skip to content
Myr

Cover JSON exports with integration snapshots

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 cb021e6 · Browse files · 6 files+557 added−264 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+557 added−264 removed

  • tests:+557 added−80 removed
  • other:+0 added−184 removed

6 files+557 added−264 removed

modified:src/export/json/metadata.rs-8 removed
47 unmodified lines
48 architecture: Some(info.machine().to_string_lossy().into_owned()),49 }50 }51}5253#[test]54fn unavailable_platform_fields_are_omitted() {55 assert_eq!(56 serde_json::to_value(Platform::default()).unwrap(),57 serde_json::json!({})58 );59}
modified:src/export/json.rs-176 removed
171 unmodified lines
172 approx::assert_relative_eq!(summary.max, 3.0 * scale);173 approx::assert_relative_eq!(summary.stddev.unwrap(), 2.0_f64.sqrt() * scale);174 }175}176177#[test]178fn test_json_optional_memory() {179 use crate::benchmark::measurement::{Measurement, Measurements};180 use crate::quantity::{byte, Information};181 use serde_json::json;182183 for (memory, expected_memory) in [184 (None, None),185 (186 Some(Information::new::<byte>(0.0)),187 Some(json!({"value": 0.0, "unit": "byte"})),188 ),189 (190 Some(Information::new::<byte>(1024.0)),191 Some(json!({"value": 1024.0, "unit": "byte"})),192 ),193 ] {194 let result = BenchmarkResult {195 command: "example".into(),196 name: None,197 measurements: Measurements::new(vec![Measurement {198 memory_peak_resident: memory,199 ..Measurement::default()200 }]),201 ..BenchmarkResult::default()202 };203 let mut exporter = JsonExporter::default();204 exporter.metadata.start_time = Some("2000-02-29T12:34:56Z".to_owned());205 let output = exporter206 .serialize(&[result], None, SortOrder::Command, None)207 .unwrap();208 let actual: serde_json::Value = serde_json::from_slice(&output).unwrap();209 let mut expected = json!({210 "schema_version": JSON_SCHEMA_VERSION,211 "metadata": {212 "hyperfine_version": env!("CARGO_PKG_VERSION"),213 "start_time": "2000-02-29T12:34:56Z",214 "platform": exporter.metadata.platform215 },216 "results": [{217 "command": "example",218 "summary": {219 "time_wall_clock": {220 "unit": "second",221 "count": 1,222 "mean": 0.0,223 "stddev": null,224 "median": 0.0,225 "min": 0.0,226 "max": 0.0227 }228 },229 "measurements": [{230 "time_wall_clock": {"value": 0.0, "unit": "second"},231 "time_user": {"value": 0.0, "unit": "second"},232 "time_system": {"value": 0.0, "unit": "second"},233 "exit_code": 0234 }]235 }]236 });237 for field in ["time_user", "time_system"] {238 expected["results"][0]["summary"][field] =239 expected["results"][0]["summary"]["time_wall_clock"].clone();240 }241 if let Some(memory) = expected_memory {242 let value = memory["value"].clone();243 expected["results"][0]["summary"]["memory_peak_resident"] = json!({244 "unit": "byte", "count": 1, "mean": value,245 "stddev": null, "median": value, "min": value, "max": value246 });247 expected["results"][0]["measurements"][0]["memory_peak_resident"] = memory;248 }249 assert_eq!(actual, expected);250 }251}252253#[test]254fn test_json_summaries_for_parameterized_results() {255 use crate::benchmark::benchmark_result::Parameter;256 use crate::benchmark::measurement::Measurement;257 use crate::quantity::{second, Time};258 use serde_json::json;259260 let results: Vec<_> = vec![("small", vec![1.0, 3.0]), ("large", vec![10.0])]261 .into_iter()262 .map(|(size, times)| BenchmarkResult {263 command: "example".into(),264 name: None,265 parameters: [(266 "size".into(),267 Parameter {268 value: size.into(),269 is_unused: size == "small",270 },271 )]272 .into(),273 measurements: Measurements::new(274 times275 .into_iter()276 .map(|time| Measurement {277 time_wall_clock: Time::new::<second>(time),278 ..Measurement::default()279 })280 .collect(),281 ),282 })283 .collect();284 let output = JsonExporter::default()285 .serialize(&results, None, SortOrder::Command, None)286 .unwrap();287 let actual: serde_json::Value = serde_json::from_slice(&output).unwrap();288 let results = actual["results"].as_array().unwrap();289 assert_eq!(results.len(), 2);290 assert_eq!(results[0]["parameters"]["size"], json!({"value": "small"}));291 assert_eq!(results[1]["parameters"]["size"], json!({"value": "large"}));292 assert_eq!(results[0]["measurements"].as_array().unwrap().len(), 2);293 assert_eq!(results[1]["measurements"].as_array().unwrap().len(), 1);294 assert_eq!(295 results[0]["summary"]["time_wall_clock"],296 json!({297 "unit": "second", "count": 2, "mean": 2.0,298 "stddev": 2.0_f64.sqrt(), "median": 2.0, "min": 1.0, "max": 3.0299 })300 );301 assert_eq!(302 results[1]["summary"]["time_wall_clock"],303 json!({304 "unit": "second", "count": 1, "mean": 10.0,305 "stddev": null, "median": 10.0, "min": 10.0, "max": 10.0306 })307 );308}309310#[test]311fn test_json_summary_metrics_and_missing_samples() {312 use crate::benchmark::measurement::Measurement;313 use crate::quantity::{byte, second, Information, Time};314 use serde_json::json;315316 let measurements = Measurements::new(317 vec![(1.0, Some(1024.0)), (2.0, None), (3.0, Some(3072.0))]318 .into_iter()319 .map(|(time, memory)| Measurement {320 time_wall_clock: Time::new::<second>(time),321 time_user: Time::new::<second>(time * 2.0),322 time_system: Time::new::<second>(time * 3.0),323 memory_peak_resident: memory.map(Information::new::<byte>),324 ..Measurement::default()325 })326 .collect(),327 );328 let actual = serde_json::to_value(BenchmarkSummary::from_measurements(&measurements)).unwrap();329 assert_eq!(330 actual,331 json!({332 "time_wall_clock": {333 "unit": "second", "count": 3, "mean": 2.0, "stddev": 1.0,334 "median": 2.0, "min": 1.0, "max": 3.0335 },336 "time_user": {337 "unit": "second", "count": 3, "mean": 4.0, "stddev": 2.0,338 "median": 4.0, "min": 2.0, "max": 6.0339 },340 "time_system": {341 "unit": "second", "count": 3, "mean": 6.0, "stddev": 3.0,342 "median": 6.0, "min": 3.0, "max": 9.0343 },344 "memory_peak_resident": {345 "unit": "byte", "count": 2, "mean": 2048.0,346 "stddev": 1024.0 * 2.0_f64.sqrt(),347 "median": 2048.0, "min": 1024.0, "max": 3072.0348 }349 })350 );351}
added:tests/snapshots/integration_tests__json_export_basic.snap+199 added
1---2source: tests/integration_tests.rs3info:4 program: hyperfine5 args:6 - "--debug-mode"7 - "--style=none"8 - "--export-json=-"9 - "--runs=2"10 - "--warmup=1"11 - "--reference-name=one second"12 - "--reference=sleep 1"13 - "--command-name=sleep 2"14 - sleep 215---16success: true17exit_code: 018----- stdout -----1920{21 "schema_version": 2,22 "metadata": {23 "hyperfine_version": "[hyperfine_version]",24 "start_time": "[start_time]",25 "platform": {26 "os": "[os]",27 "architecture": "[architecture]"28 }29 },30 "results": [31 {32 "command": "sleep 1",33 "name": "one second",34 "measurements": [35 {36 "time_wall_clock": {37 "value": 1.0,38 "unit": "second"39 },40 "time_user": {41 "value": 0.0,42 "unit": "second"43 },44 "time_system": {45 "value": 0.0,46 "unit": "second"47 },48 "memory_peak_resident": {49 "value": 0.0,50 "unit": "byte"51 },52 "exit_code": 053 },54 {55 "time_wall_clock": {56 "value": 1.0,57 "unit": "second"58 },59 "time_user": {60 "value": 0.0,61 "unit": "second"62 },63 "time_system": {64 "value": 0.0,65 "unit": "second"66 },67 "memory_peak_resident": {68 "value": 0.0,69 "unit": "byte"70 },71 "exit_code": 072 }73 ],74 "is_reference": true,75 "summary": {76 "time_wall_clock": {77 "unit": "second",78 "count": 2,79 "mean": 1.0,80 "stddev": 0.0,81 "median": 1.0,82 "min": 1.0,83 "max": 1.084 },85 "time_user": {86 "unit": "second",87 "count": 2,88 "mean": 0.0,89 "stddev": 0.0,90 "median": 0.0,91 "min": 0.0,92 "max": 0.093 },94 "time_system": {95 "unit": "second",96 "count": 2,97 "mean": 0.0,98 "stddev": 0.0,99 "median": 0.0,100 "min": 0.0,101 "max": 0.0102 },103 "memory_peak_resident": {104 "unit": "byte",105 "count": 2,106 "mean": 0.0,107 "stddev": 0.0,108 "median": 0.0,109 "min": 0.0,110 "max": 0.0111 }112 }113 },114 {115 "command": "sleep 2",116 "measurements": [117 {118 "time_wall_clock": {119 "value": 2.0,120 "unit": "second"121 },122 "time_user": {123 "value": 0.0,124 "unit": "second"125 },126 "time_system": {127 "value": 0.0,128 "unit": "second"129 },130 "memory_peak_resident": {131 "value": 0.0,132 "unit": "byte"133 },134 "exit_code": 0135 },136 {137 "time_wall_clock": {138 "value": 2.0,139 "unit": "second"140 },141 "time_user": {142 "value": 0.0,143 "unit": "second"144 },145 "time_system": {146 "value": 0.0,147 "unit": "second"148 },149 "memory_peak_resident": {150 "value": 0.0,151 "unit": "byte"152 },153 "exit_code": 0154 }155 ],156 "summary": {157 "time_wall_clock": {158 "unit": "second",159 "count": 2,160 "mean": 2.0,161 "stddev": 0.0,162 "median": 2.0,163 "min": 2.0,164 "max": 2.0165 },166 "time_user": {167 "unit": "second",168 "count": 2,169 "mean": 0.0,170 "stddev": 0.0,171 "median": 0.0,172 "min": 0.0,173 "max": 0.0174 },175 "time_system": {176 "unit": "second",177 "count": 2,178 "mean": 0.0,179 "stddev": 0.0,180 "median": 0.0,181 "min": 0.0,182 "max": 0.0183 },184 "memory_peak_resident": {185 "unit": "byte",186 "count": 2,187 "mean": 0.0,188 "stddev": 0.0,189 "median": 0.0,190 "min": 0.0,191 "max": 0.0192 }193 }194 }195 ]196}197198199----- stderr -----
added:tests/snapshots/integration_tests__json_export_parameterized_with_reference.snap+211 added
1---2source: tests/integration_tests.rs3info:4 program: hyperfine5 args:6 - "--debug-mode"7 - "--style=none"8 - "--export-json=-"9 - "--runs=2"10 - "-L"11 - duration12 - "1,2"13 - "--command-name=sleep for {duration} seconds"14 - "--reference=sleep for 2 seconds"15 - "sleep {duration}"16---17success: true18exit_code: 019----- stdout -----2021{22 "schema_version": 2,23 "metadata": {24 "hyperfine_version": "[hyperfine_version]",25 "start_time": "[start_time]",26 "platform": {27 "os": "[os]",28 "architecture": "[architecture]"29 }30 },31 "results": [32 {33 "command": "sleep 1",34 "name": "sleep for 1 seconds",35 "measurements": [36 {37 "time_wall_clock": {38 "value": 1.0,39 "unit": "second"40 },41 "time_user": {42 "value": 0.0,43 "unit": "second"44 },45 "time_system": {46 "value": 0.0,47 "unit": "second"48 },49 "memory_peak_resident": {50 "value": 0.0,51 "unit": "byte"52 },53 "exit_code": 054 },55 {56 "time_wall_clock": {57 "value": 1.0,58 "unit": "second"59 },60 "time_user": {61 "value": 0.0,62 "unit": "second"63 },64 "time_system": {65 "value": 0.0,66 "unit": "second"67 },68 "memory_peak_resident": {69 "value": 0.0,70 "unit": "byte"71 },72 "exit_code": 073 }74 ],75 "parameters": {76 "duration": {77 "value": "1"78 }79 },80 "summary": {81 "time_wall_clock": {82 "unit": "second",83 "count": 2,84 "mean": 1.0,85 "stddev": 0.0,86 "median": 1.0,87 "min": 1.0,88 "max": 1.089 },90 "time_user": {91 "unit": "second",92 "count": 2,93 "mean": 0.0,94 "stddev": 0.0,95 "median": 0.0,96 "min": 0.0,97 "max": 0.098 },99 "time_system": {100 "unit": "second",101 "count": 2,102 "mean": 0.0,103 "stddev": 0.0,104 "median": 0.0,105 "min": 0.0,106 "max": 0.0107 },108 "memory_peak_resident": {109 "unit": "byte",110 "count": 2,111 "mean": 0.0,112 "stddev": 0.0,113 "median": 0.0,114 "min": 0.0,115 "max": 0.0116 }117 }118 },119 {120 "command": "sleep 2",121 "name": "sleep for 2 seconds",122 "measurements": [123 {124 "time_wall_clock": {125 "value": 2.0,126 "unit": "second"127 },128 "time_user": {129 "value": 0.0,130 "unit": "second"131 },132 "time_system": {133 "value": 0.0,134 "unit": "second"135 },136 "memory_peak_resident": {137 "value": 0.0,138 "unit": "byte"139 },140 "exit_code": 0141 },142 {143 "time_wall_clock": {144 "value": 2.0,145 "unit": "second"146 },147 "time_user": {148 "value": 0.0,149 "unit": "second"150 },151 "time_system": {152 "value": 0.0,153 "unit": "second"154 },155 "memory_peak_resident": {156 "value": 0.0,157 "unit": "byte"158 },159 "exit_code": 0160 }161 ],162 "parameters": {163 "duration": {164 "value": "2"165 }166 },167 "is_reference": true,168 "summary": {169 "time_wall_clock": {170 "unit": "second",171 "count": 2,172 "mean": 2.0,173 "stddev": 0.0,174 "median": 2.0,175 "min": 2.0,176 "max": 2.0177 },178 "time_user": {179 "unit": "second",180 "count": 2,181 "mean": 0.0,182 "stddev": 0.0,183 "median": 0.0,184 "min": 0.0,185 "max": 0.0186 },187 "time_system": {188 "unit": "second",189 "count": 2,190 "mean": 0.0,191 "stddev": 0.0,192 "median": 0.0,193 "min": 0.0,194 "max": 0.0195 },196 "memory_peak_resident": {197 "unit": "byte",198 "count": 2,199 "mean": 0.0,200 "stddev": 0.0,201 "median": 0.0,202 "min": 0.0,203 "max": 0.0204 }205 }206 }207 ]208}209210211----- stderr -----
added:tests/snapshots/integration_tests__json_export_single_run.snap+93 added
1---2source: tests/integration_tests.rs3info:4 program: hyperfine5 args:6 - "--debug-mode"7 - "--style=none"8 - "--export-json=-"9 - "--runs=1"10 - sleep 111---12success: true13exit_code: 014----- stdout -----1516{17 "schema_version": 2,18 "metadata": {19 "hyperfine_version": "[hyperfine_version]",20 "start_time": "[start_time]",21 "platform": {22 "os": "[os]",23 "architecture": "[architecture]"24 }25 },26 "results": [27 {28 "command": "sleep 1",29 "measurements": [30 {31 "time_wall_clock": {32 "value": 1.0,33 "unit": "second"34 },35 "time_user": {36 "value": 0.0,37 "unit": "second"38 },39 "time_system": {40 "value": 0.0,41 "unit": "second"42 },43 "memory_peak_resident": {44 "value": 0.0,45 "unit": "byte"46 },47 "exit_code": 048 }49 ],50 "summary": {51 "time_wall_clock": {52 "unit": "second",53 "count": 1,54 "mean": 1.0,55 "stddev": null,56 "median": 1.0,57 "min": 1.0,58 "max": 1.059 },60 "time_user": {61 "unit": "second",62 "count": 1,63 "mean": 0.0,64 "stddev": null,65 "median": 0.0,66 "min": 0.0,67 "max": 0.068 },69 "time_system": {70 "unit": "second",71 "count": 1,72 "mean": 0.0,73 "stddev": null,74 "median": 0.0,75 "min": 0.0,76 "max": 0.077 },78 "memory_peak_resident": {79 "unit": "byte",80 "count": 1,81 "mean": 0.0,82 "stddev": null,83 "median": 0.0,84 "min": 0.0,85 "max": 0.086 }87 }88 }89 ]90}919293----- stderr -----
modified:tests/integration_tests.rs+54 added-80 removed
22 unmodified lines
23 settings.add_filter(r"(?m)[ \t]+$", "");24 settings25}2627fn json_snapshot_settings() -> insta::Settings {28 let mut settings = snapshot_settings();29 for field in ["hyperfine_version", "start_time", "os", "architecture"] {30 settings.add_filter(31 &format!(r#"("{field}": ")[^"]+(")"#),32 format!(r#"${{1}}[{field}]$2"#),33 );34 }35 settings36}3738#[test]39fn runs_successfully() {40 hyperfine()41 .arg("--runs=2")
10 unmodified lines
52 .assert()53 .success();54}5545#[test]46fn json_includes_name_only_when_different() {47 for (args, expected) in [48 (vec!["sleep 0.01"], vec![("sleep 0.01", None)]),49 (50 vec!["--command-name=example", "sleep 0.01"],51 vec![("sleep 0.01", Some("example"))],52 ),53 (54 vec!["--command-name=sleep 0.01", "sleep 0.01"],55 vec![("sleep 0.01", None)],56 ),57 (58 vec![59 "-L",60 "delay",61 "0.01,0.02",62 "--command-name=delay {delay}",63 "sleep {delay}",64 ],65 vec![66 ("sleep 0.01", Some("delay 0.01")),67 ("sleep 0.02", Some("delay 0.02")),68 ],69 ),70 (71 vec![72 "-L",73 "delay",74 "0.01,0.02",75 "--command-name=sleep {delay}",76 "sleep {delay}",77 ],78 vec![("sleep 0.01", None), ("sleep 0.02", None)],79 ),80 ] {81 let output = hyperfine_debug()82 .args(["--runs=1", "--style=none", "--export-json=-"])83 .args(args)84 .output()85 .unwrap();86 assert!(output.status.success());87 let export: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();88 let results = export["results"].as_array().unwrap();89 assert_eq!(results.len(), expected.len());90 for (result, (command, name)) in results.iter().zip(expected) {91 assert_eq!(result["command"], command);92 assert!(result.get("command_raw").is_none());93 assert_eq!(94 result.get("name"),95 name.map(serde_json::Value::from).as_ref()96 );97 }98 }99}100101#[test]102fn json_memory_availability_matches_platform() {103 let output = hyperfine_raw_command()104 .args([105 "--runs=1",106 "--style=none",107 "--export-json=-",108 "echo benchmark",109 ])110 .output()111 .unwrap();112 assert!(output.status.success());113 let export: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();114 let measurement = &export["results"][0]["measurements"][0];115 let memory = measurement.get("memory_peak_resident");116 if cfg!(windows) {117 assert!(memory.is_none(), "unavailable memory must be omitted");118 } else {119 let memory = memory.expect("Unix memory measurement must be present");120 assert_eq!(memory["unit"], "byte");121 assert!(memory["value"].as_f64().unwrap() >= 0.0);122 }123}12456/// Regression test: hyperfine must not panic when writing to a closed57/// stdout pipe (e.g. `hyperfine ... | head -n 1` or a downstream process58/// that terminates early). A `BrokenPipe` error should result in a quiet59/// exit with code 0 instead of a `println!` panic.
1223 unmodified lines
1283 "conclude:1",1284 ]1285 );1286}12871288#[test]1289fn json_export_basic() {1290 let _settings = json_snapshot_settings().bind_to_scope();1291 assert_cmd_snapshot!(hyperfine_debug().args([1292 "--style=none",1293 "--export-json=-",1294 "--runs=2",1295 "--warmup=1",1296 "--reference-name=one second",1297 "--reference=sleep 1",1298 "--command-name=sleep 2",1299 "sleep 2",1300 ]));1301}13021303#[test]1304fn json_export_single_run() {1305 // Make sure that the standard deviation is set to `null`1306 let _settings = json_snapshot_settings().bind_to_scope();1307 assert_cmd_snapshot!(hyperfine_debug().args([1308 "--style=none",1309 "--export-json=-",1310 "--runs=1",1311 "sleep 1",1312 ]));1313}13141315#[test]1316fn json_export_parameterized_with_reference() {1317 let _settings = json_snapshot_settings().bind_to_scope();1318 assert_cmd_snapshot!(hyperfine_debug().args([1319 "--style=none",1320 "--export-json=-",1321 "--runs=2",1322 "-L",1323 "duration",1324 "1,2",1325 "--command-name=sleep for {duration} seconds",1326 "--reference=sleep for 2 seconds",1327 "sleep {duration}",1328 ]));1329}