Skip to content
Myr

executor.rs

This file

Path

src/benchmark/executor.rs

361 lines · 11.2 KiB · Rust

Last changed by

Document JSON measurements and omit unavailable memory metricsDavid Peterbefore adoption4cd0e24

History of this file

Take it

The walk stopped after 200 commits: lines older than ed85ac2 point at it.

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 1–4why, one step back
1#[cfg(windows)]
2use std::os::windows::process::CommandExt;3use std::process::ExitStatus;4

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

lines 5–6why, one step back
5use crate::benchmark::measurement::Measurement;
6use crate::benchmark::measurement::Measurements;

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 7–11why, one step back
7use crate::command::Command;
8use crate::options::{9 CmdFailureAction, CommandInputPolicy, CommandOutputPolicy, Options, OutputStyleOption, Shell,10};11use crate::output::progress_bar::get_progress_bar;

CleanupDavid Peterbefore adoption9c2ee56

line 12why, one step back
12use crate::quantity::{second, Information, Time, Zero};

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 13why, one step back
13use crate::timer::execute_and_measure;

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 14–17why, one step back
14use crate::util::randomized_environment_offset;
1516use anyhow::{bail, Context, Result};17

Merge pull request #807 from willcl-ark/expose-run-varDavid Peterbefore adoption30a2b61

line 18why, one step back
18#[derive(Clone, Copy)]

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

