Rename functions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Peter · committed by David Peter · parent 7c929f7 · Browse files · 6 files·+96 added−90 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+96 added−90 removed
- other:+96 added−90 removed
Provenance
- Commit
f822455- Tree
7a74f3a- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
7c929f7
6 files changed+96 added−90 removed
modified:src/benchmark/executor.rs+2 added-2 removed
1 use std::process::{ExitStatus, Stdio};2 3 use crate::command::Command;4+use crate::execute::execute_and_measure;5 use crate::options::{CmdFailureAction, CommandOutputPolicy, Options, OutputStyleOption, Shell};6 use crate::output::progress_bar::get_progress_bar;6-use crate::shell::execute_and_time;7 use crate::timer::wallclocktimer::WallClockTimer;8 use crate::util::units::Second;9 10 use super::timing_result::TimingResult;57 unmodified lines68 CommandOutputPolicy::Forward => (Stdio::inherit(), Stdio::inherit()),69 };70 71 let wallclock_timer = WallClockTimer::start();72- let result = execute_and_time(stdout, stderr, &command.get_command_line(), self.shell)?;72+ let result = execute_and_measure(stdout, stderr, &command.get_command_line(), self.shell)?;73 let mut time_real = wallclock_timer.stop();74 75 let mut time_user = result.user_time;76 let mut time_system = result.system_time;160 unmodified linesmodified:src/util/mod.rs+1 added
1 pub mod exit_code;2 pub mod min_max;3 pub mod number;4+pub mod randomized_environment_offset;5 pub mod units;added:src/util/randomized_environment_offset.rs+8 added
1+/// Returns a string with a random length. This value will be set as an environment2+/// variable to account for offset effects. See [1] for more details.3+///4+/// [1] Mytkowicz, 2009. Producing Wrong Data Without Doing Anything Obviously Wrong!.5+/// Sigplan Notices - SIGPLAN. 44. 265-276. 10.1145/1508284.1508275.6+pub fn value() -> String {7+ "X".repeat(rand::random::<usize>() % 4096usize)8+}added:src/execute.rs+84 added
1+use std::process::{ExitStatus, Stdio};2+3+use crate::options::Shell;4+use crate::util::randomized_environment_offset;5+6+use anyhow::{Context, Result};7+8+/// Used to indicate the result of running a command9+#[derive(Debug, Copy, Clone)]10+pub struct ExecuteResult {11+ /// The amount of user time the process used12+ pub user_time: f64,13+14+ /// The amount of cpu time the process used15+ pub system_time: f64,16+17+ /// The exit status of the process18+ pub status: ExitStatus,19+}20+21+/// Execute the given command and return a timing summary22+#[cfg(not(windows))]23+pub fn execute_and_measure(24+ stdout: Stdio,25+ stderr: Stdio,26+ command: &str,27+ shell: &Shell,28+) -> Result<ExecuteResult> {29+ let cpu_timer = crate::timer::unix_timer::CPUTimer::start();30+31+ let status = shell32+ .command()33+ .arg("-c")34+ .arg(command)35+ .env(36+ "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",37+ randomized_environment_offset::value(),38+ )39+ .stdin(Stdio::null())40+ .stdout(stdout)41+ .stderr(stderr)42+ .status()43+ .with_context(|| format!("Failed to run command '{}'", command))?;44+45+ let (user_time, system_time) = cpu_timer.stop();46+47+ Ok(ExecuteResult {48+ user_time,49+ system_time,50+ status,51+ })52+}53+54+/// Execute the given command and return a timing summary55+#[cfg(windows)]56+pub fn execute_and_measure(57+ stdout: Stdio,58+ stderr: Stdio,59+ command: &str,60+ shell: &Shell,61+) -> Result<ExecuteResult> {62+ let mut child = shell63+ .command()64+ .arg("/C")65+ .arg(command)66+ .env(67+ "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",68+ randomized_environment_offset::value(),69+ )70+ .stdin(Stdio::null())71+ .stdout(stdout)72+ .stderr(stderr)73+ .spawn()74+ .with_context(|| format!("Failed to run command '{}'", command))?;75+ let cpu_timer = crate::timer::windows_timer::CPUTimer::start_for_process(&child);76+ let status = child.wait()?;77+78+ let (user_time, system_time) = cpu_timer.stop();79+ Ok(ExecuteResult {80+ user_time,81+ system_time,82+ status,83+ })84+}modified:src/main.rs+1 added-1 removed
11 unmodified lines12 pub mod app;13 pub mod benchmark;14 pub mod command;15 pub mod error;16+pub mod execute;17 pub mod export;18 pub mod options;19 pub mod outlier_detection;20 pub mod output;21 pub mod parameter;21-pub mod shell;22 pub mod timer;23 pub mod util;24 25 fn run() -> Result<()> {27 unmodified linesdeleted:src/shell.rs-87 removed
1-use std::process::{ExitStatus, Stdio};2-3-use crate::options::Shell;4-5-use anyhow::{Context, Result};6-7-/// Used to indicate the result of running a command8-#[derive(Debug, Copy, Clone)]9-pub struct ExecuteResult {10- /// The amount of user time the process used11- pub user_time: f64,12-13- /// The amount of cpu time the process used14- pub system_time: f64,15-16- /// The exit status of the process17- pub status: ExitStatus,18-}19-20-fn randomized_environment_offset_value() -> String {21- "X".repeat(rand::random::<usize>() % 4096usize)22-}23-24-/// Execute the given command and return a timing summary25-#[cfg(not(windows))]26-pub fn execute_and_time(27- stdout: Stdio,28- stderr: Stdio,29- command: &str,30- shell: &Shell,31-) -> Result<ExecuteResult> {32- let cpu_timer = crate::timer::unix_timer::CPUTimer::start();33-34- let status = shell35- .command()36- .arg("-c")37- .arg(command)38- .env(39- "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",40- randomized_environment_offset_value(),41- )42- .stdin(Stdio::null())43- .stdout(stdout)44- .stderr(stderr)45- .status()46- .with_context(|| format!("Failed to run command '{}'", command))?;47-48- let (user_time, system_time) = cpu_timer.stop();49-50- Ok(ExecuteResult {51- user_time,52- system_time,53- status,54- })55-}56-57-/// Execute the given command and return a timing summary58-#[cfg(windows)]59-pub fn execute_and_time(60- stdout: Stdio,61- stderr: Stdio,62- command: &str,63- shell: &Shell,64-) -> Result<ExecuteResult> {65- let mut child = shell66- .command()67- .arg("/C")68- .arg(command)69- .env(70- "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",71- randomized_environment_offset_value(),72- )73- .stdin(Stdio::null())74- .stdout(stdout)75- .stderr(stderr)76- .spawn()77- .with_context(|| format!("Failed to run command '{}'", command))?;78- let cpu_timer = crate::timer::windows_timer::CPUTimer::start_for_process(&child);79- let status = child.wait()?;80-81- let (user_time, system_time) = cpu_timer.stop();82- Ok(ExecuteResult {83- user_time,84- system_time,85- status,86- })87-}