Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tavianator authored and sharkdp committed May 17, 2022
1 parent efdf735 commit 3df63ba
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ pub enum OptionsError<'a> {
EmptyShell,
#[error("Failed to parse '--shell <command>' expression as command line: {0}")]
ShellParseError(shell_words::ParseError),
#[error("Unknown output policy '{0}'. Use ./{0} to output to a file named {0}.")]
#[error("Unknown output policy '{0}'. Use './{0}' to output to a file named '{0}'.")]
UnknownOutputPolicy(String),
}
30 changes: 15 additions & 15 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ impl Default for RunBounds {
/// How to handle the output of benchmarked commands
#[derive(Debug, Clone, PartialEq)]
pub enum CommandOutputPolicy {
/// Discard all output
Discard,
/// Redirect output to the null device
Null,

/// Feed output through a pipe before discarding it
Pipe,
Expand All @@ -123,19 +123,19 @@ pub enum CommandOutputPolicy {
File(PathBuf),

/// Show command output on the terminal
Forward,
Inherit,
}

impl Default for CommandOutputPolicy {
fn default() -> Self {
CommandOutputPolicy::Discard
CommandOutputPolicy::Null
}
}

impl CommandOutputPolicy {
pub fn get_stdout_stderr(&self) -> io::Result<(Stdio, Stdio)> {
let streams = match self {
CommandOutputPolicy::Discard => (Stdio::null(), Stdio::null()),
CommandOutputPolicy::Null => (Stdio::null(), Stdio::null()),

// Typically only stdout is performance-relevant, so just pipe that
CommandOutputPolicy::Pipe => (Stdio::piped(), Stdio::null()),
Expand All @@ -145,7 +145,7 @@ impl CommandOutputPolicy {
(file.into(), Stdio::null())
}

CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()),
CommandOutputPolicy::Inherit => (Stdio::inherit(), Stdio::inherit()),
};

Ok(streams)
Expand Down Expand Up @@ -212,7 +212,7 @@ impl Default for Options {
cleanup_command: None,
output_style: OutputStyleOption::Full,
executor_kind: ExecutorKind::default(),
command_output_policy: CommandOutputPolicy::Discard,
command_output_policy: CommandOutputPolicy::Null,
time_unit: None,
}
}
Expand Down Expand Up @@ -269,12 +269,12 @@ impl Options {
options.cleanup_command = matches.value_of("cleanup").map(String::from);

options.command_output_policy = if matches.is_present("show-output") {
CommandOutputPolicy::Forward
CommandOutputPolicy::Inherit
} else if let Some(output) = matches.value_of("output") {
match output {
"null" => CommandOutputPolicy::Discard,
"null" => CommandOutputPolicy::Null,
"pipe" => CommandOutputPolicy::Pipe,
"inherit" => CommandOutputPolicy::Forward,
"inherit" => CommandOutputPolicy::Inherit,
arg => {
let path = PathBuf::from(arg);
if path.components().count() <= 1 {
Expand All @@ -284,7 +284,7 @@ impl Options {
}
}
} else {
CommandOutputPolicy::Discard
CommandOutputPolicy::Null
};

options.output_style = match matches.value_of("style") {
Expand All @@ -294,12 +294,12 @@ impl Options {
Some("color") => OutputStyleOption::Color,
Some("none") => OutputStyleOption::Disabled,
_ => {
if options.command_output_policy != CommandOutputPolicy::Forward
&& atty::is(Stream::Stdout)
if options.command_output_policy == CommandOutputPolicy::Inherit
|| !atty::is(Stream::Stdout)
{
OutputStyleOption::Full
} else {
OutputStyleOption::Basic
} else {
OutputStyleOption::Full
}
}
};
Expand Down
27 changes: 12 additions & 15 deletions src/timer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ use std::fs::File;
#[cfg(target_os = "linux")]
use std::os::unix::io::AsRawFd;

#[cfg(not(target_os = "linux"))]
use std::io::Read;

use crate::util::units::Second;
use wall_clock_timer::WallClockTimer;

use std::io::Read;
use std::process::{ChildStdout, Command, ExitStatus};

use anyhow::Result;
Expand Down Expand Up @@ -65,14 +63,11 @@ fn discard(output: ChildStdout) {
}
}

#[cfg(not(target_os = "linux"))]
{
let mut output = output;
let mut buf = [0; CHUNK_SIZE];
while let Ok(bytes) = output.read(&mut buf) {
if bytes == 0 {
break;
}
let mut output = output;
let mut buf = [0; CHUNK_SIZE];
while let Ok(bytes) = output.read(&mut buf) {
if bytes == 0 {
break;
}
}
}
Expand All @@ -81,17 +76,19 @@ fn discard(output: ChildStdout) {
pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {
let wallclock_timer = WallClockTimer::start();

let mut child = command.spawn()?;
let output = child.stdout.take();

#[cfg(not(windows))]
let cpu_timer = self::unix_timer::CPUTimer::start();

let mut child = command.spawn()?;

#[cfg(windows)]
let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);

if let Some(output) = output {
if let Some(output) = child.stdout.take() {
// Handle CommandOutputPolicy::Pipe
discard(output);
}

let status = child.wait()?;

let (time_user, time_system) = cpu_timer.stop();
Expand Down

0 comments on commit 3df63ba

Please sign in to comment.