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
Provenance
- Commit
4be4354- Tree
f010fd3- Author
Christian Duerr <contact@christianduerr.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
1011eef
5 files changed+63 added−21 removed
modified:src/hyperfine/app.rs+8 added
116 unmodified lines117 .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
1 use std::io;2+use std::process::Stdio;3 4 use colored::*;5 use statistical::{mean, standard_deviation};6 31 unmodified lines38 39 /// Run the given shell command and measure the execution time40 pub 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();47 48+ 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())?;54 55 let mut time_user = result.user_time;56 let mut time_system = result.system_time;57 24 unmodified lines82 ))83 }84 85 /// Measure the average shell spawning time79-pub fn mean_shell_spawning_time(style: &OutputStyleOption) -> io::Result<TimingResult> {86+pub 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);92 93 let mut times_real: Vec<Second> = vec![];1 unmodified line95 let mut times_system: Vec<Second> = vec![];96 97 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+ );105 106 match res {107 Err(_) => {108 return Err(io::Error::new(19 unmodified lines128 })129 }130 131 /// Run the command specified by `--prepare`.117-fn run_preparation_command(command: &Option<String>) -> io::Result<TimingResult> {132+fn 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 lines182 &options.output_style,183 );184 185 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 lines196 &options.output_style,197 );198 199 // 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)?;201 202 // 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+ )?;209 210 // 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 lines231 progress_bar.set_length(count_remaining);232 233 // Gather statistics234 for _ in 0..count_remaining {212- run_preparation_command(&options.preparation_command)?;235+ run_preparation_command(&options.preparation_command, options.print_stdout)?;236 237 let msg = {238 let mean = format_duration(mean(×_real), None);239 format!("Current estimate: {}", mean.to_string().green())240 };241 progress_bar.set_message(&msg);242 220- 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+ )?;249 250 times_real.push(res.time_real);251 times_user.push(res.time_user);252 times_system.push(res.time_system);86 unmodified linesmodified:src/hyperfine/shell.rs+8 added-8 removed
17 unmodified lines18 }19 20 /// Execute the given command and return a timing summary21 #[cfg(windows)]22-pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {23- let mut child = run_shell_command(command)?;22+pub 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()?;26 27 let (user_time, system_time) = cpu_timer.stop();5 unmodified lines33 }34 35 /// Execute the given command and return a timing summary36 #[cfg(not(windows))]37-pub fn execute_and_time(command: &str) -> io::Result<ExecuteResult> {37+pub fn execute_and_time(stdout: Stdio, command: &str) -> io::Result<ExecuteResult> {38 let cpu_timer = get_cpu_timer();39 40- let status = run_shell_command(command)?;40+ let status = run_shell_command(stdout, command)?;41 42 let (user_time, system_time) = cpu_timer.stop();43 44 Ok(ExecuteResult {4 unmodified lines49 }50 51 /// Run a standard shell command52 #[cfg(not(windows))]53-fn run_shell_command(command: &str) -> io::Result<std::process::ExitStatus> {53+fn 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 }62 63 /// Run a Windows shell command using cmd.exe64 #[cfg(windows)]65-fn run_shell_command(command: &str) -> io::Result<std::process::Child> {65+fn 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 lines88 pub preparation_command: Option<String>,89 90 /// What color mode to use for output91 pub output_style: OutputStyleOption,92+93+ /// Forward benchmark's stdout to hyperfine's stdout94+ pub print_stdout: bool,95 }96 97 impl Default for HyperfineOptions {98 fn default() -> HyperfineOptions {3 unmodified lines102 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 linesmodified:src/main.rs+5 added-2 removed
54 unmodified lines55 }56 57 /// Runs the benchmark for the given commands58 fn 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)?;61 62 let mut timing_results = vec![];63 64 // Run the benchmarks63 unmodified lines128 }129 130 options.preparation_command = matches.value_of("prepare").map(String::from);131 132+ options.print_stdout = matches.is_present("print-stdout");133+134 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