Skip to content

Commit 5f8b9d5

Browse files
committed
Print runtime benchmark measurement statistics
1 parent 8d7b9fd commit 5f8b9d5

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
use crate::messages::BenchmarkStats;
1+
use crate::comm::messages::BenchmarkMeasurement;
22

3-
pub fn benchmark_function<F: FnOnce() -> R, R>(
4-
_name: &'static str,
5-
_func: F,
6-
) -> anyhow::Result<BenchmarkStats> {
3+
pub fn benchmark_function<F: FnOnce() -> R, R>(_func: F) -> anyhow::Result<BenchmarkMeasurement> {
74
panic!("Runtime benchmarking is not supported on Windows");
85
}

collector/src/runtime/benchmark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub fn discover_benchmarks(cargo_stdout: &[u8]) -> anyhow::Result<BenchmarkDatab
8686
}
8787
}
8888

89+
binaries.sort_unstable_by(|a, b| a.path.cmp(&b.path));
8990
log::debug!("Found binaries: {:?}", binaries);
9091

9192
Ok(BenchmarkDatabase { binaries })

collector/src/runtime/mod.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod benchmark;
22

33
use crate::benchmark::profile::Profile;
44
use crate::toolchain::{get_local_toolchain, LocalToolchain};
5-
use benchlib::comm::messages::BenchmarkMessage;
5+
use benchlib::comm::messages::{BenchmarkMeasurement, BenchmarkMessage, BenchmarkStats};
66
use std::io::{BufRead, BufReader};
77
use std::path::{Path, PathBuf};
88
use std::process::{Command, Stdio};
@@ -50,6 +50,7 @@ pub fn bench_runtime(
5050
benchmark_index,
5151
filtered
5252
);
53+
print_stats(&stats);
5354
}
5455
}
5556
}
@@ -113,3 +114,32 @@ fn compile_runtime_benchmarks(toolchain: &LocalToolchain, dir: &Path) -> anyhow:
113114
return Ok(result.stdout);
114115
}
115116
}
117+
118+
fn calculate_mean<I: Iterator<Item = f64> + Clone>(iter: I) -> f64 {
119+
let sum: f64 = iter.clone().sum();
120+
let count = iter.count();
121+
sum / count as f64
122+
}
123+
124+
fn print_stats(stats: &BenchmarkStats) {
125+
fn print_metric<F: Fn(&BenchmarkMeasurement) -> u64>(stats: &BenchmarkStats, name: &str, f: F) {
126+
let mean = calculate_mean(stats.measurements.iter().map(&f).map(|v| v as f64));
127+
let stddev = calculate_mean(
128+
stats
129+
.measurements
130+
.iter()
131+
.map(&f)
132+
.map(|v| (v as f64 - mean).powf(2.0)),
133+
)
134+
.sqrt();
135+
136+
let name = format!("[{name}]");
137+
println!("{name:>20}: {:>16} (+/- {:>8})", mean as u64, stddev as u64);
138+
}
139+
140+
print_metric(stats, "Instructions", |m| m.instructions);
141+
print_metric(stats, "Cycles", |m| m.cycles);
142+
print_metric(stats, "Wall time [us]", |m| m.wall_time.as_micros() as u64);
143+
print_metric(stats, "Branch misses", |m| m.branch_misses);
144+
print_metric(stats, "Cache misses", |m| m.cache_misses);
145+
}

0 commit comments

Comments
 (0)