lines 19–34why, one step back
19pub enum BenchmarkIteration {
20 NonBenchmarkRun,21 Warmup(u64),22 Benchmark(u64),23}2425impl BenchmarkIteration {26 pub fn to_env_var_value(&self) -> Option<String> {27 match self {28 BenchmarkIteration::NonBenchmarkRun => None,29 BenchmarkIteration::Warmup(i) => Some(format!("warmup-{}", i)),30 BenchmarkIteration::Benchmark(i) => Some(format!("{}", i)),31 }32 }33}34

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 35–39why, one step back
35pub trait Executor {
36 /// Run the given command and measure the execution time37 fn run_command_and_measure(38 &self,39 command: &Command<'_>,

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

line 40why, one step back
40 iteration: BenchmarkIteration,

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 41why, one step back
41 command_failure_action: Option<CmdFailureAction>,

Multiple output policiesDavid Peterbefore adoption99d24b7

line 42why, one step back
42 output_policy: &CommandOutputPolicy,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 43why, one step back
43 ) -> Result<Measurement>;

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 44–54why, one step back
44
45 /// Perform a calibration of this executor. For example,46 /// when running commands through a shell, we need to47 /// measure the shell spawning time separately in order48 /// to subtract it from the full runtime later.49 fn calibrate(&mut self) -> Result<()>;5051 /// Return the time overhead for this executor when52 /// performing a measurement. This should return the time53 /// that is being used in addition to the actual runtime54 /// of the command.

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 55why, one step back
55 fn time_overhead(&self) -> Time;

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 56–59why, one step back
56}
5758fn run_command_and_measure_common(59 mut command: std::process::Command,

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

line 60why, one step back
60 iteration: BenchmarkIteration,

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 61–64why, one step back
61 command_failure_action: CmdFailureAction,
62 command_input_policy: &CommandInputPolicy,63 command_output_policy: &CommandOutputPolicy,64 command_name: &str,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 65why, one step back
65) -> Result<Measurement> {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 66–74why, one step back
66 let stdin = command_input_policy.get_stdin()?;
67 let (stdout, stderr) = command_output_policy.get_stdout_stderr()?;68 command.stdin(stdin).stdout(stdout).stderr(stderr);6970 command.env(71 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",72 randomized_environment_offset::value(),73 );74

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

lines 75–78why, one step back
75 if let Some(value) = iteration.to_env_var_value() {
76 command.env("HYPERFINE_ITERATION", value);77 }78

RefactoringDavid Peterbefore adoption71365c2

line 79why, one step back
79 let measurement = execute_and_measure(command)

style: simplify string interpolationHamir Mahalbefore adoptionef12632

line 80why, one step back
80 .with_context(|| format!("Failed to run command '{command_name}'"))?;

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 81why, one step back
81

Resolve overlap with merged measurement refactors and current masterDavid Peterbefore adoptioncf38199

lines 82–83why, one step back
82 if !measurement.exit_status.success() {
83 use crate::util::exit_code::extract_exit_code;

Support list of exit codes in --ignore-failureDavid Peterbefore adoptionb5a6860

lines 84–89why, one step back
84
85 let should_fail = match command_failure_action {86 CmdFailureAction::RaiseError => true,87 CmdFailureAction::IgnoreAllFailures => false,88 CmdFailureAction::IgnoreSpecificFailures(ref codes) => {89 // Only fail if the exit code is not in the list of codes to ignore

Resolve overlap with merged measurement refactors and current masterDavid Peterbefore adoptioncf38199

line 90why, one step back
90 if let Some(exit_code) = extract_exit_code(measurement.exit_status) {

Support list of exit codes in --ignore-failureDavid Peterbefore adoptionb5a6860

lines 91–96why, one step back
91 !codes.contains(&exit_code)
92 } else {93 // If we can't extract an exit code, treat it as a failure94 true95 }96 }

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

lines 97–98why, one step back
97 };
98

Support list of exit codes in --ignore-failureDavid Peterbefore adoptionb5a6860

lines 99–109why, one step back
99 if should_fail {
100 let when = match iteration {101 BenchmarkIteration::NonBenchmarkRun => "a non-benchmark run".to_string(),102 BenchmarkIteration::Warmup(0) => "the first warmup run".to_string(),103 BenchmarkIteration::Warmup(i) => format!("warmup iteration {i}"),104 BenchmarkIteration::Benchmark(0) => "the first benchmark run".to_string(),105 BenchmarkIteration::Benchmark(i) => format!("benchmark iteration {i}"),106 };107 bail!(108 "{cause} in {when}. Use the '-i'/'--ignore-failure' option if you want to ignore this. \109 Alternatively, use the '--show-output' option to debug what went wrong.",

Resolve overlap with merged measurement refactors and current masterDavid Peterbefore adoptioncf38199

line 110why, one step back
110 cause=measurement.exit_status.code().map_or(

Support list of exit codes in --ignore-failureDavid Peterbefore adoptionb5a6860

lines 111–116why, one step back
111 "The process has been terminated by a signal".into(),
112 |c| format!("Command terminated with non-zero exit code {c}")113114 ),115 );116 }

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 117–118why, one step back
117 }
118

RefactoringDavid Peterbefore adoption71365c2

line 119why, one step back
119 Ok(measurement)

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 120–131why, one step back
120}
121122pub struct RawExecutor<'a> {123 options: &'a Options,124}125126impl<'a> RawExecutor<'a> {127 pub fn new(options: &'a Options) -> Self {128 RawExecutor { options }129 }130}131

apply clippy advices on executor/commands lifetimes.David Carlierbefore adoption2899deb

line 132why, one step back
132impl Executor for RawExecutor<'_> {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 133–135why, one step back
133 fn run_command_and_measure(
134 &self,135 command: &Command<'_>,

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

line 136why, one step back
136 iteration: BenchmarkIteration,

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 137why, one step back
137 command_failure_action: Option<CmdFailureAction>,

Multiple output policiesDavid Peterbefore adoption99d24b7

line 138why, one step back
138 output_policy: &CommandOutputPolicy,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 139why, one step back
139 ) -> Result<Measurement> {

RefactoringDavid Peterbefore adoption71365c2

line 140why, one step back
140 run_command_and_measure_common(

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 141why, one step back
141 command.get_command()?,

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

line 142why, one step back
142 iteration,

Support list of exit codes in --ignore-failureDavid Peterbefore adoptionb5a6860

line 143why, one step back
143 command_failure_action.unwrap_or_else(|| self.options.command_failure_action.clone()),

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 144why, one step back
144 &self.options.command_input_policy,

Multiple output policiesDavid Peterbefore adoption99d24b7

line 145why, one step back
145 output_policy,

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 146why, one step back
146 &command.get_command_line(),

RefactoringDavid Peterbefore adoption71365c2

line 147why, one step back
147 )

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 148–153why, one step back
148 }
149150 fn calibrate(&mut self) -> Result<()> {151 Ok(())152 }153

Use uomDavid Peterbefore adoption641451a

lines 154–155why, one step back
154 fn time_overhead(&self) -> Time {
155 Time::zero()

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 156–161why, one step back
156 }
157}158159pub struct ShellExecutor<'a> {160 options: &'a Options,161 shell: &'a Shell,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 162why, one step back
162 shell_spawning_time: Option<Measurement>,

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 163–174why, one step back
163}
164165impl<'a> ShellExecutor<'a> {166 pub fn new(shell: &'a Shell, options: &'a Options) -> Self {167 ShellExecutor {168 shell,169 options,170 shell_spawning_time: None,171 }172 }173}174

apply clippy advices on executor/commands lifetimes.David Carlierbefore adoption2899deb

line 175why, one step back
175impl Executor for ShellExecutor<'_> {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 176–178why, one step back
176 fn run_command_and_measure(
177 &self,178 command: &Command<'_>,

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

line 179why, one step back
179 iteration: BenchmarkIteration,

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 180why, one step back
180 command_failure_action: Option<CmdFailureAction>,

Multiple output policiesDavid Peterbefore adoption99d24b7

line 181why, one step back
181 output_policy: &CommandOutputPolicy,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 182why, one step back
182 ) -> Result<Measurement> {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 183–194why, one step back
183 let on_windows_cmd = cfg!(windows) && *self.shell == Shell::Default("cmd.exe");
184 let mut command_builder = self.shell.command();185 command_builder.arg(if on_windows_cmd { "/C" } else { "-c" });186187 // Windows needs special treatment for its behavior on parsing cmd arguments188 if on_windows_cmd {189 #[cfg(windows)]190 command_builder.raw_arg(command.get_command_line());191 } else {192 command_builder.arg(command.get_command_line());193 }194

RefactoringDavid Peterbefore adoption71365c2

line 195why, one step back
195 let mut measurement = run_command_and_measure_common(

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 196why, one step back
196 command_builder,

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

line 197why, one step back
197 iteration,

Support list of exit codes in --ignore-failureDavid Peterbefore adoptionb5a6860

line 198why, one step back
198 command_failure_action.unwrap_or_else(|| self.options.command_failure_action.clone()),

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 199why, one step back
199 &self.options.command_input_policy,

Multiple output policiesDavid Peterbefore adoption99d24b7

line 200why, one step back
200 output_policy,

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 201–204why, one step back
201 &command.get_command_line(),
202 )?;203204 // Subtract shell spawning time

Use uomDavid Peterbefore adoption641451a

lines 205–207why, one step back
205 fn ensure_non_negative(time: Time) -> Time {
206 if time < Time::zero() {207 Time::zero()

Switch to unit-safe quantitiesDavid Peterbefore adoptionf9aa299

lines 208–212why, one step back
208 } else {
209 time210 }211 }212

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

line 213why, one step back
213 if let Some(ref spawning_time) = self.shell_spawning_time {

RefactoringDavid Peterbefore adoption71365c2

lines 214–219why, one step back
214 measurement.time_wall_clock =
215 ensure_non_negative(measurement.time_wall_clock - spawning_time.time_wall_clock);216 measurement.time_user =217 ensure_non_negative(measurement.time_user - spawning_time.time_user);218 measurement.time_system =219 ensure_non_negative(measurement.time_system - spawning_time.time_system);

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 220–221why, one step back
220 }
221

RefactoringDavid Peterbefore adoption71365c2

line 222why, one step back
222 Ok(measurement)

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 223–237why, one step back
223 }
224225 /// Measure the average shell spawning time226 fn calibrate(&mut self) -> Result<()> {227 const COUNT: u64 = 50;228 let progress_bar = if self.options.output_style != OutputStyleOption::Disabled {229 Some(get_progress_bar(230 COUNT,231 "Measuring shell spawning time",232 self.options.output_style,233 ))234 } else {235 None236 };237

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

line 238why, one step back
238 let mut measurements = Measurements::default();

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 239–241why, one step back
239
240 for _ in 0..COUNT {241 // Just run the shell without any command

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

line 242why, one step back
242 let measurement = self.run_command_and_measure(

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

lines 243–245why, one step back
243 &Command::new(None, ""),
244 BenchmarkIteration::NonBenchmarkRun,245 None,

Multiple output policiesDavid Peterbefore adoption99d24b7

line 246why, one step back
246 &CommandOutputPolicy::Null,

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

line 247why, one step back
247 );

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 248why, one step back
248

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

line 249why, one step back
249 match measurement {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 250–261why, one step back
250 Err(_) => {
251 let shell_cmd = if cfg!(windows) {252 format!("{} /C \"\"", self.shell)253 } else {254 format!("{} -c \"\"", self.shell)255 };256257 bail!(258 "Could not measure shell execution time. Make sure you can run '{}'.",259 shell_cmd260 );261 }

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

lines 262–263why, one step back
262 Ok(result) => {
263 measurements.push(result);

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 264–275why, one step back
264 }
265 }266267 if let Some(bar) = progress_bar.as_ref() {268 bar.inc(1)269 }270 }271272 if let Some(bar) = progress_bar.as_ref() {273 bar.finish_and_clear()274 }275

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

lines 276–279why, one step back
276 self.shell_spawning_time = Some(Measurement {
277 time_wall_clock: measurements.time_wall_clock_mean(),278 time_user: measurements.time_user_mean(),279 time_system: measurements.time_system_mean(),

Document JSON measurements and omit unavailable memory metricsDavid Peterbefore adoption4cd0e24

line 280why, one step back
280 peak_memory_usage: None,

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

line 281why, one step back
281 exit_status: ExitStatus::default(),

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 282–286why, one step back
282 });
283284 Ok(())285 }286

Use uomDavid Peterbefore adoption641451a

line 287why, one step back
287 fn time_overhead(&self) -> Time {

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

line 288why, one step back
288 self.shell_spawning_time.as_ref().unwrap().time_wall_clock

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 289–301why, one step back
289 }
290}291292#[derive(Clone)]293pub struct MockExecutor {294 shell: Option<String>,295}296297impl MockExecutor {298 pub fn new(shell: Option<String>) -> Self {299 MockExecutor { shell }300 }301

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 302why, one step back
302 fn extract_time<S: AsRef<str>>(sleep_command: S) -> Time {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 303why, one step back
303 assert!(sleep_command.as_ref().starts_with("sleep "));

Remove alternative constructorsDavid Peterbefore adoption3494c8a

line 304why, one step back
304 Time::new::<second>(

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

lines 305–310why, one step back
305 sleep_command
306 .as_ref()307 .trim_start_matches("sleep ")308 .parse::<f64>()309 .unwrap(),310 )

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 311–317why, one step back
311 }
312}313314impl Executor for MockExecutor {315 fn run_command_and_measure(316 &self,317 command: &Command<'_>,

Add iteration information to failure error messageDavid Peterbefore adoption83f04cd

line 318why, one step back
318 _iteration: BenchmarkIteration,

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 319why, one step back
319 _command_failure_action: Option<CmdFailureAction>,

Multiple output policiesDavid Peterbefore adoption99d24b7

line 320why, one step back
320 _output_policy: &CommandOutputPolicy,

Merge pull request #954 from sharkdp/codex/remove-statisticalDavid Peterbefore adoption750fe11

line 321why, one step back
321 ) -> Result<Measurement> {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 322why, one step back
322 #[cfg(unix)]

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

line 323why, one step back
323 let exit_status = {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 324–328why, one step back
324 use std::os::unix::process::ExitStatusExt;
325 ExitStatus::from_raw(0)326 };327328 #[cfg(windows)]

Merge pull request #958 from sharkdp/codex/measurements-collectionDavid Peterbefore adoption16e23a1

line 329why, one step back
329 let exit_status = {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 330–333why, one step back
330 use std::os::windows::process::ExitStatusExt;
331 ExitStatus::from_raw(0)332 };333

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

lines 334–335why, one step back
334 Ok(Measurement {
335 time_wall_clock: Self::extract_time(command.get_command_line()),

Use uomDavid Peterbefore adoption641451a

lines 336–337why, one step back
336 time_user: Time::zero(),
337 time_system: Time::zero(),

Document JSON measurements and omit unavailable memory metricsDavid Peterbefore adoption4cd0e24

line 338why, one step back
338 peak_memory_usage: Some(Information::zero()),

Unify TimingResult/TimerResult/MeasurementDavid Peterbefore adoption42e75c1

lines 339–340why, one step back
339 exit_status,
340 })

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 341–346why, one step back
341 }
342343 fn calibrate(&mut self) -> Result<()> {344 Ok(())345 }346

Merge pull request #961 from sharkdp/codex/uom-quantitiesDavid Peterbefore adoption975a25c

line 347why, one step back
347 fn time_overhead(&self) -> Time {

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 348why, one step back
348 match &self.shell {

Use uomDavid Peterbefore adoption641451a

line 349why, one step back
349 None => Time::zero(),

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

lines 350–356why, one step back
350 Some(shell) => Self::extract_time(shell),
351 }352 }353}354355#[test]356fn test_mock_executor_extract_time() {

Use uomDavid Peterbefore adoption641451a

lines 357–358why, one step back
357 assert_eq!(
358 MockExecutor::extract_time("sleep 0.1"),

Remove alternative constructorsDavid Peterbefore adoption3494c8a

line 359why, one step back
359 Time::new::<second>(0.1)

Use uomDavid Peterbefore adoption641451a

line 360why, one step back
360 );

Bump serde from 1.0.179 to 1.0.188dependabot[bot]before adoptioned85ac2

line 361why, one step back
361}