mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-20 03:11:28 +00:00
Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
623ce2d2d9 | ||
|
|
3df63ba2bb | ||
|
|
efdf735723 | ||
|
|
ecee9df575 | ||
|
|
dfd1046d63 | ||
|
|
277807cb5a | ||
|
|
f75f0a10fe | ||
|
|
692469659b | ||
|
|
eb0ccc85f1 | ||
|
|
2443383fc8 | ||
|
|
cb3a91ea2c | ||
|
|
251a697e30 | ||
|
|
b9240335f5 | ||
|
|
75460bb9b4 | ||
|
|
97163ce830 |
No files matched your search
@@ -60,7 +60,7 @@ jobs:
|
||||
- { target: x86_64-apple-darwin , os: macos-10.15 }
|
||||
- { target: x86_64-pc-windows-gnu , 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 }
|
||||
steps:
|
||||
- name: Checkout source code
|
||||
|
||||
+5
-13
@@ -1,19 +1,11 @@
|
||||
# unreleased
|
||||
# v1.14.0
|
||||
|
||||
## Features
|
||||
|
||||
|
||||
## Changes
|
||||
|
||||
|
||||
## Bugfixes
|
||||
|
||||
|
||||
## Other
|
||||
|
||||
|
||||
## Packaging
|
||||
|
||||
- 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)
|
||||
|
||||
|
||||
# v1.13.0
|
||||
|
||||
Generated
+45
-17
@@ -13,9 +13,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.56"
|
||||
version = "1.0.57"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4361135be9122e0870de935d7c439aef945b9f9ddd4199a553b5270b49c82a27"
|
||||
checksum = "08f9b8508dccb7687a1d6c4ce66b2b0ecef467c94667de27d8d7fe1f8d2a9cdc"
|
||||
|
||||
[[package]]
|
||||
name = "approx"
|
||||
@@ -98,15 +98,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "3.1.7"
|
||||
version = "3.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c67e7973e74896f4bba06ca2dcfd28d54f9cb8c035e940a32b88ed48f5f5ecf2"
|
||||
checksum = "535434c063ced786eb04aaf529308092c5ab60889e8fe24275d15de07b01fa97"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"bitflags",
|
||||
"clap_lex",
|
||||
"indexmap",
|
||||
"lazy_static",
|
||||
"os_str_bytes",
|
||||
"strsim",
|
||||
"termcolor",
|
||||
"terminal_size",
|
||||
@@ -122,6 +122,15 @@ dependencies = [
|
||||
"clap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213"
|
||||
dependencies = [
|
||||
"os_str_bytes",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cloudabi"
|
||||
version = "0.0.3"
|
||||
@@ -253,7 +262,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "hyperfine"
|
||||
version = "1.13.0"
|
||||
version = "1.14.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"approx",
|
||||
@@ -265,6 +274,7 @@ dependencies = [
|
||||
"csv",
|
||||
"indicatif",
|
||||
"libc",
|
||||
"nix",
|
||||
"predicates",
|
||||
"rand 0.8.5",
|
||||
"rust_decimal",
|
||||
@@ -337,9 +347,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.121"
|
||||
version = "0.2.125"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f"
|
||||
checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
@@ -347,6 +357,27 @@ version = "2.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
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]]
|
||||
name = "normalize-line-endings"
|
||||
version = "0.3.0"
|
||||
@@ -447,9 +478,6 @@ name = "os_str_bytes"
|
||||
version = "6.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
@@ -730,9 +758,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.79"
|
||||
version = "1.0.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95"
|
||||
checksum = "f972498cf015f7c0746cac89ebe1d6ef10c293b94175a243a2d9442c163d9944"
|
||||
dependencies = [
|
||||
"itoa 1.0.1",
|
||||
"ryu",
|
||||
@@ -822,18 +850,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.30"
|
||||
version = "1.0.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
|
||||
checksum = "bd829fe32373d27f76265620b5309d0340cb8550f523c1dda251d6298069069a"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.30"
|
||||
version = "1.0.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
|
||||
checksum = "0396bc89e626244658bef819e22d0cc459e795a5ebe878e6ec336d1674a8d79a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
||||
+4
-1
@@ -7,7 +7,7 @@ license = "MIT/Apache-2.0"
|
||||
name = "hyperfine"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/sharkdp/hyperfine"
|
||||
version = "1.13.0"
|
||||
version = "1.14.0"
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
|
||||
@@ -31,6 +31,9 @@ libc = "0.2"
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
winapi = { version = "0.3", features = ["processthreadsapi", "minwindef", "winnt"] }
|
||||
|
||||
[target.'cfg(target_os="linux")'.dependencies]
|
||||
nix = { version = "0.24.1", features = ["zerocopy"] }
|
||||
|
||||
[dependencies.clap]
|
||||
version = "3"
|
||||
default-features = false
|
||||
|
||||
@@ -36,10 +36,10 @@ pub trait Executor {
|
||||
fn run_command_and_measure_common(
|
||||
mut command: std::process::Command,
|
||||
command_failure_action: CmdFailureAction,
|
||||
command_output_policy: CommandOutputPolicy,
|
||||
command_output_policy: &CommandOutputPolicy,
|
||||
command_name: &str,
|
||||
) -> 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.env(
|
||||
@@ -83,7 +83,7 @@ impl<'a> Executor for RawExecutor<'a> {
|
||||
let result = run_command_and_measure_common(
|
||||
command.get_command()?,
|
||||
command_failure_action.unwrap_or(self.options.command_failure_action),
|
||||
self.options.command_output_policy,
|
||||
&self.options.command_output_policy,
|
||||
&command.get_command_line(),
|
||||
)?;
|
||||
|
||||
@@ -136,7 +136,7 @@ impl<'a> Executor for ShellExecutor<'a> {
|
||||
let mut result = run_command_and_measure_common(
|
||||
command_builder,
|
||||
command_failure_action.unwrap_or(self.options.command_failure_action),
|
||||
self.options.command_output_policy,
|
||||
&self.options.command_output_policy,
|
||||
&command.get_command_line(),
|
||||
)?;
|
||||
|
||||
|
||||
+24
@@ -241,6 +241,13 @@ fn build_command() -> Command<'static> {
|
||||
.value_name("FILE")
|
||||
.help("Export the timing summary statistics as a Markdown table to the given FILE."),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("export-orgmode")
|
||||
.long("export-orgmode")
|
||||
.takes_value(true)
|
||||
.value_name("FILE")
|
||||
.help("Export the timing summary statistics as a Emacs org-mode table to the given FILE."),
|
||||
)
|
||||
.arg(
|
||||
Arg::new("show-output")
|
||||
.long("show-output")
|
||||
@@ -252,6 +259,23 @@ fn build_command() -> Command<'static> {
|
||||
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::new("command-name")
|
||||
.long("command-name")
|
||||
|
||||
@@ -49,4 +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}'.")]
|
||||
UnknownOutputPolicy(String),
|
||||
}
|
||||
@@ -6,11 +6,13 @@ mod csv;
|
||||
mod json;
|
||||
mod markdown;
|
||||
mod markup;
|
||||
mod orgmode;
|
||||
|
||||
use self::asciidoc::AsciidocExporter;
|
||||
use self::csv::CsvExporter;
|
||||
use self::json::JsonExporter;
|
||||
use self::markdown::MarkdownExporter;
|
||||
use self::orgmode::OrgmodeExporter;
|
||||
|
||||
use crate::benchmark::benchmark_result::BenchmarkResult;
|
||||
use crate::util::units::Unit;
|
||||
@@ -32,6 +34,9 @@ pub enum ExportType {
|
||||
|
||||
/// Markdown table
|
||||
Markdown,
|
||||
|
||||
/// Emacs org-mode tables
|
||||
Orgmode,
|
||||
}
|
||||
|
||||
/// Interface for different exporters.
|
||||
@@ -67,6 +72,7 @@ impl ExportManager {
|
||||
add_exporter("export-json", ExportType::Json)?;
|
||||
add_exporter("export-csv", ExportType::Csv)?;
|
||||
add_exporter("export-markdown", ExportType::Markdown)?;
|
||||
add_exporter("export-orgmode", ExportType::Orgmode)?;
|
||||
}
|
||||
Ok(export_manager)
|
||||
}
|
||||
@@ -81,6 +87,7 @@ impl ExportManager {
|
||||
ExportType::Csv => Box::new(CsvExporter::default()),
|
||||
ExportType::Json => Box::new(JsonExporter::default()),
|
||||
ExportType::Markdown => Box::new(MarkdownExporter::default()),
|
||||
ExportType::Orgmode => Box::new(OrgmodeExporter::default()),
|
||||
};
|
||||
self.exporters.push(ExporterWithFilename {
|
||||
exporter,
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
use super::markup::Alignment;
|
||||
use crate::export::markup::MarkupExporter;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct OrgmodeExporter {}
|
||||
|
||||
impl MarkupExporter for OrgmodeExporter {
|
||||
fn table_row(&self, cells: &[&str]) -> String {
|
||||
format!(
|
||||
"| {} | {} |\n",
|
||||
cells.first().unwrap(),
|
||||
&cells[1..].join(" | ")
|
||||
)
|
||||
}
|
||||
|
||||
fn table_divider(&self, cell_aligmnents: &[Alignment]) -> String {
|
||||
format!("|{}--|\n", "--+".repeat(cell_aligmnents.len() - 1))
|
||||
}
|
||||
|
||||
fn command(&self, cmd: &str) -> String {
|
||||
format!("={}=", cmd)
|
||||
}
|
||||
}
|
||||
|
||||
/// Check Emacs org-mode data row formatting
|
||||
#[test]
|
||||
fn test_orgmode_formatter_table_data() {
|
||||
let exporter = OrgmodeExporter::default();
|
||||
|
||||
let actual = exporter.table_row(&["a", "b", "c"]);
|
||||
let expect = "| a | b | c |\n";
|
||||
|
||||
assert_eq!(expect, actual);
|
||||
}
|
||||
|
||||
/// Check Emacs org-mode horizontal line formatting
|
||||
#[test]
|
||||
fn test_orgmode_formatter_table_line() {
|
||||
let exporter = OrgmodeExporter::default();
|
||||
|
||||
let actual = exporter.table_divider(&[
|
||||
Alignment::Left,
|
||||
Alignment::Left,
|
||||
Alignment::Left,
|
||||
Alignment::Left,
|
||||
Alignment::Left,
|
||||
]);
|
||||
let expect = "|--+--+--+--+--|\n";
|
||||
|
||||
assert_eq!(expect, actual);
|
||||
}
|
||||
|
||||
/// Test helper function to create unit-based header and horizontal line
|
||||
/// independently from the markup functionality for Emacs org-mode.
|
||||
#[cfg(test)]
|
||||
fn cfg_test_table_header(unit_short_name: String) -> String {
|
||||
format!(
|
||||
"| Command | Mean [{unit}] | Min [{unit}] | Max [{unit}] | Relative |\n|--+--+--+--+--|\n",
|
||||
unit = unit_short_name
|
||||
)
|
||||
}
|
||||
|
||||
/// Ensure the Emacs org-mode output includes the table header and the multiple
|
||||
/// benchmark results as a table. The list of actual times is not included
|
||||
/// in the output.
|
||||
///
|
||||
/// This also demonstrates that the first entry's units (ms) are used to set
|
||||
/// the units for all entries when the time unit is not given.
|
||||
#[test]
|
||||
fn test_orgmode_format_ms() {
|
||||
use super::Exporter;
|
||||
use crate::benchmark::benchmark_result::BenchmarkResult;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
let exporter = OrgmodeExporter::default();
|
||||
|
||||
let results = vec![
|
||||
BenchmarkResult {
|
||||
command: String::from("sleep 0.1"),
|
||||
mean: 0.1057,
|
||||
stddev: Some(0.0016),
|
||||
median: 0.1057,
|
||||
user: 0.0009,
|
||||
system: 0.0011,
|
||||
min: 0.1023,
|
||||
max: 0.1080,
|
||||
times: Some(vec![0.1, 0.1, 0.1]),
|
||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||
parameters: BTreeMap::new(),
|
||||
},
|
||||
BenchmarkResult {
|
||||
command: String::from("sleep 2"),
|
||||
mean: 2.0050,
|
||||
stddev: Some(0.0020),
|
||||
median: 2.0050,
|
||||
user: 0.0009,
|
||||
system: 0.0012,
|
||||
min: 2.0020,
|
||||
max: 2.0080,
|
||||
times: Some(vec![2.0, 2.0, 2.0]),
|
||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||
parameters: BTreeMap::new(),
|
||||
},
|
||||
];
|
||||
|
||||
let actual = String::from_utf8(exporter.serialize(&results, None).unwrap()).unwrap();
|
||||
let expect = format!(
|
||||
"{}\
|
||||
| =sleep 0.1= | 105.7 ± 1.6 | 102.3 | 108.0 | 1.00 |
|
||||
| =sleep 2= | 2005.0 ± 2.0 | 2002.0 | 2008.0 | 18.97 ± 0.29 |
|
||||
",
|
||||
cfg_test_table_header("ms".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(expect, actual);
|
||||
}
|
||||
|
||||
/// This test demonstrates that the given unit (s) is used to set
|
||||
/// the units for all entries.
|
||||
#[test]
|
||||
fn test_orgmode_format_s() {
|
||||
use super::Exporter;
|
||||
use crate::benchmark::benchmark_result::BenchmarkResult;
|
||||
use crate::util::units::Unit;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
let exporter = OrgmodeExporter::default();
|
||||
|
||||
let results = vec![
|
||||
BenchmarkResult {
|
||||
command: String::from("sleep 2"),
|
||||
mean: 2.0050,
|
||||
stddev: Some(0.0020),
|
||||
median: 2.0050,
|
||||
user: 0.0009,
|
||||
system: 0.0012,
|
||||
min: 2.0020,
|
||||
max: 2.0080,
|
||||
times: Some(vec![2.0, 2.0, 2.0]),
|
||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||
parameters: BTreeMap::new(),
|
||||
},
|
||||
BenchmarkResult {
|
||||
command: String::from("sleep 0.1"),
|
||||
mean: 0.1057,
|
||||
stddev: Some(0.0016),
|
||||
median: 0.1057,
|
||||
user: 0.0009,
|
||||
system: 0.0011,
|
||||
min: 0.1023,
|
||||
max: 0.1080,
|
||||
times: Some(vec![0.1, 0.1, 0.1]),
|
||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||
parameters: BTreeMap::new(),
|
||||
},
|
||||
];
|
||||
|
||||
let actual =
|
||||
String::from_utf8(exporter.serialize(&results, Some(Unit::Second)).unwrap()).unwrap();
|
||||
let expect = format!(
|
||||
"{}\
|
||||
| =sleep 2= | 2.005 ± 0.002 | 2.002 | 2.008 | 18.97 ± 0.29 |
|
||||
| =sleep 0.1= | 0.106 ± 0.002 | 0.102 | 0.108 | 1.00 |
|
||||
",
|
||||
cfg_test_table_header("s".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(expect, actual);
|
||||
}
|
||||
+50
-18
@@ -1,5 +1,7 @@
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::{cmp, fmt};
|
||||
use std::{cmp, fmt, io};
|
||||
|
||||
use anyhow::ensure;
|
||||
use atty::Stream;
|
||||
@@ -109,27 +111,44 @@ impl Default for RunBounds {
|
||||
}
|
||||
|
||||
/// How to handle the output of benchmarked commands
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[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,
|
||||
|
||||
/// Redirect output to a file
|
||||
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) -> (Stdio, Stdio) {
|
||||
match self {
|
||||
CommandOutputPolicy::Discard => (Stdio::null(), Stdio::null()),
|
||||
CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()),
|
||||
}
|
||||
pub fn get_stdout_stderr(&self) -> io::Result<(Stdio, Stdio)> {
|
||||
let streams = match self {
|
||||
CommandOutputPolicy::Null => (Stdio::null(), Stdio::null()),
|
||||
|
||||
// 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,
|
||||
output_style: OutputStyleOption::Full,
|
||||
executor_kind: ExecutorKind::default(),
|
||||
command_output_policy: CommandOutputPolicy::Discard,
|
||||
command_output_policy: CommandOutputPolicy::Null,
|
||||
time_unit: None,
|
||||
}
|
||||
}
|
||||
@@ -250,9 +269,22 @@ 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::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 {
|
||||
CommandOutputPolicy::Discard
|
||||
CommandOutputPolicy::Null
|
||||
};
|
||||
|
||||
options.output_style = match matches.value_of("style") {
|
||||
@@ -262,12 +294,12 @@ impl Options {
|
||||
Some("color") => OutputStyleOption::Color,
|
||||
Some("none") => OutputStyleOption::Disabled,
|
||||
_ => {
|
||||
if options.command_output_policy == CommandOutputPolicy::Discard
|
||||
&& atty::is(Stream::Stdout)
|
||||
if options.command_output_policy == CommandOutputPolicy::Inherit
|
||||
|| !atty::is(Stream::Stdout)
|
||||
{
|
||||
OutputStyleOption::Full
|
||||
} else {
|
||||
OutputStyleOption::Basic
|
||||
} else {
|
||||
OutputStyleOption::Full
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+51
-12
@@ -6,10 +6,18 @@ mod windows_timer;
|
||||
#[cfg(not(windows))]
|
||||
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 wall_clock_timer::WallClockTimer;
|
||||
|
||||
use std::process::{Command, ExitStatus};
|
||||
use std::io::Read;
|
||||
use std::process::{ChildStdout, Command, ExitStatus};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
@@ -33,26 +41,57 @@ pub struct TimerResult {
|
||||
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
|
||||
pub fn execute_and_measure(mut command: Command) -> Result<TimerResult> {
|
||||
let wallclock_timer = WallClockTimer::start();
|
||||
|
||||
#[cfg(not(windows))]
|
||||
let ((time_user, time_system), status) = {
|
||||
let cpu_timer = self::unix_timer::CPUTimer::start();
|
||||
let status = command.status()?;
|
||||
(cpu_timer.stop(), status)
|
||||
};
|
||||
let cpu_timer = self::unix_timer::CPUTimer::start();
|
||||
|
||||
let mut child = command.spawn()?;
|
||||
|
||||
#[cfg(windows)]
|
||||
let ((time_user, time_system), status) = {
|
||||
let mut child = command.spawn()?;
|
||||
let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);
|
||||
let status = child.wait()?;
|
||||
let cpu_timer = self::windows_timer::CPUTimer::start_for_process(&child);
|
||||
|
||||
(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();
|
||||
|
||||
Ok(TimerResult {
|
||||
|
||||
Reference in new issue
Block a user