Skip to content
Myr

Export exit codes to JSON output

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

authored by Jordi Chauzi · committed by David Peter · parent 9e57bf6 · Browse files · 7 files+53 added−6 removed
This commit

State

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

Diff stats

7 files+53 added−6 removed

  • other:+53 added−6 removed

7 files+53 added−6 removed

modified:src/hyperfine/export/asciidoc.rs+4 added
97 unmodified lines
98 0.1003342584,99 0.10745223440000001,100 0.10697327940000001,101 ],102 vec![0, 0, 0], // exit codes103 BTreeMap::new(), // param104 );105106 let expms = format!(
44 unmodified lines
151 0.1003342584,152 0.10745223440000001,153 0.10697327940000001,154 ],155 vec![0, 0, 0], // exit codes156 BTreeMap::new(), // param157 );158 let exps = format!(159 "| `sleep 1\\|`\n\
26 unmodified lines
186 4.0,187 5.0,188 6.0,189 vec![7.0, 8.0, 9.0],190 vec![0, 0, 0],191 {192 let mut params = BTreeMap::new();193 params.insert("foo".into(), "1".into());194 params.insert("bar".into(), "2".into());
9 unmodified lines
204 14.0,205 15.0,206 16.0,207 vec![17.0, 18.0, 19.0],208 vec![0, 0, 0],209 {210 let mut params = BTreeMap::new();211 params.insert("foo".into(), "1".into());212 params.insert("bar".into(), "7".into());
25 unmodified lines
modified:src/hyperfine/export/csv.rs+3 added-1 removed
15 unmodified lines
16 let mut writer = WriterBuilder::new().from_writer(vec![]);1718 {19 let mut headers: Vec<Cow<[u8]>> = [20 // The list of times cannot be exported to the CSV file - omit it.20 // The list of times and exit codes cannot be exported to the CSV file - omit them.21 "command", "mean", "stddev", "median", "user", "system", "min", "max",22 ]23 .iter()24 .map(|x| Cow::Borrowed(x.as_bytes()))
42 unmodified lines
67 4.0,68 5.0,69 6.0,70 vec![7.0, 8.0, 9.0],71 vec![0, 0, 0],72 {73 let mut params = BTreeMap::new();74 params.insert("foo".into(), "one".into());75 params.insert("bar".into(), "two".into());
9 unmodified lines
85 14.0,86 15.0,87 16.5,88 vec![17.0, 18.0, 19.0],89 vec![0, 0, 0],90 {91 let mut params = BTreeMap::new();92 params.insert("foo".into(), "one".into());93 params.insert("bar".into(), "seven".into());
15 unmodified lines
modified:src/hyperfine/export/markdown.rs+8 added
100 unmodified lines
101 0.0011, // system_mean102 0.1023, // min103 0.1080, // max104 vec![0.1, 0.1, 0.1], // times105 vec![0, 0, 0], // exit codes106 BTreeMap::new(), // parameter107 ));108109 timing_results.push(BenchmarkResult::new(
5 unmodified lines
115 0.0012, // system_mean116 2.0020, // min117 2.0080, // max118 vec![2.0, 2.0, 2.0], // times119 vec![0, 0, 0], // exit codes120 BTreeMap::new(), // parameter121 ));122123 let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
27 unmodified lines
151 0.0012, // system_mean152 2.0020, // min153 2.0080, // max154 vec![2.0, 2.0, 2.0], // times155 vec![0, 0, 0], // exit codes156 BTreeMap::new(), // parameter157 ));158159 timing_results.push(BenchmarkResult::new(
5 unmodified lines
165 0.0011, // system_mean166 0.1023, // min167 0.1080, // max168 vec![0.1, 0.1, 0.1], // times169 vec![0, 0, 0], // exit codes170 BTreeMap::new(), // parameter171 ));172173 let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
26 unmodified lines
200 0.0011, // system_mean201 0.1023, // min202 0.1080, // max203 vec![0.1, 0.1, 0.1], // times204 vec![0, 0, 0], // exit codes205 BTreeMap::new(), // parameter206 ));207208 timing_results.push(BenchmarkResult::new(
5 unmodified lines
214 0.0012, // system_mean215 2.0020, // min216 2.0080, // max217 vec![2.0, 2.0, 2.0], // times218 vec![0, 0, 0], // exit codes219 BTreeMap::new(), // parameter220 ));221222 let formatted = String::from_utf8(
32 unmodified lines
255 0.0012, // system_mean256 2.0020, // min257 2.0080, // max258 vec![2.0, 2.0, 2.0], // times259 vec![0, 0, 0], // exit codes260 BTreeMap::new(), // parameter261 ));262263 timing_results.push(BenchmarkResult::new(
5 unmodified lines
269 0.0011, // system_mean270 0.1023, // min271 0.1080, // max272 vec![0.1, 0.1, 0.1], // times273 vec![0, 0, 0], // exit codes274 BTreeMap::new(), // parameter275 ));276277 let formatted = String::from_utf8(
16 unmodified lines
modified:src/hyperfine/benchmark.rs+31 added-5 removed
1use std::cmp;2use std::io;3use std::process::Stdio;3use std::process::{ExitStatus, Stdio};45use colored::*;6use statistical::{mean, median, standard_deviation};7
37 unmodified lines
45 command: &Command<'_>,46 show_output: bool,47 failure_action: CmdFailureAction,48 shell_spawning_time: Option<TimingResult>,49) -> io::Result<(TimingResult, bool)> {49) -> io::Result<(TimingResult, ExitStatus)> {50 let (stdout, stderr) = if show_output {51 (Stdio::inherit(), Stdio::inherit())52 } else {53 (Stdio::null(), Stdio::null())
33 unmodified lines
87 time_real,88 time_user,89 time_system,90 },91 result.status.success(),91 result.status,92 ))93}9495/// Measure the average shell spawning time
104 unmodified lines
200201 run_intermediate_command(shell, command, show_output, error_output)202}203204#[cfg(unix)]205fn extract_exit_code(status: ExitStatus) -> i32 {206 use std::os::unix::process::ExitStatusExt;207208 /* From the ExitStatus::code documentation:209 "On Unix, this will return None if the process was terminated by a signal."210 In that case, ExitStatusExt::signal should never return None.211 */212 status.code().unwrap_or_else(|| status.signal().unwrap())213}214215#[cfg(not(unix))]216fn extract_exit_code(status: ExitStatus) -> i32 {217 /* From the ExitStatus::code documentation:218 "On Unix, this will return None if the process was terminated by a signal."219 On the other configurations, ExitStatus::code should never return None.220 */221 status.code().unwrap()222}223224/// Run the benchmark for a single shell command225pub fn run_benchmark(226 num: usize,227 cmd: &Command<'_>,
19 unmodified lines
247248 let mut times_real: Vec<Second> = vec![];249 let mut times_user: Vec<Second> = vec![];250 let mut times_system: Vec<Second> = vec![];251 let mut exit_codes: Vec<i32> = vec![];252 let mut all_succeeded = true;253254 // Run init command255 let prepare_cmd = options.preparation_command.as_ref().map(|values| {
44 unmodified lines
300301 let prepare_res = run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;302303 // Initial timing run283 let (res, success) = time_shell_command(304 let (res, status) = time_shell_command(305 &options.shell,306 cmd,307 options.show_output,308 options.failure_action,309 Some(shell_spawning_time),310 )?;311 let success = status.success();312313 // Determine number of benchmark runs314 let runs_in_min_time = (options.min_time_sec315 / (res.time_real + prepare_res.time_real + shell_spawning_time.time_real))
15 unmodified lines
331 // Save the first result332 times_real.push(res.time_real);333 times_user.push(res.time_user);334 times_system.push(res.time_system);335 exit_codes.push(extract_exit_code(status));336337 all_succeeded = all_succeeded && success;338339 // Re-configure the progress bar
10 unmodified lines
350 };351352 progress_bar.as_ref().map(|bar| bar.set_message(msg.to_owned()));353331 let (res, success) = time_shell_command(354 let (res, status) = time_shell_command(355 &options.shell,356 cmd,357 options.show_output,358 options.failure_action,359 Some(shell_spawning_time),360 )?;361 let success = status.success();362363 times_real.push(res.time_real);364 times_user.push(res.time_user);365 times_system.push(res.time_system);366 exit_codes.push(extract_exit_code(status));367368 all_succeeded = all_succeeded && success;369370 progress_bar.as_ref().map(|bar| bar.inc(1));
91 unmodified lines
462 system_mean,463 t_min,464 t_max,465 times_real,466 exit_codes,467 cmd.get_parameters()468 .iter()469 .map(|(name, value)| ((*name).to_string(), value.to_string()))470 .collect(),
2 unmodified lines
modified:src/hyperfine/internal.rs+1 added
155 unmodified lines
156 system: 0.0,157 min: mean,158 max: mean,159 times: None,160 exit_codes: Vec::new(),161 parameters: BTreeMap::new(),162 }163}164
86 unmodified lines
modified:src/hyperfine/types.rs+5 added
266 unmodified lines
267 /// All run time measurements268 #[serde(skip_serializing_if = "Option::is_none")]269 pub times: Option<Vec<Second>>,270271 /// All run exit codes272 pub exit_codes: Vec<i32>,273274 /// Any parameter values used275 #[serde(skip_serializing_if = "BTreeMap::is_empty")]276 pub parameters: BTreeMap<String, String>,277}
9 unmodified lines
287 system: Second,288 min: Second,289 max: Second,290 times: Vec<Second>,291 exit_codes: Vec<i32>,292 parameters: BTreeMap<String, String>,293 ) -> Self {294 BenchmarkResult {295 command,
4 unmodified lines
300 system,301 min,302 max,303 times: Some(times),304 exit_codes,305 parameters,306 }307 }308}
modified:CHANGELOG.md+1 added
1# unreleased23- Add command exit code to output if it fails, see #342 (@KaindlJulian)4- Export command exit code to JSON output, see #371 (@JordiChauzi)56## Features78## Changes
257 unmodified lines