Skip to content
Myr

measurement.rs

This file

Path

src/benchmark/measurement.rs

183 lines · 5.2 KiB · Rust

Last changed by

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

History of this file

Take it

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

lines 1–2why, one step back
1use std::process::ExitStatus;
2

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

lines 3–4why, one step back
3use serde::Serialize;
4

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

lines 5–6why, one step back
5use crate::metric::Metric;
6use crate::quantity::statistics::{mean, modified_zscores_f64};

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

lines 7–8why, one step back
7use crate::quantity::{serialize_information, serialize_time, Information, Time};
8use crate::util::exit_code::extract_exit_code;

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 9why, one step back
9

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

lines 10–32why, one step back
10fn serialize_exit_status<S>(exit_status: &ExitStatus, serializer: S) -> Result<S::Ok, S::Error>
11where12 S: serde::Serializer,13{14 match extract_exit_code(*exit_status) {15 Some(code) => serializer.serialize_i32(code),16 None => serializer.serialize_unit(),17 }18}1920fn serialize_optional_information<S>(21 value: &Option<Information>,22 serializer: S,23) -> Result<S::Ok, S::Error>24where25 S: serde::Serializer,26{27 match value {28 Some(value) => serialize_information(value, serializer),29 None => serializer.serialize_none(),30 }31}32

Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0

lines 33–73why, one step back
33fn serialize_optional_count<S>(value: &Option<u64>, serializer: S) -> Result<S::Ok, S::Error>
34where35 S: serde::Serializer,36{37 #[derive(Serialize)]38 struct Count {39 value: u64,40 }4142 value.map(|value| Count { value }).serialize(serializer)43}4445#[derive(Debug, Clone, Default, Serialize, PartialEq)]46pub struct HardwareCounters {47 #[serde(48 skip_serializing_if = "Option::is_none",49 serialize_with = "serialize_optional_count"50 )]51 pub cpu_cycles: Option<u64>,52 #[serde(53 skip_serializing_if = "Option::is_none",54 serialize_with = "serialize_optional_count"55 )]56 pub instructions: Option<u64>,57 #[serde(58 skip_serializing_if = "Option::is_none",59 serialize_with = "serialize_optional_count"60 )]61 pub cache_references: Option<u64>,62 #[serde(63 skip_serializing_if = "Option::is_none",64 serialize_with = "serialize_optional_count"65 )]66 pub cache_misses: Option<u64>,67 #[serde(68 skip_serializing_if = "Option::is_none",69 serialize_with = "serialize_optional_count"70 )]71 pub branch_misses: Option<u64>,72}73

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

lines 74–75why, one step back
74/// Performance metric measurements and exit code for a single run
75#[derive(Debug, Clone, Default, Serialize, PartialEq)]

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 76why, one step back
76pub struct Measurement {

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

lines 77–78why, one step back
77 /// Elapsed wall clock time (real time)
78 #[serde(serialize_with = "serialize_time")]

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 79why, one step back
79 pub time_wall_clock: Time,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 80why, one step back
80

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

lines 81–84why, one step back
81 /// Total CPU time (user and kernel mode)
82 #[serde(serialize_with = "serialize_time")]83 pub time_cpu: Time,84

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 85why, one step back
85 /// Time spent in user mode

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

line 86why, one step back
86 #[serde(serialize_with = "serialize_time")]

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 87why, one step back
87 pub time_user: Time,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

lines 88–89why, one step back
88
89 /// Time spent in kernel mode

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

line 90why, one step back
90 #[serde(serialize_with = "serialize_time")]

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 91why, one step back
91 pub time_system: Time,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 92why, one step back
92

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

lines 93–98why, one step back
93 /// Peak resident set size, if available on this platform
94 #[serde(95 skip_serializing_if = "Option::is_none",96 serialize_with = "serialize_optional_information"97 )]98 pub memory_peak_resident: Option<Information>,

Support hardware counters (instructions, cache misses, ...) (#983)David Peterbefore adoptionbc3a3f0

lines 99–101why, one step back
99
100 #[serde(flatten)]101 pub hardware_counters: HardwareCounters,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 102why, one step back
102

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

lines 103–104why, one step back
103 // The exit status of the process
104 #[serde(rename = "exit_code", serialize_with = "serialize_exit_status")]

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

lines 105–107why, one step back
105 pub exit_status: ExitStatus,
106}107

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

line 108why, one step back
108#[derive(Debug, Default, Clone, Serialize, PartialEq)]

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

lines 109–113why, one step back
109pub struct Measurements {
110 pub measurements: Vec<Measurement>,111}112113impl Measurements {

Merge pull request #969 from sharkdp/codex/result-measurementsDavid Peterbefore adoptione689520

lines 114–117why, one step back
114 pub fn new(measurements: Vec<Measurement>) -> Self {
115 Self { measurements }116 }117

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

lines 118–129why, one step back
118 pub fn len(&self) -> usize {
119 self.measurements.len()120 }121122 pub fn is_empty(&self) -> bool {123 self.measurements.is_empty()124 }125126 pub fn push(&mut self, measurement: Measurement) {127 self.measurements.push(measurement);128 }129

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 130why, one step back
130 pub fn wall_clock_times(&self) -> impl Iterator<Item = Time> + '_ {

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

lines 131–133why, one step back
131 self.measurements.iter().map(|m| m.time_wall_clock)
132 }133

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

line 134why, one step back
134 /// The average wall clock time

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 135why, one step back
135 pub fn time_wall_clock_mean(&self) -> Time {

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

lines 136–138why, one step back
136 mean(self.wall_clock_times())
137 }138

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

lines 139–146why, one step back
139 /// Compute modified Z-scores for a metric validated to be available in every run.
140 pub fn modified_zscores(&self, metric: Metric) -> Vec<f64> {141 let values: Vec<_> = self142 .measurements143 .iter()144 .map(|m| metric.value(m).expect("Validated metric is available"))145 .collect();146 modified_zscores_f64(&values)

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

lines 147–148why, one step back
147 }
148

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

line 149why, one step back
149 /// The average user time

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 150why, one step back
150 pub fn time_user_mean(&self) -> Time {

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

lines 151–153why, one step back
151 mean(self.measurements.iter().map(|m| m.time_user))
152 }153

Merge pull request #790 from sharkdp/new-metricsDavid Peterbefore adoptionf26c015

line 154why, one step back
154 /// The average system time

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 155why, one step back
155 pub fn time_system_mean(&self) -> Time {

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

line 156why, one step back
156 mean(self.measurements.iter().map(|m| m.time_system))

Alternative performance metrics in the terminal (#986)David Peterbefore adoptionbdbfae9

lines 157–181why, one step back
157 }
158}159160#[cfg(test)]161mod tests {162 use super::*;163 use crate::outlier_detection::OUTLIER_THRESHOLD;164 use crate::quantity::{byte, second};165166 #[test]167 fn outliers_follow_the_selected_metric() {168 let measurements = Measurements::new(169 [(1.0, 100.0), (1.0, 1.0), (1.0, 1.0), (100.0, 1.0)]170 .iter()171 .copied()172 .map(|(time, memory)| Measurement {173 time_wall_clock: Time::new::<second>(time),174 memory_peak_resident: Some(Information::new::<byte>(memory)),175 ..Measurement::default()176 })177 .collect(),178 );179180 assert!(measurements.modified_zscores(Metric::TimeWallClock)[0] < OUTLIER_THRESHOLD);181 assert!(measurements.modified_zscores(Metric::MemoryPeakResident)[0] > OUTLIER_THRESHOLD);

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

line 182why, one step back
182 }

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 183why, one step back
183}