Skip to content
Myr

Use raw executor by default, skip intermediate shell

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

This adds a new "raw executor" (next to the "shell executor") that allows hyperfine to execute commands directly without any intermediate shell. The command line is split into tokens (using the `shell-words` crate), and according to POSIX rules. The first token is taken as the executable, and the rest as arguments. The new executor is enabled by default. In order to select the shell executor, users will have to pass `--shell=default`. This allows us to reduce measurement noise and to benchmark very quick commands. It also decreases the time to run benchmarks, as we don't need the calibration phase. Also, it allows one to make sure that the executed command is not implemented as a shell builtin. For example `hyperfine true` and `hyperfine --shell=default true` return different times due to the fact that `bash` executes `true` as a NOP.

authored by David Peter · committed by David Peter · parent f2f1dd9 · Browse files · 8 files+88 added−8 removed
This commit

State

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

Trailers

  • Co-authored: Ciprian Dorin Craciun <ciprian@volution.ro>

Diff stats

8 files+88 added−8 removed

  • tests:+21 added−3 removed
  • other:+67 added−5 removed

8 files+88 added−8 removed

modified:src/benchmark/executor.rs+42 added
62 unmodified lines
6364 Ok(result)65}6667pub struct RawExecutor<'a> {68 options: &'a Options,69}7071impl<'a> RawExecutor<'a> {72 pub fn new(options: &'a Options) -> Self {73 RawExecutor { options }74 }75}7677impl<'a> Executor for RawExecutor<'a> {78 fn run_command_and_measure(79 &self,80 command: &Command<'_>,81 command_failure_action: Option<CmdFailureAction>,82 ) -> Result<(TimingResult, ExitStatus)> {83 let result = run_command_and_measure_common(84 command.get_command()?,85 command_failure_action.unwrap_or(self.options.command_failure_action),86 self.options.command_output_policy,87 &command.get_command_line(),88 )?;8990 Ok((91 TimingResult {92 time_real: result.time_real,93 time_user: result.time_user,94 time_system: result.time_system,95 },96 result.status,97 ))98 }99100 fn calibrate(&mut self) -> Result<()> {101 Ok(())102 }103104 fn time_overhead(&self) -> Second {105 0.0106 }107}108109pub struct ShellExecutor<'a> {110 options: &'a Options,111 shell: &'a Shell,112 shell_spawning_time: Option<TimingResult>,
175 unmodified lines
modified:src/benchmark/mod.rs+4 added-2 removed
5 unmodified lines
67use std::cmp;89use crate::command::Command;10use crate::options::{CmdFailureAction, Options, OutputStyleOption};10use crate::options::{CmdFailureAction, ExecutorKind, Options, OutputStyleOption};11use crate::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};12use crate::output::format::{format_duration, format_duration_unit};13use crate::output::progress_bar::get_progress_bar;14use crate::output::warnings::Warnings;
304 unmodified lines
319 // Warnings320 let mut warnings = vec![];321322 // Check execution time323 if times_real.iter().any(|&t| t < MIN_EXECUTION_TIME) {323 if matches!(self.options.executor_kind, ExecutorKind::Shell(_))324 && times_real.iter().any(|&t| t < MIN_EXECUTION_TIME)325 {326 warnings.push(Warnings::FastExecutionTime);327 }328329 // Check programm exit codes
46 unmodified lines
modified:src/benchmark/scheduler.rs+2 added-1 removed
1use colored::*;23use super::benchmark_result::BenchmarkResult;4use super::executor::{Executor, MockExecutor, ShellExecutor};4use super::executor::{Executor, MockExecutor, RawExecutor, ShellExecutor};5use super::{relative_speed, Benchmark};67use crate::command::Commands;8use crate::export::ExportManager;
23 unmodified lines
32 }3334 pub fn run_benchmarks(&mut self) -> Result<()> {35 let mut executor: Box<dyn Executor> = match self.options.executor_kind {36 ExecutorKind::Raw => Box::new(RawExecutor::new(self.options)),37 ExecutorKind::Mock(ref shell) => Box::new(MockExecutor::new(shell.clone())),38 ExecutorKind::Shell(ref shell) => Box::new(ShellExecutor::new(shell, self.options)),39 };40
57 unmodified lines
modified:src/command.rs+16 added-1 removed
13 unmodified lines
1415use crate::parameter::tokenize::tokenize;16use crate::parameter::ParameterValue;1718use anyhow::{bail, Result};18use anyhow::{bail, Context, Result};19use clap::Values;20use rust_decimal::Decimal;2122/// A command that should be benchmarked.
40 unmodified lines
63 pub fn get_command_line(&self) -> String {64 self.replace_parameters_in(self.expression)65 }6667 pub fn get_command(&self) -> Result<std::process::Command> {68 let command_line = self.get_command_line();69 let mut tokens = shell_words::split(&command_line)70 .with_context(|| format!("Failed to parse command '{}'", command_line))?71 .into_iter();7273 if let Some(program_name) = tokens.next() {74 let mut command_builder = std::process::Command::new(program_name);75 command_builder.args(tokens);76 Ok(command_builder)77 } else {78 bail!("Can not execute empty command")79 }80 }8182 pub fn get_parameters(&self) -> &[(&'a str, ParameterValue)] {83 &self.parameters84 }85
452 unmodified lines
modified:src/options.rs+3 added-1 removed
132 unmodified lines
133 }134}135136pub enum ExecutorKind {137 Raw,138 Shell(Shell),139 Mock(Option<String>),140}141
141 unmodified lines
283 };284285 options.executor_kind = match (matches.is_present("debug-mode"), matches.value_of("shell"))286 {287 (false, Some(shell)) if shell == "default" => ExecutorKind::Shell(Shell::default()),288 (false, Some(shell)) => ExecutorKind::Shell(Shell::parse_from_str(shell)?),287 (false, None) => ExecutorKind::Shell(Shell::default()),289 (false, None) => ExecutorKind::Raw,290 (true, Some(shell)) => ExecutorKind::Mock(Some(shell.into())),291 (true, None) => ExecutorKind::Mock(None),292 };293
70 unmodified lines
modified:tests/common.rs+6 added
9 unmodified lines
1011pub fn hyperfine() -> assert_cmd::Command {12 assert_cmd::Command::from_std(hyperfine_raw_command())13}1415pub fn hyperfine_shell() -> assert_cmd::Command {16 let mut cmd = hyperfine();17 cmd.arg("--shell=default");18 cmd19}
modified:tests/execution_order_tests.rs+2 added-2 removed
1 unmodified line
23use tempfile::{tempdir, TempDir};45mod common;6use common::hyperfine;6use common::hyperfine_shell;78struct ExecutionOrderTest {9 cmd: assert_cmd::Command,10 expected_content: String,
7 unmodified lines
18 let tempdir = tempdir().unwrap();19 let logfile_path = tempdir.path().join("output.log");2021 ExecutionOrderTest {22 cmd: hyperfine(),22 cmd: hyperfine_shell(),23 expected_content: String::new(),24 logfile_path,25 tempdir,26 }
252 unmodified lines
modified:tests/integration_tests.rs+13 added-1 removed
1mod common;2use common::hyperfine;2use common::{hyperfine, hyperfine_shell};34use predicates::prelude::*;56pub fn hyperfine_debug() -> assert_cmd::Command {
78 unmodified lines
85 .arg("some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")86 .assert()87 .failure()88 .stderr(predicate::str::contains(89 "Failed to run command 'some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577'",90 ));91}9293#[test]94fn fails_for_unknown_shell_command() {95 hyperfine_shell()96 .arg("--runs=1")97 .arg("some-nonexisting-program-b5d9574198b7e4b12a71fa4747c0a577")98 .assert()99 .failure()100 .stderr(predicate::str::contains(101 "Command terminated with non-zero exit code",102 ));103}104
190 unmodified lines