Represent memory usage as f64 as well
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 27ad0fb · Browse files · 8 files·+93 added−79 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+93 added−79 removed
- other:+93 added−79 removed
Provenance
- Commit
6b4e9eb- Tree
fd0d3ef- Author
David Peter <sharkdp@users.noreply.github.com>- Committer
David Peter <mail@david-peter.de>- Parent
27ad0fb
8 files changed+93 added−79 removed
modified:src/benchmark/executor.rs+2 added-2 removed
7 unmodified lines8 use crate::options::{9 CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,10 };11 use crate::output::progress_bar::get_progress_bar;12-use crate::quantity::{byte, second, Information, Time, TimeQuantity};12+use crate::quantity::{second, Information, InformationQuantity, Time, TimeQuantity};13 use crate::timer::execute_and_measure;14 use crate::util::randomized_environment_offset;15 16 use anyhow::{bail, Context, Result};330 unmodified lines347 Ok(Measurement {348 time_wall_clock: Self::extract_time(command.get_command_line()),349 time_user: Time::zero(),350 time_system: Time::zero(),351- peak_memory_usage: Information::new::<byte>(0),351+ peak_memory_usage: Information::zero(),352 exit_status,353 })354 }355 19 unmodified linesmodified:src/benchmark/measurement.rs+10 added-14 removed
1 use std::process::ExitStatus;2 3 use serde::Serialize;4 5-use crate::outlier_detection::modified_zscores;5 use crate::quantity::{7- max, mean, median, min, second, serialize_information, serialize_time, standard_deviation,8- Information, Time, TimeQuantity,6+ max, mean, median, min, modified_zscores, serialize_information, serialize_time,7+ standard_deviation, Information, Time,8 };9 use crate::util::exit_code::extract_exit_code;10 11 fn serialize_exit_status<S>(exit_status: &ExitStatus, serializer: S) -> Result<S::Ok, S::Error>89 unmodified lines101 pub fn max(&self) -> Time {102 max(&self.wall_clock_times())103 }104 105+ /// Compute modified Z-scores for the wall clock times106+ pub fn modified_zscores(&self) -> Vec<f64> {107+ modified_zscores(&self.wall_clock_times())108+ }109+110 /// The average user time111 pub fn time_user_mean(&self) -> Time {112 mean(113 &self15 unmodified lines129 )130 }131 132 pub fn peak_memory_usage_mean(&self) -> Information {129- self.measurements130- .iter()131- .map(|m| m.peak_memory_usage)132- .max() // TODO: should be mean: do we need InformationF64?133- .unwrap_or_default()134- }135-136- pub fn modified_zscores(&self) -> Vec<f64> {137- modified_zscores(133+ mean(134 &self139- .wall_clock_times()135+ .measurements136 .iter()141- .map(|t| t.value_in(second)) // TODO137+ .map(|m| m.peak_memory_usage)138 .collect::<Vec<_>>(),139 )140 }141 4 unmodified linesmodified:src/export/csv.rs+6 added-6 removed
83 unmodified lines84 Measurement {85 time_wall_clock: Time::new::<second>(7.0),86 time_user: Time::new::<second>(7.0),87 time_system: Time::zero(),88- peak_memory_usage: Information::new::<byte>(1024),88+ peak_memory_usage: Information::new::<byte>(1024.),89 exit_status: ExitStatus::default(),90 },91 Measurement {92 time_wall_clock: Time::new::<second>(8.0),93 time_user: Time::new::<second>(8.0),94 time_system: Time::zero(),95- peak_memory_usage: Information::new::<byte>(1024),95+ peak_memory_usage: Information::new::<byte>(1024.),96 exit_status: ExitStatus::default(),97 },98 Measurement {99 time_wall_clock: Time::new::<second>(12.0),100 time_user: Time::new::<second>(12.0),101 time_system: Time::zero(),102- peak_memory_usage: Information::new::<byte>(1024),102+ peak_memory_usage: Information::new::<byte>(1024.),103 exit_status: ExitStatus::default(),104 },105 ]),106 parameters: {21 unmodified lines128 Measurement {129 time_wall_clock: Time::new::<second>(17.0),130 time_user: Time::new::<second>(17.0),131 time_system: Time::zero(),132- peak_memory_usage: Information::new::<byte>(1024),132+ peak_memory_usage: Information::new::<byte>(1024.),133 exit_status: ExitStatus::default(),134 },135 Measurement {136 time_wall_clock: Time::new::<second>(18.0),137 time_user: Time::new::<second>(18.0),138 time_system: Time::zero(),139- peak_memory_usage: Information::new::<byte>(1024),139+ peak_memory_usage: Information::new::<byte>(1024.),140 exit_status: ExitStatus::default(),141 },142 Measurement {143 time_wall_clock: Time::new::<second>(19.0),144 time_user: Time::new::<second>(19.0),145 time_system: Time::zero(),146- peak_memory_usage: Information::new::<byte>(1024),146+ peak_memory_usage: Information::new::<byte>(1024.),147 exit_status: ExitStatus::default(),148 },149 ]),150 parameters: {33 unmodified linesmodified: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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: 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: Information::new::<byte>(1024.),374 exit_status: ExitStatus::default(),375 },376 ]),377 parameters: BTreeMap::new(),40 unmodified linesmodified:src/quantity/mod.rs+45 added-17 removed
7 unmodified lines8 pub use uom::si::information::{byte, kibibyte};9 pub use uom::si::ratio::ratio;10 pub use uom::si::time::{hour, microsecond, millisecond, minute, nanosecond, second};11 12-pub use si::f64::Ratio;13-pub use si::f64::Time;14-pub use si::u64::Information;12+pub use si::f64::{Information, Ratio, Time};13 14 pub use si::f64::{Information, Ratio, Time};15 pub use si::information::{byte, gibibyte, kibibyte, mebibyte, tebibyte};16 pub use si::ratio::ratio;72 unmodified lines89 }90 91 pub trait InformationQuantity {92 fn zero() -> Information {95- Information::new::<byte>(0)93+ Information::new::<byte>(0.)94 }95 98- fn value_in<U>(&self, u: U) -> u6496+ fn value_in<U>(&self, u: U) -> f6497 where100- U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>;98+ U: si::information::Unit + Conversion<f64, T = f64>;99 100 fn to_string<U>(&self, u: U) -> String101 where104- U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>;102+ U: si::information::Unit + Conversion<f64, T = f64>;103 }104 105 impl InformationQuantity for Information {108- fn value_in<U>(&self, _u: U) -> u64106+ fn value_in<U>(&self, _u: U) -> f64107 where110- U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>,108+ U: si::information::Unit + Conversion<f64, T = f64>,109 {110 self.get::<U>()111 }112 113 fn to_string<U>(&self, u: U) -> String114 where117- U: si::information::Unit + Conversion<u64, T = uom::num_rational::Ratio<u64>>,115+ U: si::information::Unit + Conversion<f64, T = f64>,116 {117 format!(118 "{}",119 self.into_format_args(u, uom::fmt::DisplayStyle::Abbreviation)120 )121 }122 }123 124+pub trait UnsafeRawValue {125+ fn unsafe_raw_value(&self) -> f64;126+ fn unsafe_from_raw_value(value: f64) -> Self;127+}128+129+impl UnsafeRawValue for Time {130+ fn unsafe_raw_value(&self) -> f64 {131+ self.value_in(second)132+ }133+134+ fn unsafe_from_raw_value(value: f64) -> Self {135+ Time::new::<second>(value)136+ }137+}138+139+impl UnsafeRawValue for Information {140+ fn unsafe_raw_value(&self) -> f64 {141+ self.value_in(byte)142+ }143+144+ fn unsafe_from_raw_value(value: f64) -> Self {145+ Information::new::<byte>(value)146+ }147+}148+149 pub fn serialize_time<S>(t: &Time, s: S) -> Result<S::Ok, S::Error>150 where151 S: Serializer,152 {14 unmodified lines167 }168 169 macro_rules! quantity_fn {170 ($name:ident, $unwrapped_values:ident, $body:expr) => {148- pub fn $name(values: &[Time]) -> Time {149- let $unwrapped_values: Vec<_> = values.iter().map(|q| q.value_in(second)).collect();171+ pub fn $name<Q: UnsafeRawValue>(values: &[Q]) -> Q {172+ let $unwrapped_values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();173 let result_value = $body;174 152- Time::new::<second>(result_value)175+ Q::unsafe_from_raw_value(result_value)176 }177 };178 }179 5 unmodified lines185 let mean_value = statistical::mean(&values);186 statistical::standard_deviation(&values, Some(mean_value))187 });188 189+pub fn modified_zscores<Q: UnsafeRawValue>(values: &[Q]) -> Vec<f64> {190+ let values: Vec<_> = values.iter().map(|q| q.unsafe_raw_value()).collect();191+ crate::outlier_detection::modified_zscores(&values)192+}193+194 #[test]195 fn test_time() {196 let time = Time::new::<millisecond>(123.4);197 assert_eq!(time.value_in(millisecond), 123.4);6 unmodified lines204 }205 206 #[test]207 fn test_information() {180- let information = Information::new::<kibibyte>(8);181- assert_eq!(information.value_in(byte), 8192);208+ let information = Information::new::<kibibyte>(8.);209+ assert_eq!(information.value_in(byte), 8192.);210 211 let information_kib = information.value_in(kibibyte);184- assert_eq!(information_kib, 8);212+ assert_eq!(information_kib, 8.);213 }214 215 #[test]216 fn test_format() {1 unmodified line218 assert_eq!(time.format_auto(Some(TimeUnit::Second)), "0.123 s");219 assert_eq!(time.format_auto(Some(TimeUnit::MilliSecond)), "123.4 ms");220 assert_eq!(time.format_auto(Some(TimeUnit::MicroSecond)), "123400.0 µs");221 194- let peak_memory_usage = Information::new::<kibibyte>(8);222+ let peak_memory_usage = Information::new::<kibibyte>(8.);223 assert_eq!(peak_memory_usage.to_string(byte), "8192 B");224 assert_eq!(peak_memory_usage.to_string(kibibyte), "8 KiB");225 }226 43 unmodified linesmodified:src/timer/unix_timer.rs+5 added-3 removed
1 #![cfg(not(windows))]2 3-use std::convert::TryFrom;3 use std::io;4 use std::mem::MaybeUninit;5 use std::os::unix::process::ExitStatusExt;6 use std::process::{Child, ExitStatus};38 unmodified lines45 let rusage = unsafe { rusage.assume_init() };46 47 let memory_usage_byte = if cfg!(target_os = "macos") || cfg!(target_os = "ios") {48 // Linux and *BSD return the value in KibiBytes, Darwin flavors in bytes50- Information::new::<byte>(u64::try_from(rusage.ru_maxrss).unwrap_or(0))49+ // A f64 can represent integers up to 2^53 exactly, so we can represent50+ // all values up to 2^53 bytes = 8 PiB exactly. Beyond that, our precision51+ // is larger than 1 byte, but that's considered acceptable.52+ Information::new::<byte>(rusage.ru_maxrss as f64)53 } else {52- Information::new::<kibibyte>(u64::try_from(rusage.ru_maxrss).unwrap_or(0))54+ Information::new::<kibibyte>(rusage.ru_maxrss as f64)55 };56 57 Ok((58 ExitStatus::from_raw(status),26 unmodified linesmodified:Cargo.lock+1 added-12 removed
513 unmodified lines514 "num-bigint",515 "num-complex",516 "num-integer",517 "num-iter",518- "num-rational 0.2.4",518+ "num-rational",519 "num-traits",520 ]521 522 [[package]]49 unmodified lines572 "num-traits",573 ]574 575 [[package]]576-name = "num-rational"577-version = "0.4.2"578-source = "registry+https://github.com/rust-lang/crates.io-index"579-checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"580-dependencies = [581- "num-integer",582- "num-traits",583-]584-585-[[package]]576 name = "num-traits"577 version = "0.2.19"578 source = "registry+https://github.com/rust-lang/crates.io-index"579 checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"450 unmodified lines1030 version = "0.36.0"1031 source = "registry+https://github.com/rust-lang/crates.io-index"1032 checksum = "ffd36e5350a65d112584053ee91843955826bf9e56ec0d1351214e01f6d7cd9c"1033 dependencies = [1044- "num-rational 0.4.2",1034 "num-traits",1035 "typenum",1036 ]1037 213 unmodified linesmodified:Cargo.toml-1 removed
30 unmodified lines31323334353536373849 unmodified lines30 unmodified linesuom = { version = "0.36.0", default-features = false, features = [ "std", "si", "f64", "u64",] }
[target.'cfg(not(windows))'.dependencies]libc = "0.2"49 unmodified lines