-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathwalltime_results.rs
More file actions
222 lines (191 loc) · 6.14 KB
/
walltime_results.rs
File metadata and controls
222 lines (191 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use codspeed::walltime::{BenchmarkMetadata, RawWallTimeData};
use serde::{Deserialize, Serialize};
use statrs::statistics::{Data, Distribution, Max, Min, OrderStatistics};
const IQR_OUTLIER_FACTOR: f64 = 1.5;
const STDEV_OUTLIER_FACTOR: f64 = 3.0;
#[derive(Debug, Serialize, Deserialize)]
struct BenchmarkStats {
min_ns: f64,
max_ns: f64,
mean_ns: f64,
stdev_ns: f64,
q1_ns: f64,
median_ns: f64,
q3_ns: f64,
rounds: u64,
total_time: f64,
iqr_outlier_rounds: u64,
stdev_outlier_rounds: u64,
iter_per_round: u64,
warmup_iters: u64,
}
#[derive(Debug, Serialize, Deserialize, Default)]
struct BenchmarkConfig {
warmup_time_ns: Option<f64>,
min_round_time_ns: Option<f64>,
max_time_ns: Option<f64>,
max_rounds: Option<u64>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct WalltimeBenchmark {
#[serde(flatten)]
metadata: BenchmarkMetadata,
config: BenchmarkConfig,
stats: BenchmarkStats,
}
impl WalltimeBenchmark {
pub(crate) fn is_invalid(&self) -> bool {
self.stats.min_ns < f64::EPSILON
}
pub(crate) fn name(&self) -> &str {
&self.metadata.name
}
}
impl From<RawWallTimeData> for WalltimeBenchmark {
fn from(value: RawWallTimeData) -> Self {
let total_time = value.times_per_round_ns.iter().sum::<u128>() as f64 / 1_000_000_000.0;
let time_per_iteration_per_round_ns: Vec<_> = value
.times_per_round_ns
.into_iter()
.zip(&value.iters_per_round)
.map(|(time_per_round, iter_per_round)| time_per_round / iter_per_round)
.map(|t| t as f64)
.collect::<Vec<f64>>();
let mut data = Data::new(time_per_iteration_per_round_ns);
let rounds = data.len() as u64;
let mean_ns = data.mean().unwrap();
let stdev_ns = if data.len() < 2 {
// std_dev() returns f64::NAN if data has less than two entries, so we have to
// manually handle this case.
0.0
} else {
data.std_dev().unwrap()
};
let q1_ns = data.quantile(0.25);
let median_ns = data.median();
let q3_ns = data.quantile(0.75);
let iqr_ns = q3_ns - q1_ns;
let iqr_outlier_rounds = data
.iter()
.filter(|&&t| {
t < q1_ns - IQR_OUTLIER_FACTOR * iqr_ns || t > q3_ns + IQR_OUTLIER_FACTOR * iqr_ns
})
.count() as u64;
let stdev_outlier_rounds = data
.iter()
.filter(|&&t| {
t < mean_ns - STDEV_OUTLIER_FACTOR * stdev_ns
|| t > mean_ns + STDEV_OUTLIER_FACTOR * stdev_ns
})
.count() as u64;
let min_ns = data.min();
let max_ns = data.max();
// TODO(COD-1056): We currently only support single iteration count per round
let iter_per_round = (value.iters_per_round.iter().sum::<u128>()
/ value.iters_per_round.len() as u128) as u64;
let warmup_iters = 0; // FIXME: add warmup detection
let stats = BenchmarkStats {
min_ns,
max_ns,
mean_ns,
stdev_ns,
q1_ns,
median_ns,
q3_ns,
rounds,
total_time,
iqr_outlier_rounds,
stdev_outlier_rounds,
iter_per_round,
warmup_iters,
};
WalltimeBenchmark {
metadata: BenchmarkMetadata {
name: value.metadata.name,
uri: value.metadata.uri,
},
config: BenchmarkConfig {
max_time_ns: value.max_time_ns.map(|t| t as f64),
..Default::default()
},
stats,
}
}
}
#[derive(Debug, Serialize, Deserialize)]
struct Instrument {
#[serde(rename = "type")]
type_: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct Creator {
name: String,
version: String,
pid: u32,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct WalltimeResults {
creator: Creator,
instrument: Instrument,
benchmarks: Vec<WalltimeBenchmark>,
}
impl WalltimeResults {
pub fn from_benchmarks(benchmarks: Vec<WalltimeBenchmark>) -> Self {
WalltimeResults {
instrument: Instrument {
type_: "walltime".to_string(),
},
creator: Creator {
name: "codspeed-rust".to_string(),
version: env!("CARGO_PKG_VERSION").to_string(),
pid: std::process::id(),
},
benchmarks,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_single_benchmark() {
let metadata = BenchmarkMetadata {
name: "benchmark".to_string(),
uri: "test::benchmark".to_string(),
};
let raw_bench = RawWallTimeData {
metadata,
iters_per_round: vec![1],
max_time_ns: None,
times_per_round_ns: vec![42],
};
let benchmark: WalltimeBenchmark = raw_bench.into();
assert_eq!(benchmark.stats.stdev_ns, 0.);
assert_eq!(benchmark.stats.min_ns, 42.);
assert_eq!(benchmark.stats.max_ns, 42.);
assert_eq!(benchmark.stats.mean_ns, 42.);
}
#[test]
fn test_parse_bench_with_variable_iterations() {
let metadata = BenchmarkMetadata {
name: "benchmark".to_string(),
uri: "test::benchmark".to_string(),
};
let raw_bench = RawWallTimeData {
metadata,
iters_per_round: vec![1, 2, 3, 4, 5, 6],
max_time_ns: None,
times_per_round_ns: vec![42, 42 * 2, 42 * 3, 42 * 4, 42 * 5, 42 * 6],
};
let total_rounds = raw_bench.iters_per_round.iter().sum::<u128>() as f64;
let benchmark: WalltimeBenchmark = raw_bench.into();
assert_eq!(benchmark.stats.stdev_ns, 0.);
assert_eq!(benchmark.stats.min_ns, 42.);
assert_eq!(benchmark.stats.max_ns, 42.);
assert_eq!(benchmark.stats.mean_ns, 42.);
assert_eq!(
benchmark.stats.total_time,
42. * total_rounds / 1_000_000_000.0
);
}
}