refactor: remove conditional color code in bench reporter (#23593)

There is no need for this conditional code because it's handled by the
`colors` module.
This commit is contained in:
David Sherret 2024-04-29 09:27:55 -04:00 committed by GitHub
parent 0b454056a8
commit 7d93704591
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 123 deletions

View File

@ -201,7 +201,6 @@ pub mod reporter {
pub struct Options { pub struct Options {
size: usize, size: usize,
pub avg: bool, pub avg: bool,
pub colors: bool,
pub min_max: bool, pub min_max: bool,
pub percentiles: bool, pub percentiles: bool,
} }
@ -210,7 +209,6 @@ pub mod reporter {
pub fn new(names: &[&str]) -> Options { pub fn new(names: &[&str]) -> Options {
Options { Options {
avg: true, avg: true,
colors: true,
min_max: true, min_max: true,
size: size(names), size: size(names),
percentiles: true, percentiles: true,
@ -253,23 +251,12 @@ pub mod reporter {
let mut s = String::new(); let mut s = String::new();
s.push_str(&format!("{:<size$}", n)); s.push_str(&format!("{:<size$}", n));
s.push_str(&format!( s.push_str(&format!("{}: {}", colors::red("error"), e.message));
"{}: {}",
&(if !options.colors {
"error".to_string()
} else {
colors::red("error").to_string()
}),
e.message
));
if let Some(ref stack) = e.stack { if let Some(ref stack) = e.stack {
s.push('\n'); s.push('\n');
match options.colors { s.push_str(&colors::gray(stack).to_string());
false => s.push_str(stack),
true => s.push_str(&colors::gray(stack).to_string()),
}
} }
s s
@ -304,64 +291,36 @@ pub mod reporter {
s.push_str(&format!("{:<size$}", name)); s.push_str(&format!("{:<size$}", name));
if !options.colors { if options.avg {
if options.avg { s.push_str(&format!(
s.push_str(&format!( "{:>30}",
"{:>14}", format!("{}/iter", colors::yellow(fmt_duration(stats.avg)))
format!("{}/iter", fmt_duration(stats.avg)) ));
)); s.push_str(&format!("{:>14}", avg_to_iter_per_s(stats.avg)));
s.push_str(&format!("{:>14}", avg_to_iter_per_s(stats.avg))); }
} if options.min_max {
if options.min_max { s.push_str(&format!(
s.push_str(&format!( "{:>50}",
"{:>24}", format!(
format!( "({} … {})",
"({} … {})", colors::cyan(fmt_duration(stats.min)),
fmt_duration(stats.min), colors::magenta(fmt_duration(stats.max))
fmt_duration(stats.max) )
) ));
)); }
} if options.percentiles {
if options.percentiles { s.push_str(&format!(
s.push_str(&format!( " {:>22} {:>22} {:>22}",
" {:>9} {:>9} {:>9}", colors::magenta(fmt_duration(stats.p75)),
fmt_duration(stats.p75), colors::magenta(fmt_duration(stats.p99)),
fmt_duration(stats.p99), colors::magenta(fmt_duration(stats.p995))
fmt_duration(stats.p995) ));
));
}
} else {
if options.avg {
s.push_str(&format!(
"{:>30}",
format!("{}/iter", colors::yellow(fmt_duration(stats.avg)))
));
s.push_str(&format!("{:>14}", avg_to_iter_per_s(stats.avg)));
}
if options.min_max {
s.push_str(&format!(
"{:>50}",
format!(
"({} … {})",
colors::cyan(fmt_duration(stats.min)),
colors::magenta(fmt_duration(stats.max))
)
));
}
if options.percentiles {
s.push_str(&format!(
" {:>22} {:>22} {:>22}",
colors::magenta(fmt_duration(stats.p75)),
colors::magenta(fmt_duration(stats.p99)),
colors::magenta(fmt_duration(stats.p995))
));
}
} }
s s
} }
pub fn summary(benchmarks: &[GroupBenchmark], options: &Options) -> String { pub fn summary(benchmarks: &[GroupBenchmark]) -> String {
let mut s = String::new(); let mut s = String::new();
let mut benchmarks = benchmarks.to_owned(); let mut benchmarks = benchmarks.to_owned();
benchmarks.sort_by(|a, b| a.stats.avg.partial_cmp(&b.stats.avg).unwrap()); benchmarks.sort_by(|a, b| a.stats.avg.partial_cmp(&b.stats.avg).unwrap());
@ -370,58 +329,34 @@ pub mod reporter {
.find(|b| b.baseline) .find(|b| b.baseline)
.unwrap_or(&benchmarks[0]); .unwrap_or(&benchmarks[0]);
if !options.colors { s.push_str(&format!(
s.push_str(&format!("summary\n {}", baseline.name)); "{}\n {}",
colors::gray("summary"),
colors::cyan_bold(&baseline.name)
));
for b in benchmarks.iter().filter(|b| *b != baseline) { for b in benchmarks.iter().filter(|b| *b != baseline) {
let faster = b.stats.avg >= baseline.stats.avg; let faster = b.stats.avg >= baseline.stats.avg;
let diff = f64::from_str(&format!( let diff = f64::from_str(&format!(
"{:.2}", "{:.2}",
1.0 / baseline.stats.avg * b.stats.avg 1.0 / baseline.stats.avg * b.stats.avg
)) ))
.unwrap(); .unwrap();
let inv_diff = f64::from_str(&format!( let inv_diff = f64::from_str(&format!(
"{:.2}", "{:.2}",
1.0 / b.stats.avg * baseline.stats.avg 1.0 / b.stats.avg * baseline.stats.avg
)) ))
.unwrap(); .unwrap();
s.push_str(&format!(
"\n {}x times {} than {}",
if faster { diff } else { inv_diff },
if faster { "faster" } else { "slower" },
b.name
));
}
} else {
s.push_str(&format!( s.push_str(&format!(
"{}\n {}", "\n {}x {} than {}",
colors::gray("summary"), if faster {
colors::cyan_bold(&baseline.name) colors::green(diff.to_string()).to_string()
} else {
colors::red(inv_diff.to_string()).to_string()
},
if faster { "faster" } else { "slower" },
colors::cyan_bold(&b.name)
)); ));
for b in benchmarks.iter().filter(|b| *b != baseline) {
let faster = b.stats.avg >= baseline.stats.avg;
let diff = f64::from_str(&format!(
"{:.2}",
1.0 / baseline.stats.avg * b.stats.avg
))
.unwrap();
let inv_diff = f64::from_str(&format!(
"{:.2}",
1.0 / b.stats.avg * baseline.stats.avg
))
.unwrap();
s.push_str(&format!(
"\n {}x {} than {}",
if faster {
colors::green(diff.to_string()).to_string()
} else {
colors::red(inv_diff.to_string()).to_string()
},
if faster { "faster" } else { "slower" },
colors::cyan_bold(&b.name)
));
}
} }
s s

View File

@ -138,7 +138,6 @@ impl BenchReporter for ConsoleReporter {
let options = self.options.as_mut().unwrap(); let options = self.options.as_mut().unwrap();
options.percentiles = true; options.percentiles = true;
options.colors = colors::use_color();
if FIRST_PLAN if FIRST_PLAN
.compare_exchange(true, false, Ordering::SeqCst, Ordering::SeqCst) .compare_exchange(true, false, Ordering::SeqCst, Ordering::SeqCst)
@ -246,10 +245,9 @@ impl BenchReporter for ConsoleReporter {
} }
fn report_group_summary(&mut self) { fn report_group_summary(&mut self) {
let options = match self.options.as_ref() { if self.options.is_none() {
None => return, return;
Some(options) => options, }
};
if 2 <= self.group_measurements.len() if 2 <= self.group_measurements.len()
&& (self.group.is_some() || (self.group.is_none() && self.baseline)) && (self.group.is_some() || (self.group.is_none() && self.baseline))
@ -275,7 +273,6 @@ impl BenchReporter for ConsoleReporter {
}, },
}) })
.collect::<Vec<mitata::reporter::GroupBenchmark>>(), .collect::<Vec<mitata::reporter::GroupBenchmark>>(),
options
) )
); );
} }