@@ -2,7 +2,7 @@ mod benchmark;
22
33use crate :: benchmark:: profile:: Profile ;
44use crate :: toolchain:: { get_local_toolchain, LocalToolchain } ;
5- use benchlib:: comm:: messages:: BenchmarkMessage ;
5+ use benchlib:: comm:: messages:: { BenchmarkMeasurement , BenchmarkMessage , BenchmarkStats } ;
66use std:: io:: { BufRead , BufReader } ;
77use std:: path:: { Path , PathBuf } ;
88use 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