add cleanup command which is executed at the end of each benchmark
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Colin Wahl · committed by David Peter · parent e3dfc7f · Browse files · 4 files·+66 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+66 added−5 removed
- other:+66 added−5 removed
Provenance
- Commit
5af43cf- Tree
c433d71- Author
Colin Wahl <cwahl@awakesecurity.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
e3dfc7f
4 files changed+66 added−5 removed
modified:src/hyperfine/app.rs+11 added
81 unmodified lines82 clearing disk caches, for example.",83 ),84 )85 .arg(86+ Arg::with_name("cleanup")87+ .long("cleanup")88+ .short("c")89+ .takes_value(true)90+ .value_name("CMD")91+ .help(92+ "Execute CMD after each benchmark run. This is useful for \93+ cleaning up resources allocated during prepare.",94+ ),95+ )96+ .arg(97 Arg::with_name("parameter-scan")98 .long("parameter-scan")99 .short("P")100 .takes_value(true)81 unmodified linesmodified:src/hyperfine/benchmark.rs+49 added-5 removed
142 unmodified lines143 time_system: mean(×_system),144 })145 }146 147-/// Run the command specified by `--prepare`.148-fn run_preparation_command(147+fn run_intermediate_command(148 shell: &str,149 command: &Option<Command>,150 show_output: bool,151+ error_output: &'static str,152 ) -> io::Result<TimingResult> {153 if let &Some(ref cmd) = command {154 let res = time_shell_command(shell, cmd, show_output, CmdFailureAction::RaiseError, None);155 if res.is_err() {156 return Err(io::Error::new(157 io::ErrorKind::Other,158- "The preparation command terminated with a non-zero exit code. \159- Append ' || true' to the command if you are sure that this can be ignored.",158+ error_output159 ));160 }161 return res.map(|r| r.0);162 }163 Ok(TimingResult {164 ..Default::default()165 })166+167 }168 169+/// Run the command specified by `--prepare`.170+fn run_preparation_command(171+ shell: &str,172+ command: &Option<Command>,173+ show_output: bool,174+) -> io::Result<TimingResult> {175+ let error_output = "The preparation command terminated with a non-zero exit code. \176+ Append ' || true' to the command if you are sure that this can be ignored.";177+178+ run_intermediate_command(179+ shell,180+ command,181+ show_output,182+ error_output183+ )184+}185+186+/// Run the command specified by `--cleanup`.187+fn run_cleanup_command(188+ shell: &str,189+ command: &Option<Command>,190+ show_output: bool,191+) -> io::Result<TimingResult> {192+ let error_output = "The cleanup command terminated with a non-zero exit code. \193+ Append ' || true' to the command if you are sure that this can be ignored.";194+195+ run_intermediate_command(196+ shell,197+ command,198+ show_output,199+ error_output200+ )201+}202+203 /// Run the benchmark for a single shell command204 pub fn run_benchmark(205 num: usize,206 cmd: &Command,39 unmodified lines246 "Initial time measurement",247 &options.output_style,248 );249 216- // Run init / cleanup command250+ // Run init command251 let prepare_cmd = options252 .preparation_command253 .as_ref()254 .map(|preparation_command| match cmd.get_parameter() {137 unmodified lines392 }393 394 println!(" ");395 396+ // Run cleanup command397+ let cleanup_cmd = options398+ .cleanup_command399+ .as_ref()400+ .map(|cleanup_command| match cmd.get_parameter() {401+ Some((param, value)) => Command::new_parametrized(cleanup_command, param, value),402+ None => Command::new(cleanup_command),403+ });404+ run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?;405+406 Ok(BenchmarkResult::new(407 cmd.get_shell_command(),408 t_mean,409 t_stddev,7 unmodified linesmodified:src/hyperfine/types.rs+4 added
111 unmodified lines112 113 /// Command to run before each timing run114 pub preparation_command: Option<String>,115 116+ /// Command to run after each benchmark117+ pub cleanup_command: Option<String>,118+119 /// What color mode to use for output120 pub output_style: OutputStyleOption,121 122 /// The shell to use for executing commands.13 unmodified lines136 runs: Runs::default(),137 min_time_sec: 3.0,138 failure_action: CmdFailureAction::RaiseError,139 preparation_command: None,140+ cleanup_command: None,141 output_style: OutputStyleOption::Full,142 shell: DEFAULT_SHELL.to_string(),143 show_output: false,144 time_unit: None,57 unmodified linesmodified:src/main.rs+2 added
162 unmodified lines163 };164 165 options.preparation_command = matches.value_of("prepare").map(String::from);166 167+ options.cleanup_command = matches.value_of("cleanup").map(String::from);168+169 options.show_output = matches.is_present("show-output");170 171 options.output_style = match matches.value_of("style") {172 Some("full") => OutputStyleOption::Full,81 unmodified lines