Document JSON measurements and omit unavailable memory metrics
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 21bbcc5 · Browse files · 9 files·+147 added−44 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+147 added−44 removed
- tests:+24 added−0 removed
- other:+123 added−44 removed
Provenance
- Commit
4cd0e24- Tree
39b29be- Author
David Peter <mail@david-peter.de>- Committer
David Peter <mail@david-peter.de>- Parent
21bbcc5
9 files changed+147 added−44 removed
modified:src/benchmark/executor.rs+2 added-2 removed
275 unmodified lines276 self.shell_spawning_time = Some(Measurement {277 time_wall_clock: measurements.time_wall_clock_mean(),278 time_user: measurements.time_user_mean(),279 time_system: measurements.time_system_mean(),280- peak_memory_usage: measurements.peak_memory_usage_mean(),280+ peak_memory_usage: None,281 exit_status: ExitStatus::default(),282 });283 284 Ok(())49 unmodified lines334 Ok(Measurement {335 time_wall_clock: Self::extract_time(command.get_command_line()),336 time_user: Time::zero(),337 time_system: Time::zero(),338- peak_memory_usage: Information::zero(),338+ peak_memory_usage: Some(Information::zero()),339 exit_status,340 })341 }342 19 unmodified linesmodified:src/benchmark/measurement.rs+19 added-7 removed
14 unmodified lines15 None => serializer.serialize_unit(),16 }17 }18 19+fn serialize_optional_information<S>(20+ value: &Option<Information>,21+ serializer: S,22+) -> Result<S::Ok, S::Error>23+where24+ S: serde::Serializer,25+{26+ match value {27+ Some(value) => serialize_information(value, serializer),28+ None => serializer.serialize_none(),29+ }30+}31+32 /// Performance metric measurements and exit code for a single run33 #[derive(Debug, Clone, Default, Serialize, PartialEq)]34 pub struct Measurement {35 /// Elapsed wall clock time (real time)7 unmodified lines43 /// Time spent in kernel mode44 #[serde(serialize_with = "serialize_time")]45 pub time_system: Time,46 34- /// Maximum memory usage of the process35- #[serde(serialize_with = "serialize_information")]36- pub peak_memory_usage: Information,47+ /// Peak resident set size, if available on this platform48+ #[serde(49+ skip_serializing_if = "Option::is_none",50+ serialize_with = "serialize_optional_information"51+ )]52+ pub peak_memory_usage: Option<Information>,53 54 // The exit status of the process55 #[serde(rename = "exit_code", serialize_with = "serialize_exit_status")]56 pub exit_status: ExitStatus,69 unmodified lines126 /// The average system time127 pub fn time_system_mean(&self) -> Time {128 mean(self.measurements.iter().map(|m| m.time_system))129 }114-115- pub fn peak_memory_usage_mean(&self) -> Information {116- mean(self.measurements.iter().map(|m| m.peak_memory_usage))117- }130 }modified:src/export/csv.rs+6 added-6 removed
84 unmodified lines85 Measurement {86 time_wall_clock: Time::new::<second>(7.0),87 time_user: Time::new::<second>(7.0),88 time_system: Time::zero(),89- peak_memory_usage: Information::new::<byte>(1024.),89+ peak_memory_usage: Some(Information::new::<byte>(1024.)),90 exit_status: ExitStatus::default(),91 },92 Measurement {93 time_wall_clock: Time::new::<second>(8.0),94 time_user: Time::new::<second>(8.0),95 time_system: Time::zero(),96- peak_memory_usage: Information::new::<byte>(1024.),96+ peak_memory_usage: Some(Information::new::<byte>(1024.)),97 exit_status: ExitStatus::default(),98 },99 Measurement {100 time_wall_clock: Time::new::<second>(12.0),101 time_user: Time::new::<second>(12.0),102 time_system: Time::zero(),103- peak_memory_usage: Information::new::<byte>(1024.),103+ peak_memory_usage: Some(Information::new::<byte>(1024.)),104 exit_status: ExitStatus::default(),105 },106 ]),107 parameters: {21 unmodified lines129 Measurement {130 time_wall_clock: Time::new::<second>(17.0),131 time_user: Time::new::<second>(17.0),132 time_system: Time::zero(),133- peak_memory_usage: Information::new::<byte>(1024.),133+ peak_memory_usage: Some(Information::new::<byte>(1024.)),134 exit_status: ExitStatus::default(),135 },136 Measurement {137 time_wall_clock: Time::new::<second>(18.0),138 time_user: Time::new::<second>(18.0),139 time_system: Time::zero(),140- peak_memory_usage: Information::new::<byte>(1024.),140+ peak_memory_usage: Some(Information::new::<byte>(1024.)),141 exit_status: ExitStatus::default(),142 },143 Measurement {144 time_wall_clock: Time::new::<second>(19.0),145 time_user: Time::new::<second>(19.0),146 time_system: Time::zero(),147- peak_memory_usage: Information::new::<byte>(1024.),147+ peak_memory_usage: Some(Information::new::<byte>(1024.)),148 exit_status: ExitStatus::default(),149 },150 ]),151 parameters: {33 unmodified linesmodified:src/export/json.rs+47 added
30 unmodified lines31 32 Ok(output?)33 }34 }35+36+#[test]37+fn test_json_optional_memory() {38+ use crate::benchmark::measurement::{Measurement, Measurements};39+ use crate::quantity::{byte, Information};40+ use serde_json::json;41+42+ for (memory, expected_memory) in [43+ (None, None),44+ (45+ Some(Information::new::<byte>(0.0)),46+ Some(json!({"value": 0.0, "unit": "byte"})),47+ ),48+ (49+ Some(Information::new::<byte>(1024.0)),50+ Some(json!({"value": 1024.0, "unit": "byte"})),51+ ),52+ ] {53+ let result = BenchmarkResult {54+ command: "example".into(),55+ measurements: Measurements::new(vec![Measurement {56+ peak_memory_usage: memory,57+ ..Measurement::default()58+ }]),59+ ..BenchmarkResult::default()60+ };61+ let output = JsonExporter::default()62+ .serialize(&[result], None, SortOrder::Command)63+ .unwrap();64+ let actual: serde_json::Value = serde_json::from_slice(&output).unwrap();65+ let mut expected = json!({66+ "results": [{67+ "command": "example",68+ "measurements": [{69+ "time_wall_clock": {"value": 0.0, "unit": "second"},70+ "time_user": {"value": 0.0, "unit": "second"},71+ "time_system": {"value": 0.0, "unit": "second"},72+ "exit_code": 073+ }]74+ }]75+ });76+ if let Some(memory) = expected_memory {77+ expected["results"][0]["measurements"][0]["peak_memory_usage"] = memory;78+ }79+ assert_eq!(actual, expected);80+ }81+}modified:src/export/tests.rs+24 added-24 removed
31 unmodified lines32 Measurement {33 time_wall_clock: Time::new::<second>(0.09),34 time_user: Time::new::<second>(0.09),35 time_system: Time::zero(),36- peak_memory_usage: Information::new::<byte>(1024.),36+ peak_memory_usage: Some(Information::new::<byte>(1024.)),37 exit_status: ExitStatus::default(),38 },39 Measurement {40 time_wall_clock: Time::new::<second>(0.10),41 time_user: Time::new::<second>(0.10),42 time_system: Time::zero(),43- peak_memory_usage: Information::new::<byte>(1024.),43+ peak_memory_usage: Some(Information::new::<byte>(1024.)),44 exit_status: ExitStatus::default(),45 },46 Measurement {47 time_wall_clock: Time::new::<second>(0.14),48 time_user: Time::new::<second>(0.14),49 time_system: Time::zero(),50- peak_memory_usage: Information::new::<byte>(1024.),50+ peak_memory_usage: Some(Information::new::<byte>(1024.)),51 exit_status: ExitStatus::default(),52 },53 ]),54 parameters: BTreeMap::new(),4 unmodified lines59 Measurement {60 time_wall_clock: Time::new::<second>(2.0),61 time_user: Time::new::<second>(2.0),62 time_system: Time::zero(),63- peak_memory_usage: Information::new::<byte>(1024.),63+ peak_memory_usage: Some(Information::new::<byte>(1024.)),64 exit_status: ExitStatus::default(),65 },66 Measurement {67 time_wall_clock: Time::new::<second>(3.0),68 time_user: Time::new::<second>(3.0),69 time_system: Time::zero(),70- peak_memory_usage: Information::new::<byte>(1024.),70+ peak_memory_usage: Some(Information::new::<byte>(1024.)),71 exit_status: ExitStatus::default(),72 },73 Measurement {74 time_wall_clock: Time::new::<second>(4.0),75 time_user: Time::new::<second>(4.0),76 time_system: Time::zero(),77- peak_memory_usage: Information::new::<byte>(1024.),77+ peak_memory_usage: Some(Information::new::<byte>(1024.)),78 exit_status: ExitStatus::default(),79 },80 ]),81 parameters: BTreeMap::new(),49 unmodified lines131 Measurement {132 time_wall_clock: Time::new::<second>(2.1),133 time_user: Time::new::<second>(2.1),134 time_system: Time::zero(),135- peak_memory_usage: Information::new::<byte>(1024.),135+ peak_memory_usage: Some(Information::new::<byte>(1024.)),136 exit_status: ExitStatus::default(),137 },138 Measurement {139 time_wall_clock: Time::new::<second>(2.2),140 time_user: Time::new::<second>(2.2),141 time_system: Time::zero(),142- peak_memory_usage: Information::new::<byte>(1024.),142+ peak_memory_usage: Some(Information::new::<byte>(1024.)),143 exit_status: ExitStatus::default(),144 },145 Measurement {146 time_wall_clock: Time::new::<second>(2.3),147 time_user: Time::new::<second>(2.3),148 time_system: Time::zero(),149- peak_memory_usage: Information::new::<byte>(1024.),149+ peak_memory_usage: Some(Information::new::<byte>(1024.)),150 exit_status: ExitStatus::default(),151 },152 ]),153 parameters: BTreeMap::new(),4 unmodified lines158 Measurement {159 time_wall_clock: Time::new::<second>(0.1),160 time_user: Time::new::<second>(0.1),161 time_system: Time::zero(),162- peak_memory_usage: Information::new::<byte>(1024.),162+ peak_memory_usage: Some(Information::new::<byte>(1024.)),163 exit_status: ExitStatus::default(),164 },165 Measurement {166 time_wall_clock: Time::new::<second>(0.2),167 time_user: Time::new::<second>(0.2),168 time_system: Time::zero(),169- peak_memory_usage: Information::new::<byte>(1024.),169+ peak_memory_usage: Some(Information::new::<byte>(1024.)),170 exit_status: ExitStatus::default(),171 },172 Measurement {173 time_wall_clock: Time::new::<second>(0.3),174 time_user: Time::new::<second>(0.3),175 time_system: Time::zero(),176- peak_memory_usage: Information::new::<byte>(1024.),176+ peak_memory_usage: Some(Information::new::<byte>(1024.)),177 exit_status: ExitStatus::default(),178 },179 ]),180 parameters: BTreeMap::new(),49 unmodified lines230 Measurement {231 time_wall_clock: Time::new::<second>(2.1),232 time_user: Time::new::<second>(2.1),233 time_system: Time::zero(),234- peak_memory_usage: Information::new::<byte>(1024.),234+ peak_memory_usage: Some(Information::new::<byte>(1024.)),235 exit_status: ExitStatus::default(),236 },237 Measurement {238 time_wall_clock: Time::new::<second>(2.2),239 time_user: Time::new::<second>(2.2),240 time_system: Time::zero(),241- peak_memory_usage: Information::new::<byte>(1024.),241+ peak_memory_usage: Some(Information::new::<byte>(1024.)),242 exit_status: ExitStatus::default(),243 },244 Measurement {245 time_wall_clock: Time::new::<second>(2.3),246 time_user: Time::new::<second>(2.3),247 time_system: Time::zero(),248- peak_memory_usage: Information::new::<byte>(1024.),248+ peak_memory_usage: Some(Information::new::<byte>(1024.)),249 exit_status: ExitStatus::default(),250 },251 ]),252 parameters: BTreeMap::new(),4 unmodified lines257 Measurement {258 time_wall_clock: Time::new::<second>(0.1),259 time_user: Time::new::<second>(0.1),260 time_system: Time::zero(),261- peak_memory_usage: Information::new::<byte>(1024.),261+ peak_memory_usage: Some(Information::new::<byte>(1024.)),262 exit_status: ExitStatus::default(),263 },264 Measurement {265 time_wall_clock: Time::new::<second>(0.2),266 time_user: Time::new::<second>(0.2),267 time_system: Time::zero(),268- peak_memory_usage: Information::new::<byte>(1024.),268+ peak_memory_usage: Some(Information::new::<byte>(1024.)),269 exit_status: ExitStatus::default(),270 },271 Measurement {272 time_wall_clock: Time::new::<second>(0.3),273 time_user: Time::new::<second>(0.3),274 time_system: Time::zero(),275- peak_memory_usage: Information::new::<byte>(1024.),275+ peak_memory_usage: Some(Information::new::<byte>(1024.)),276 exit_status: ExitStatus::default(),277 },278 ]),279 parameters: BTreeMap::new(),48 unmodified lines328 Measurement {329 time_wall_clock: Time::new::<second>(2.01),330 time_user: Time::new::<second>(2.01),331 time_system: Time::zero(),332- peak_memory_usage: Information::new::<byte>(1024.),332+ peak_memory_usage: Some(Information::new::<byte>(1024.)),333 exit_status: ExitStatus::default(),334 },335 Measurement {336 time_wall_clock: Time::new::<second>(2.02),337 time_user: Time::new::<second>(2.02),338 time_system: Time::zero(),339- peak_memory_usage: Information::new::<byte>(1024.),339+ peak_memory_usage: Some(Information::new::<byte>(1024.)),340 exit_status: ExitStatus::default(),341 },342 Measurement {343 time_wall_clock: Time::new::<second>(2.03),344 time_user: Time::new::<second>(2.03),345 time_system: Time::zero(),346- peak_memory_usage: Information::new::<byte>(1024.),346+ peak_memory_usage: Some(Information::new::<byte>(1024.)),347 exit_status: ExitStatus::default(),348 },349 ]),350 parameters: BTreeMap::new(),4 unmodified lines355 Measurement {356 time_wall_clock: Time::new::<second>(0.11),357 time_user: Time::new::<second>(0.11),358 time_system: Time::zero(),359- peak_memory_usage: Information::new::<byte>(1024.),359+ peak_memory_usage: Some(Information::new::<byte>(1024.)),360 exit_status: ExitStatus::default(),361 },362 Measurement {363 time_wall_clock: Time::new::<second>(0.12),364 time_user: Time::new::<second>(0.12),365 time_system: Time::zero(),366- peak_memory_usage: Information::new::<byte>(1024.),366+ peak_memory_usage: Some(Information::new::<byte>(1024.)),367 exit_status: ExitStatus::default(),368 },369 Measurement {370 time_wall_clock: Time::new::<second>(0.13),371 time_user: Time::new::<second>(0.13),372 time_system: Time::zero(),373- peak_memory_usage: Information::new::<byte>(1024.),373+ peak_memory_usage: Some(Information::new::<byte>(1024.)),374 exit_status: ExitStatus::default(),375 },376 ]),377 parameters: BTreeMap::new(),40 unmodified linesmodified:src/timer/unix_timer.rs+2 added-2 removed
75 unmodified lines76 pub fn start() -> Self {77 Self {}78 }79 80- pub fn stop(&self, child: Child) -> Result<(Time, Time, Information, ExitStatus)> {80+ pub fn stop(&self, child: Child) -> Result<(Time, Time, Option<Information>, ExitStatus)> {81 let (status, usage) = wait4(child)?;82 Ok((83 usage.time_user,84 usage.time_system,85- usage.memory_usage,85+ Some(usage.memory_usage),86 status,87 ))88 }89 }modified:src/timer/windows_timer.rs+3 added-3 removed
82 unmodified lines83 84 Self { job_object }85 }86 87- pub fn stop(&self, mut child: Child) -> Result<(Time, Time, Information, ExitStatus)> {87+ pub fn stop(&self, mut child: Child) -> Result<(Time, Time, Option<Information>, ExitStatus)> {88 let status = child.wait()?;89 90 let mut job_object_info =91 mem::MaybeUninit::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>::uninit();23 unmodified lines115 // processes no longer associated with the job, in 100-nanosecond ticks."116 let system_time =117 Time::new::<nanosecond>((job_object_info.TotalKernelTime as f64) * 100.0);118 119- Ok((user_time, system_time, Information::zero(), status))119+ Ok((user_time, system_time, None, status))120 } else {121- Ok((Time::zero(), Time::zero(), Information::zero(), status))121+ Ok((Time::zero(), Time::zero(), None, status))122 }123 }124 }125 6 unmodified linesmodified:tests/integration_tests.rs+24 added
40 unmodified lines41 .assert()42 .success();43 }44 45+#[test]46+fn json_memory_availability_matches_platform() {47+ let output = hyperfine_raw_command()48+ .args([49+ "--runs=1",50+ "--style=none",51+ "--export-json=-",52+ "echo benchmark",53+ ])54+ .output()55+ .unwrap();56+ assert!(output.status.success());57+ let export: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();58+ let measurement = &export["results"][0]["measurements"][0];59+ let memory = measurement.get("peak_memory_usage");60+ if cfg!(windows) {61+ assert!(memory.is_none(), "unavailable memory must be omitted");62+ } else {63+ let memory = memory.expect("Unix memory measurement must be present");64+ assert_eq!(memory["unit"], "byte");65+ assert!(memory["value"].as_f64().unwrap() >= 0.0);66+ }67+}68+69 /// Regression test: hyperfine must not panic when writing to a closed70 /// stdout pipe (e.g. `hyperfine ... | head -n 1` or a downstream process71 /// that terminates early). A `BrokenPipe` error should result in a quiet72 /// exit with code 0 instead of a `println!` panic.1227 unmodified linesmodified:README.md+20 added
156 unmodified lines157 | `fd -HI '.*[0-9]\.jpg$'` | 0.232 ± 0.002 | 0.230 | 0.236 | 1.00 |158 159 #### JSON160 161+The JSON export contains a `results` array with one entry per benchmark. Each entry has a162+`measurements` array with one object per measured run (excluding warmup runs):163+164+| Field | Format | Meaning and platform differences |165+|:---|:---|:---|166+| `time_wall_clock` | `{ "value": number, "unit": "second" }` | Elapsed time, including time spent waiting or descheduled. Available on all platforms. |167+| `time_user` | `{ "value": number, "unit": "second" }` | CPU time spent in user mode, accumulated across threads. On Unix, includes descendants whose usage propagates through parents waiting for them. On Windows, includes processes associated with the benchmark's job object. |168+| `time_system` | `{ "value": number, "unit": "second" }` | CPU time spent in kernel mode, with the same process accounting as `time_user`. |169+| `peak_memory_usage` | `{ "value": number, "unit": "byte" }` | Peak resident set size (RSS) on Linux and macOS; omitted on Windows. This is the largest per-process peak among the command and descendants whose usage is collected by waiting for them, **not the simultaneous total memory of the process tree**. Linux's KiB counter and macOS's byte counter are both exported in bytes. On macOS, this is RSS, not Apple's separate memory-footprint metric. |170+| `exit_code` | Integer or `null` | Process exit code; `0` indicates success. On Unix, termination by a signal is encoded as `128 + signal number`. `null` means no exit code could be determined. Failed runs are included when allowed by `--ignore-failure`. |171+172+Metrics unavailable on a platform are omitted, rather than reported as zero. Consumers should173+accept missing metrics and additional fields as new metrics are introduced. Time values are always174+exported in seconds, regardless of `--time-unit`. When using a shell, the estimated shell startup175+CPU and wall-clock times are subtracted; memory usage is not adjusted.176+177+For example, two concurrent worker processes that each use 128 MiB can produce a178+`peak_memory_usage` of about 128 MiB, even though their combined memory use is about 256 MiB.179+Threads share a process's address space and are covered by its RSS measurement.180+181 The JSON output is useful if you want to analyze the benchmark results in more detail. The182 [`scripts/`](https://github.com/sharkdp/hyperfine/tree/master/scripts) folder includes a lot183 of helpful Python programs to further analyze benchmark results and create helpful184 visualizations, like a histogram of runtimes or a whisker plot to compare190 unmodified lines