Update warnings, switch to colored crate

This commit is contained in:
sharkdp authored and David Peter committed 2018-01-24 20:30:40 +01:00
1 parent 2ddc21c7d4
commit d5c21ec272
8 files changed
+101 -87

No files matched your search

+20 -19
View File
@@ -2,11 +2,12 @@ use std::io;
use std::process::{Command, Stdio};
use std::time::Instant;
use ansi_term::Colour::{Blue, Cyan, Green, Purple, White, Yellow};
use colored::*;
use statistical::{mean, standard_deviation};
use hyperfine::internal::{get_progress_bar, max, min, CmdFailureAction, HyperfineOptions, Second,
Warnings, MIN_EXECUTION_TIME};
MIN_EXECUTION_TIME};
use hyperfine::warnings::Warnings;
use hyperfine::format::{format_duration, format_duration_unit};
use hyperfine::cputime::{cpu_time_interval, get_cpu_times};
use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};
@@ -148,8 +149,8 @@ pub fn run_benchmark(
) -> io::Result<()> {
println!(
"{}{}: {}",
White.bold().paint("Benchmark #"),
White.bold().paint((num + 1).to_string()),
"Benchmark #".bold(),
(num + 1).to_string().bold(),
cmd
);
println!();
@@ -208,7 +209,7 @@ pub fn run_benchmark(
let msg = {
let mean = format_duration(mean(&times_real), None);
format!("Current estimate: {}", Green.paint(mean))
format!("Current estimate: {}", mean.to_string().green())
};
progress_bar.set_message(&msg);
@@ -244,22 +245,22 @@ pub fn run_benchmark(
let system_str = format_duration(system_mean, Some(user_unit));
println!(
" Time ({} ± {}): {} ± {} [User: {}, System: {}]",
Green.bold().paint("mean"),
Green.paint("σ"),
Green.bold().paint(mean_str),
Green.paint(stddev_str),
Blue.paint(user_str),
Blue.paint(system_str)
" Time ({} ± {}): {:>8} ± {:>8} [User: {}, System: {}]",
"mean".green().bold(),
"σ".green(),
mean_str.green().bold(),
stddev_str.green(),
user_str.blue(),
system_str.blue()
);
println!(" ");
println!(
" Range ({} … {}): {} … {}",
Cyan.paint("min"),
Purple.paint("max"),
Cyan.paint(min_str),
Purple.paint(max_str)
" Range ({} … {}): {:>8} … {:>8}",
"min".cyan(),
"max".purple(),
min_str.cyan(),
max_str.purple()
);
// Warnings
@@ -278,7 +279,7 @@ pub fn run_benchmark(
// Run outlier detection
let scores = modified_zscores(&times_real);
if scores[0] > OUTLIER_THRESHOLD {
warnings.push(Warnings::SlowInitialRun);
warnings.push(Warnings::SlowInitialRun(times_real[0]));
} else if scores.iter().any(|&s| s > OUTLIER_THRESHOLD) {
warnings.push(Warnings::OutliersDetected);
}
@@ -286,7 +287,7 @@ pub fn run_benchmark(
if !warnings.is_empty() {
eprintln!(" ");
for warning in &warnings {
eprintln!(" {}: {}", Yellow.paint("Warning"), warning);
eprintln!(" {}: {}", "Warning".yellow(), warning);
}
}
+6 -6
View File
@@ -17,9 +17,9 @@ pub fn format_duration(duration: Second, unit: Option<Unit>) -> String {
/// Like `format_duration`, but returns the target unit as well.
pub fn format_duration_unit(duration: Second, unit: Option<Unit>) -> (String, Unit) {
if (duration < 1.0 && unit.is_none()) || unit == Some(Unit::MilliSecond) {
(format!("{:5.1} ms", duration * 1e3), Unit::MilliSecond)
(format!("{:.1} ms", duration * 1e3), Unit::MilliSecond)
} else {
(format!("{:6.3} s", duration), Unit::Second)
(format!("{:.3} s", duration), Unit::Second)
}
}
@@ -27,12 +27,12 @@ pub fn format_duration_unit(duration: Second, unit: Option<Unit>) -> (String, Un
fn test_format_duration_unit_basic() {
let (out_str, out_unit) = format_duration_unit(1.3, None);
assert_eq!(" 1.300 s", out_str);
assert_eq!("1.300 s", out_str);
assert_eq!(Unit::Second, out_unit);
let (out_str, out_unit) = format_duration_unit(1.0, None);
assert_eq!(" 1.000 s", out_str);
assert_eq!("1.000 s", out_str);
assert_eq!(Unit::Second, out_unit);
let (out_str, out_unit) = format_duration_unit(0.999, None);
@@ -42,7 +42,7 @@ fn test_format_duration_unit_basic() {
let (out_str, out_unit) = format_duration_unit(0.0, None);
assert_eq!(" 0.0 ms", out_str);
assert_eq!("0.0 ms", out_str);
assert_eq!(Unit::MilliSecond, out_unit);
let (out_str, out_unit) = format_duration_unit(1000.0, None);
@@ -55,7 +55,7 @@ fn test_format_duration_unit_basic() {
fn test_format_duration_unit_with_unit() {
let (out_str, out_unit) = format_duration_unit(1.3, Some(Unit::Second));
assert_eq!(" 1.300 s", out_str);
assert_eq!("1.300 s", out_str);
assert_eq!(Unit::Second, out_unit);
let (out_str, out_unit) = format_duration_unit(1.3, Some(Unit::MilliSecond));
-37
View File
@@ -1,5 +1,3 @@
use std::fmt;
use indicatif::{ProgressBar, ProgressStyle};
/// Type alias for unit of time
@@ -62,41 +60,6 @@ pub fn get_progress_bar(length: u64, msg: &str) -> ProgressBar {
progress_bar
}
/// Possible benchmark warnings
pub enum Warnings {
FastExecutionTime,
NonZeroExitCode,
SlowInitialRun,
OutliersDetected,
}
impl fmt::Display for Warnings {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Warnings::FastExecutionTime => write!(
f,
"Command took less than {:.0} ms to complete. Results might be inaccurate.",
MIN_EXECUTION_TIME * 1e3
),
Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),
Warnings::SlowInitialRun => write!(
f,
"The first benchmarking run for this command was significantly slower than the \
rest. This could be caused by (filesystem) caches that were not filled. You \
should consider using the '--warmup' option to fill those caches before the \
benchmark. Alternatively, use the '--prepare' option to clear the caches before \
each timing run."
),
Warnings::OutliersDetected => write!(
f,
"Statistical outliers were detected. Consider re-running this benchmark without \
any interferences from other programs. It might help to use the '--warmup' or \
'--prepare' options to mitigate outliers."
),
}
}
}
/// A max function for f64's without NaNs
pub fn max(vals: &[f64]) -> f64 {
*vals.iter()
+1
View File
@@ -3,3 +3,4 @@ pub mod format;
pub mod internal;
pub mod cputime;
pub mod outlier_detection;
pub mod warnings;
+40
View File
@@ -0,0 +1,40 @@
use std::fmt;
use hyperfine::internal::{Second, MIN_EXECUTION_TIME};
use hyperfine::format::format_duration;
/// A list of all possible warnings
pub enum Warnings {
FastExecutionTime,
NonZeroExitCode,
SlowInitialRun(Second),
OutliersDetected,
}
impl fmt::Display for Warnings {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Warnings::FastExecutionTime => write!(
f,
"Command took less than {:.0} ms to complete. Results might be inaccurate.",
MIN_EXECUTION_TIME * 1e3
),
Warnings::NonZeroExitCode => write!(f, "Ignoring non-zero exit code."),
Warnings::SlowInitialRun(t_first) => write!(
f,
"The first benchmarking run for this command was significantly slower than the \
rest ({}). This could be caused by (filesystem) caches that were not filled until \
after the first run. You should consider using the '--warmup' option to fill \
those caches before the actual benchmark. Alternatively, use the '--prepare' \
option to clear the caches before each timing run.",
format_duration(t_first, None)
),
Warnings::OutliersDetected => write!(
f,
"Statistical outliers were detected. Consider re-running this benchmark on a quiet \
PC without any interferences from other programs. It might help to use the \
'--warmup' or '--prepare' options."
),
}
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
extern crate ansi_term;
#[macro_use]
extern crate clap;
extern crate colored;
extern crate indicatif;
extern crate libc;
extern crate statistical;
@@ -13,7 +13,7 @@ use std::cmp;
use std::error::Error;
use std::io;
use ansi_term::Colour::Red;
use colored::*;
use clap::{App, AppSettings, Arg};
mod hyperfine;
@@ -23,7 +23,7 @@ use hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};
/// Print error message to stderr and terminate
pub fn error(message: &str) -> ! {
eprintln!("{}: {}", Red.paint("Error"), message);
eprintln!("{} {}", "Error:".red(), message);
std::process::exit(1);
}