Compare commits

...
Author SHA1 Message Date
David Peter 623ce2d2d9 Bump version 2022-05-17 22:37:08 +02:00
Tavian Barnes 3df63ba2bb Address review comments 2022-05-17 22:30:45 +02:00
Tavian Barnes efdf735723 Forbid --output=file without a path separator 2022-05-17 22:30:45 +02:00
Tavian Barnes ecee9df575 Add --output=inherit as an alias for --show-output 2022-05-17 22:30:45 +02:00
Tavian Barnes dfd1046d63 Clarify --output's help text 2022-05-17 22:30:45 +02:00
Tavian Barnes 277807cb5a Implement --output={null,pipe,<FILE>}
Fixes #377.
2022-05-17 22:30:45 +02:00
David Peter f75f0a10fe Use cross to build x86_64-unknown-linux-gnu executables
We do this in order to link against older versions of glibc.

closes #508
2022-05-16 21:15:57 +02:00
9 changed files with 156 additions and 50 deletions

No files matched your search

+1 -1
View File
@@ -60,7 +60,7 @@ jobs:
- { target: x86_64-apple-darwin , os: macos-10.15 } - { target: x86_64-apple-darwin , os: macos-10.15 }
- { target: x86_64-pc-windows-gnu , os: windows-2019 } - { target: x86_64-pc-windows-gnu , os: windows-2019 }
- { target: x86_64-pc-windows-msvc , os: windows-2019 } - { target: x86_64-pc-windows-msvc , os: windows-2019 }
- { target: x86_64-unknown-linux-gnu , os: ubuntu-20.04 } - { target: x86_64-unknown-linux-gnu , os: ubuntu-20.04, use-cross: true }
- { target: x86_64-unknown-linux-musl , os: ubuntu-20.04, use-cross: true } - { target: x86_64-unknown-linux-musl , os: ubuntu-20.04, use-cross: true }
steps: steps:
- name: Checkout source code - name: Checkout source code
+4 -13
View File
@@ -1,21 +1,12 @@
# unreleased # v1.14.0
## Features ## Features
- Add a new `--output={null,pipe,inherit,<FILE>}` option to control
where the output of the benchmarked program is redirected (if at all),
see #377 and #509 (@tavianator, originally suggested by @BurntSushi)
- Add Emacs org-mode as a new export format, see #491 (@ppaulweber) - Add Emacs org-mode as a new export format, see #491 (@ppaulweber)
## Changes
## Bugfixes
## Other
## Packaging
# v1.13.0 # v1.13.0
Generated
+23 -1
View File
@@ -262,7 +262,7 @@ dependencies = [
[[package]] [[package]]
name = "hyperfine" name = "hyperfine"
version = "1.13.0" version = "1.14.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"approx", "approx",
@@ -274,6 +274,7 @@ dependencies = [
"csv", "csv",
"indicatif", "indicatif",
"libc", "libc",
"nix",
"predicates", "predicates",
"rand 0.8.5", "rand 0.8.5",
"rust_decimal", "rust_decimal",
@@ -356,6 +357,27 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "memoffset"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
dependencies = [
"autocfg 1.1.0",
]
[[package]]
name = "nix"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f17df307904acd05aa8e32e97bb20f2a0df1728bbc2d771ae8f9a90463441e9"
dependencies = [
"bitflags",
"cfg-if",
"libc",
"memoffset",
]
[[package]] [[package]]
name = "normalize-line-endings" name = "normalize-line-endings"
version = "0.3.0" version = "0.3.0"
+4 -1
View File
@@ -7,7 +7,7 @@ license = "MIT/Apache-2.0"
name = "hyperfine" name = "hyperfine"
readme = "README.md" readme = "README.md"
repository = "https://github.com/sharkdp/hyperfine" repository = "https://github.com/sharkdp/hyperfine"
version = "1.13.0" version = "1.14.0"
edition = "2018" edition = "2018"
build = "build.rs" build = "build.rs"
@@ -31,6 +31,9 @@ libc = "0.2"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["processthreadsapi", "minwindef", "winnt"] } winapi = { version = "0.3", features = ["processthreadsapi", "minwindef", "winnt"] }
[target.'cfg(target_os="linux")'.dependencies]
nix = { version = "0.24.1", features = ["zerocopy"] }
[dependencies.clap] [dependencies.clap]
version = "3" version = "3"
default-features = false default-features = false
+4 -4
View File
@@ -36,10 +36,10 @@ pub trait Executor {
fn run_command_and_measure_common( fn run_command_and_measure_common(
mut command: std::process::Command, mut command: std::process::Command,
command_failure_action: CmdFailureAction, command_failure_action: CmdFailureAction,
command_output_policy: CommandOutputPolicy, command_output_policy: &CommandOutputPolicy,
command_name: &str, command_name: &str,
) -> Result<TimerResult> { ) -> Result<TimerResult> {
let (stdout, stderr) = command_output_policy.get_stdout_stderr(); let (stdout, stderr) = command_output_policy.get_stdout_stderr()?;
command.stdin(Stdio::null()).stdout(stdout).stderr(stderr); command.stdin(Stdio::null()).stdout(stdout).stderr(stderr);
command.env( command.env(
@@ -83,7 +83,7 @@ impl<'a> Executor for RawExecutor<'a> {
let result = run_command_and_measure_common( let result = run_command_and_measure_common(
command.get_command()?, command.get_command()?,
command_failure_action.unwrap_or(self.options.command_failure_action), command_failure_action.unwrap_or(self.options.command_failure_action),
self.options.command_output_policy, &self.options.command_output_policy,
&command.get_command_line(), &command.get_command_line(),
)?; )?;
@@ -136,7 +136,7 @@ impl<'a> Executor for ShellExecutor<'a> {
let mut result = run_command_and_measure_common( let mut result = run_command_and_measure_common(
command_builder, command_builder,
command_failure_action.unwrap_or(self.options.command_failure_action), command_failure_action.unwrap_or(self.options.command_failure_action),
self.options.command_output_policy, &self.options.command_output_policy,
&command.get_command_line(), &command.get_command_line(),
)?; )?;
+17
View File
@@ -259,6 +259,23 @@ fn build_command() -> Command<'static> {
when trying to benchmark output speed.", when trying to benchmark output speed.",
), ),
) )
.arg(
Arg::new("output")
.long("output")
.conflicts_with("show-output")
.takes_value(true)
.value_name("WHERE")
.help(
"Control where the output of the benchmark is redirected. <WHERE> can be:\n\n\
null: Redirect output to /dev/null (the default). \
Note that some programs like 'grep' detect when standard output is /dev/null \
and apply certain optimizations. To avoid that, consider using \
'--output=pipe'.\n\n\
pipe: Feed the output through a pipe before discarding it.\n\n\
inherit: Don't redirect the output at all (same as '--show-output').\n\n\
<FILE>: Write the output to the given file.",
),
)
.arg( .arg(
Arg::new("command-name") Arg::new("command-name")
.long("command-name") .long("command-name")
+2
View File
@@ -49,4 +49,6 @@ pub enum OptionsError<'a> {
EmptyShell, EmptyShell,
#[error("Failed to parse '--shell <command>' expression as command line: {0}")] #[error("Failed to parse '--shell <command>' expression as command line: {0}")]
ShellParseError(shell_words::ParseError), ShellParseError(shell_words::ParseError),
#[error("Unknown output policy '{0}'. Use './{0}' to output to a file named '{0}'.")]
UnknownOutputPolicy(String),
} }
+50 -18
View File
@@ -1,5 +1,7 @@
use std::fs::File;
use std::path::PathBuf;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::{cmp, fmt}; use std::{cmp, fmt, io};
use anyhow::ensure; use anyhow::ensure;
use atty::Stream; use atty::Stream;
@@ -109,27 +111,44 @@ impl Default for RunBounds {
} }
/// How to handle the output of benchmarked commands /// How to handle the output of benchmarked commands
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum CommandOutputPolicy { pub enum CommandOutputPolicy {
/// Discard all output /// Redirect output to the null device
Discard, Null,
/// Feed output through a pipe before discarding it
Pipe,
/// Redirect output to a file
File(PathBuf),
/// Show command output on the terminal /// Show command output on the terminal
Forward, Inherit,
} }
impl Default for CommandOutputPolicy { impl Default for CommandOutputPolicy {
fn default() -> Self { fn default() -> Self {
CommandOutputPolicy::Discard CommandOutputPolicy::Null
} }
} }
impl CommandOutputPolicy { impl CommandOutputPolicy {
pub fn get_stdout_stderr(&self) -> (Stdio, Stdio) { pub fn get_stdout_stderr(&self) -> io::Result<(Stdio, Stdio)> {
match self { let streams = match self {
CommandOutputPolicy::Discard => (Stdio::null(), Stdio::null()), CommandOutputPolicy::Null => (Stdio::null(), Stdio::null()),
CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()),
} // Typically only stdout is performance-relevant, so just pipe that
CommandOutputPolicy::Pipe => (Stdio::piped(), Stdio::null()),
CommandOutputPolicy::File(path) => {
let file = File::create(&path)?;
(file.into(), Stdio::null())
}
CommandOutputPolicy::Inherit => (Stdio::inherit(), Stdio::inherit()),
};
Ok(streams)
} }
} }
@@ -193,7 +212,7 @@ impl Default for Options {
cleanup_command: None, cleanup_command: None,
output_style: OutputStyleOption::Full, output_style: OutputStyleOption::Full,
executor_kind: ExecutorKind::default(), executor_kind: ExecutorKind::default(),
command_output_policy: CommandOutputPolicy::Discard, command_output_policy: CommandOutputPolicy::Null,
time_unit: None, time_unit: None,
} }
} }
@@ -250,9 +269,22 @@ impl Options {
options.cleanup_command = matches.value_of("cleanup").map(String::from); options.cleanup_command = matches.value_of("cleanup").map(String::from);
options.command_output_policy = if matches.is_present("show-output") { 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::Null,
"pipe" => CommandOutputPolicy::Pipe,
"inherit" => CommandOutputPolicy::Inherit,
arg => {
let path = PathBuf::from(arg);
if path.components().count() <= 1 {
return Err(OptionsError::UnknownOutputPolicy(arg.to_string()));
}
CommandOutputPolicy::File(path)
}
}
} else { } else {
CommandOutputPolicy::Discard CommandOutputPolicy::Null
}; };
options.output_style = match matches.value_of("style") { options.output_style = match matches.value_of("style") {
@@ -262,12 +294,12 @@ impl Options {
Some("color") => OutputStyleOption::Color, Some("color") => OutputStyleOption::Color,
Some("none") => OutputStyleOption::Disabled, Some("none") => OutputStyleOption::Disabled,
_ => { _ => {
if options.command_output_policy == CommandOutputPolicy::Discard if options.command_output_policy == CommandOutputPolicy::Inherit
&& atty::is(Stream::Stdout) || !atty::is(Stream::Stdout)
{ {
OutputStyleOption::Full
} else {
OutputStyleOption::Basic OutputStyleOption::Basic
} else {
OutputStyleOption::Full
} }
} }
}; };
+51 -12
View File
@@ -6,10 +6,18 @@ mod windows_timer;
#[cfg(not(windows))] #[cfg(not(windows))]
mod unix_timer; mod unix_timer;
#[cfg(target_os = "linux")]
use nix::fcntl::{splice, SpliceFFlags};
#[cfg(target_os = "linux")]
use std::fs::File;
#[cfg(target_os = "linux")]
use std::os::unix::io::AsRawFd;
use crate::util::units::Second; use crate::util::units::Second;
use wall_clock_timer::WallClockTimer; use wall_clock_timer::WallClockTimer;
use std::process::{Command, ExitStatus}; use std::io::Read;
use std::process::{ChildStdout, Command, ExitStatus};
use anyhow::Result; use anyhow::Result;
@@ -33,26 +41,57 @@ pub struct TimerResult {
pub status: ExitStatus, pub status: ExitStatus,
} }
/// Discard the output of a child process.
fn discard(output: ChildStdout) {
const CHUNK_SIZE: usize = 64 << 10;
#[cfg(target_os = "linux")]
{
if let Ok(file) = File::create("/dev/null") {
while let Ok(bytes) = splice(
output.as_raw_fd(),
None,
file.as_raw_fd(),
None,
CHUNK_SIZE,
SpliceFFlags::empty(),
) {
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;
}
}
}
/// Execute the given command and return a timing summary /// Execute the given command and return a timing summary
pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> { pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {
let wallclock_timer = WallClockTimer::start(); let wallclock_timer = WallClockTimer::start();
#[cfg(not(windows))] #[cfg(not(windows))]
let ((time_user, time_system), status) = { let cpu_timer = self::unix_timer::CPUTimer::start();
let cpu_timer = self::unix_timer::CPUTimer::start();
let status = command.status()?; let mut child = command.spawn()?;
(cpu_timer.stop(), status)
};
#[cfg(windows)] #[cfg(windows)]
let ((time_user, time_system), status) = { let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);
let mut child = command.spawn()?;
let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);
let status = child.wait()?;
(cpu_timer.stop(), status) if let Some(output) = child.stdout.take() {
}; // Handle CommandOutputPolicy::Pipe
discard(output);
}
let status = child.wait()?;
let (time_user, time_system) = cpu_timer.stop();
let time_real = wallclock_timer.stop(); let time_real = wallclock_timer.stop();
Ok(TimerResult { Ok(TimerResult {