Skip to content

Commit

Permalink
Format source code
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Nov 21, 2018
1 parent 3144c4b commit a4cf1ea
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 101 deletions.
23 changes: 17 additions & 6 deletions src/hyperfine/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};
use hyperfine::shell::execute_and_time;
use hyperfine::timer::wallclocktimer::WallClockTimer;
use hyperfine::timer::{TimerStart, TimerStop};
use hyperfine::types::{BenchmarkResult, Command, CmdFailureAction, HyperfineOptions, OutputStyleOption};
use hyperfine::types::{
BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,
};
use hyperfine::units::Second;
use hyperfine::warnings::Warnings;

Expand Down Expand Up @@ -117,10 +119,13 @@ pub fn mean_shell_spawning_time(
};

return Err(io::Error::new(
io::ErrorKind::Other,
format!("Could not measure shell execution time. \
Make sure you can run '{}'.", shell_cmd),
))
io::ErrorKind::Other,
format!(
"Could not measure shell execution time. \
Make sure you can run '{}'.",
shell_cmd
),
));
}
Ok((r, _)) => {
times_real.push(r.time_real);
Expand Down Expand Up @@ -189,7 +194,13 @@ pub fn run_benchmark(
);

for _ in 0..options.warmup_count {
let _ = time_shell_command(&options.shell, cmd, options.show_output, options.failure_action, None)?;
let _ = time_shell_command(
&options.shell,
cmd,
options.show_output,
options.failure_action,
None,
)?;
progress_bar.inc(1);
}
progress_bar.finish_and_clear();
Expand Down
204 changes: 113 additions & 91 deletions src/hyperfine/export/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ impl Exporter for MarkdownExporter {
}

fn table_header(unit_short_name: String) -> String {
format!("| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n", unit=unit_short_name)
format!(
"| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n",
unit = unit_short_name
)
}

fn start_table(unit: Unit) -> Vec<u8> {
Expand All @@ -49,12 +52,13 @@ fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResult, unit: Unit) {
dest.extend(
format!(
"| `{command}` | {mean} ± {stddev} | {min}…{max} |\n",
command=entry.command.replace("|", "\\|"),
mean=mean_str,
stddev=stddev_str,
min=min_str,
max=max_str,
).as_bytes(),
command = entry.command.replace("|", "\\|"),
mean = mean_str,
stddev = stddev_str,
min = min_str,
max = max_str,
)
.as_bytes(),
);
}

Expand All @@ -71,34 +75,36 @@ fn test_markdown_format_ms() {
let mut timing_results = vec![];

timing_results.push(BenchmarkResult::new(
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
));
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
));

timing_results.push(BenchmarkResult::new(
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
));
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
));

let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();

let formatted_expected = format!(
"{}\
"{}\
| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 |
| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 |
", table_header("ms".to_string()));
",
table_header("ms".to_string())
);

assert_eq!(formatted_expected, formatted);
}
Expand All @@ -112,34 +118,36 @@ fn test_markdown_format_s() {
let mut timing_results = vec![];

timing_results.push(BenchmarkResult::new(
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
));
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
));

timing_results.push(BenchmarkResult::new(
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
));
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
));

let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();

let formatted_expected = format!(
"{}\
"{}\
| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 |
| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 |
", table_header("s".to_string()));
",
table_header("s".to_string())
);

assert_eq!(formatted_expected, formatted);
}
Expand All @@ -152,34 +160,41 @@ fn test_markdown_format_time_unit_s() {
let mut timing_results = vec![];

timing_results.push(BenchmarkResult::new(
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
));
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
));

timing_results.push(BenchmarkResult::new(
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
));

let formatted = String::from_utf8(exporter.serialize(&timing_results, Some(Unit::Second)).unwrap()).unwrap();
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
));

let formatted = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::Second))
.unwrap(),
)
.unwrap();

let formatted_expected = format!(
"{}\
"{}\
| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 |
| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 |
", table_header("s".to_string()));
",
table_header("s".to_string())
);

assert_eq!(formatted_expected, formatted);
}
Expand All @@ -193,34 +208,41 @@ fn test_markdown_format_time_unit_ms() {
let mut timing_results = vec![];

timing_results.push(BenchmarkResult::new(
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
));
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
));

timing_results.push(BenchmarkResult::new(
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
));

let formatted = String::from_utf8(exporter.serialize(&timing_results, Some(Unit::MilliSecond)).unwrap()).unwrap();
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
));

let formatted = String::from_utf8(
exporter
.serialize(&timing_results, Some(Unit::MilliSecond))
.unwrap(),
)
.unwrap();

let formatted_expected = format!(
"{}\
"{}\
| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 |
| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 |
", table_header("ms".to_string()));
",
table_header("ms".to_string())
);

assert_eq!(formatted_expected, formatted);
}
14 changes: 12 additions & 2 deletions src/hyperfine/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ pub struct ExecuteResult {

/// Execute the given command and return a timing summary
#[cfg(windows)]
pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str) -> io::Result<ExecuteResult> {
pub fn execute_and_time(
stdout: Stdio,
stderr: Stdio,
command: &str,
shell: &str,
) -> io::Result<ExecuteResult> {
let mut child = run_shell_command(stdout, stderr, command, shell)?;
let cpu_timer = get_cpu_timer(&child);
let status = child.wait()?;
Expand All @@ -34,7 +39,12 @@ pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str

/// Execute the given command and return a timing summary
#[cfg(not(windows))]
pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str) -> io::Result<ExecuteResult> {
pub fn execute_and_time(
stdout: Stdio,
stderr: Stdio,
command: &str,
shell: &str,
) -> io::Result<ExecuteResult> {
let cpu_timer = get_cpu_timer();

let status = run_shell_command(stdout, stderr, command, shell)?;
Expand Down
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ pub fn error(message: &str) -> ! {

/// Runs the benchmark for the given commands
fn run(commands: &Vec<Command>, options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> {
let shell_spawning_time = mean_shell_spawning_time(&options.shell, &options.output_style, options.show_output)?;
let shell_spawning_time =
mean_shell_spawning_time(&options.shell, &options.output_style, options.show_output)?;

let mut timing_results = vec![];

Expand Down Expand Up @@ -191,7 +192,10 @@ fn build_hyperfine_options(matches: &ArgMatches) -> Result<HyperfineOptions, Opt
_ => {}
};

options.shell = matches.value_of("shell").unwrap_or(&options.shell).to_string();
options.shell = matches
.value_of("shell")
.unwrap_or(&options.shell)
.to_string();

if matches.is_present("ignore-failure") {
options.failure_action = CmdFailureAction::Ignore;
Expand Down

0 comments on commit a4cf1ea

Please sign in to comment.