Skip to content
Myr

Allow forwarding benchmark's stdout to hyperfine

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This change makes it possible to forward the stdout of the command the user tries to benchmark to hyperfine's stdout using the `--print-stdout` flag. This should be irrelevant for most use-cases, however it makes it possible to get some crude benchmarks of terminal emulators similar to what's currently often done with `time cat file`.

authored by Christian Duerr · committed by David Peter · parent 1011eef · Browse files · 5 files+63 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+63 added−21 removed

  • other:+63 added−21 removed

5 files+63 added−21 removed

modified:src/hyperfine/app.rs+8 added
116 unmodified lines
117 .takes_value(true)118 .value_name("FILE")119 .help("Export the timing results as a Markdown table to the given FILE."),120 )121 .arg(122 Arg::with_name("print-stdout")123 .long("print-stdout")124 .conflicts_with("style")125 .help("Print the stdout of the benchmark instead of suppressing it. \126 This will increase the time it takes for benchmarks to run, \127 so it should only be used when trying to benchmark output speed."),128 )129 .help_message("Print this help message.")130 .version_message("Show version information.")131}
modified:src/hyperfine/benchmark.rs+38 added-11 removed
1use std::io;2use std::process::Stdio;34use colored::*;5use statistical::{mean, standard_deviation};6
31 unmodified lines
3839/// Run the given shell command and measure the execution time40pub fn time_shell_command(41 command: &Command,42 print_stdout: bool,43 failure_action: CmdFailureAction,44 shell_spawning_time: Option<TimingResult>,45) -> io::Result<(TimingResult, bool)> {46 let wallclock_timer = WallClockTimer::start();4748 let stdout = if print_stdout {49 Stdio::inherit()50 } else {51 Stdio::null()52 };46 let result = execute_and_time(&command.get_shell_command())?;53 let result = execute_and_time(stdout, &command.get_shell_command())?;5455 let mut time_user = result.user_time;56 let mut time_system = result.system_time;57
24 unmodified lines
82 ))83}8485/// Measure the average shell spawning time79pub fn mean_shell_spawning_time(style: &OutputStyleOption) -> io::Result<TimingResult> {86pub fn mean_shell_spawning_time(87 style: &OutputStyleOption,88 print_stdout: bool,89) -> io::Result<TimingResult> {90 const COUNT: u64 = 200;91 let progress_bar = get_progress_bar(COUNT, "Measuring shell spawning time", style);9293 let mut times_real: Vec<Second> = vec![];
1 unmodified line
95 let mut times_system: Vec<Second> = vec![];9697 for _ in 0..COUNT {98 // Just run the shell without any command89 let res = time_shell_command(&Command::new(""), CmdFailureAction::RaiseError, None);99 let res = time_shell_command(100 &Command::new(""),101 print_stdout,102 CmdFailureAction::RaiseError,103 None,104 );105106 match res {107 Err(_) => {108 return Err(io::Error::new(
19 unmodified lines
128 })129}130131/// Run the command specified by `--prepare`.117fn run_preparation_command(command: &Option<String>) -> io::Result<TimingResult> {132fn run_preparation_command(133 command: &Option<String>,134 print_stdout: bool,135) -> io::Result<TimingResult> {136 if let &Some(ref preparation_command) = command {137 let res = time_shell_command(138 &Command::new(preparation_command),139 print_stdout,140 CmdFailureAction::RaiseError,141 None,142 );143 if res.is_err() {
38 unmodified lines
182 &options.output_style,183 );184185 for _ in 0..options.warmup_count {167 let _ = time_shell_command(cmd, options.failure_action, None)?;186 let _ = time_shell_command(cmd, options.print_stdout, options.failure_action, None)?;187 progress_bar.inc(1);188 }189 progress_bar.finish_and_clear();190 }
5 unmodified lines
196 &options.output_style,197 );198199 // Run init / cleanup command181 let prepare_res = run_preparation_command(&options.preparation_command)?;200 let prepare_res = run_preparation_command(&options.preparation_command, options.print_stdout)?;201202 // Initial timing run184 let (res, success) =185 time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;203 let (res, success) = time_shell_command(204 cmd,205 options.print_stdout,206 options.failure_action,207 Some(shell_spawning_time),208 )?;209210 // Determine number of benchmark runs211 let runs_in_min_time = (options.min_time_sec212 / (res.time_real + prepare_res.time_real + shell_spawning_time.time_real))
18 unmodified lines
231 progress_bar.set_length(count_remaining);232233 // Gather statistics234 for _ in 0..count_remaining {212 run_preparation_command(&options.preparation_command)?;235 run_preparation_command(&options.preparation_command, options.print_stdout)?;236237 let msg = {238 let mean = format_duration(mean(&times_real), None);239 format!("Current estimate: {}", mean.to_string().green())240 };241 progress_bar.set_message(&msg);242220 let (res, success) =221 time_shell_command(cmd, options.failure_action, Some(shell_spawning_time))?;243 let (res, success) = time_shell_command(244 cmd,245 options.print_stdout,246 options.failure_action,247 Some(shell_spawning_time),248 )?;249250 times_real.push(res.time_real);251 times_user.push(res.time_user);252 times_system.push(res.time_system);
86 unmodified lines
modified:src/hyperfine/shell.rs+8 added-8 removed
17 unmodified lines
18}1920/// Execute the given command and return a timing summary21#[cfg(windows)]22pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {23 let mut child = run_shell_command(command)?;22pub fn execute_and_time(stdout: Stdio, command: &str) -> io::Result<ExecuteResult> {23 let mut child = run_shell_command(stdout, command)?;24 let cpu_timer = get_cpu_timer(&child);25 let status = child.wait()?;2627 let (user_time, system_time) = cpu_timer.stop();
5 unmodified lines
33}3435/// Execute the given command and return a timing summary36#[cfg(not(windows))]37pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {37pub fn execute_and_time(stdout: Stdio, command: &str) -> io::Result<ExecuteResult> {38 let cpu_timer = get_cpu_timer();3940 let status = run_shell_command(command)?;40 let status = run_shell_command(stdout, command)?;4142 let (user_time, system_time) = cpu_timer.stop();4344 Ok(ExecuteResult {
4 unmodified lines
49}5051/// Run a standard shell command52#[cfg(not(windows))]53fn run_shell_command(command: &str) -> io::Result<std::process::ExitStatus> {53fn run_shell_command(stdout: Stdio, command: &str) -> io::Result<std::process::ExitStatus> {54 Command::new("sh")55 .arg("-c")56 .arg(command)57 .stdin(Stdio::null())58 .stdout(Stdio::null())58 .stdout(stdout)59 .stderr(Stdio::null())60 .status()61}6263/// Run a Windows shell command using cmd.exe64#[cfg(windows)]65fn run_shell_command(command: &str) -> io::Result<std::process::Child> {65fn run_shell_command(print_stdout: bool, command: &str) -> io::Result<std::process::Child> {66 Command::new("cmd")67 .arg("/C")68 .arg(command)69 .stdin(Stdio::null())70 .stdout(Stdio::null())70 .stdout(stdout)71 .stderr(Stdio::null())72 .spawn()73}
modified:src/hyperfine/types.rs+4 added
87 unmodified lines
88 pub preparation_command: Option<String>,8990 /// What color mode to use for output91 pub output_style: OutputStyleOption,9293 /// Forward benchmark's stdout to hyperfine's stdout94 pub print_stdout: bool,95}9697impl Default for HyperfineOptions {98 fn default() -> HyperfineOptions {
3 unmodified lines
102 min_time_sec: 3.0,103 failure_action: CmdFailureAction::RaiseError,104 preparation_command: None,105 output_style: OutputStyleOption::Full,106 print_stdout: false,107 }108 }109}110
53 unmodified lines
modified:src/main.rs+5 added-2 removed
54 unmodified lines
55}5657/// Runs the benchmark for the given commands58fn run(commands: &Vec<Command>, options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> {59 let shell_spawning_time = mean_shell_spawning_time(&options.output_style)?;59 let shell_spawning_time =60 mean_shell_spawning_time(&options.output_style, options.print_stdout)?;6162 let mut timing_results = vec![];6364 // Run the benchmarks
63 unmodified lines
128 }129130 options.preparation_command = matches.value_of("prepare").map(String::from);131132 options.print_stdout = matches.is_present("print-stdout");133134 options.output_style = match matches.value_of("style") {135 Some("full") => OutputStyleOption::Full,136 Some("basic") => OutputStyleOption::Basic,137 Some("nocolor") => OutputStyleOption::NoColor,135 _ => if atty::is(Stream::Stdout) {138 _ => if !options.print_stdout && atty::is(Stream::Stdout) {139 OutputStyleOption::Full140 } else {141 OutputStyleOption::Basic142 },
57 unmodified lines