Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export exit codes to JSON output #371

Merged
merged 3 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# unreleased

- Add command exit code to output if it fails, see #342 (@KaindlJulian)
- Export command exit code to JSON output, see #371 (@JordiChauzi)

## Features

Expand Down
32 changes: 27 additions & 5 deletions src/hyperfine/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cmp;
use std::io;
use std::process::Stdio;
use std::process::{ExitStatus, Stdio};

use colored::*;
use statistical::{mean, median, standard_deviation};
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn time_shell_command(
show_output: bool,
failure_action: CmdFailureAction,
shell_spawning_time: Option<TimingResult>,
) -> io::Result<(TimingResult, bool)> {
) -> io::Result<(TimingResult, ExitStatus)> {
let (stdout, stderr) = if show_output {
(Stdio::inherit(), Stdio::inherit())
} else {
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn time_shell_command(
time_user,
time_system,
},
result.status.success(),
result.status,
))
}

Expand Down Expand Up @@ -201,6 +201,22 @@ fn run_cleanup_command(
run_intermediate_command(shell, command, show_output, error_output)
}

#[cfg(unix)]
fn extract_exit_code(status: ExitStatus) -> Option<i32> {
use std::os::unix::process::ExitStatusExt;

/* From the ExitStatus::code documentation:
"On Unix, this will return None if the process was terminated by a signal."
In that case, ExitStatusExt::signal should never return None.
*/
status.code().or_else(|| status.signal())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should either return the exit code (if it's there) OR the signal n + 128. This is a standard procedure, e.g. in Bash: https://tldp.org/LDP/abs/html/exitcodes.html

Returning just the signal number (e.g. 9 for KILL) could be confused with the actual exit code 9.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right! I could not easily find a constant in libc or in the std library, so I hardcoded 128 with a comment.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

}

#[cfg(not(unix))]
fn extract_exit_code(status: ExitStatus) -> Option<i32> {
status.code()
}

/// Run the benchmark for a single shell command
pub fn run_benchmark(
num: usize,
Expand Down Expand Up @@ -228,6 +244,7 @@ pub fn run_benchmark(
let mut times_real: Vec<Second> = vec![];
let mut times_user: Vec<Second> = vec![];
let mut times_system: Vec<Second> = vec![];
let mut exit_codes: Vec<Option<i32>> = vec![];
let mut all_succeeded = true;

// Run init command
Expand Down Expand Up @@ -280,13 +297,14 @@ pub fn run_benchmark(
let prepare_res = run_preparation_command(&options.shell, &prepare_cmd, options.show_output)?;

// Initial timing run
let (res, success) = time_shell_command(
let (res, status) = time_shell_command(
&options.shell,
cmd,
options.show_output,
options.failure_action,
Some(shell_spawning_time),
)?;
let success = status.success();

// Determine number of benchmark runs
let runs_in_min_time = (options.min_time_sec
Expand All @@ -310,6 +328,7 @@ pub fn run_benchmark(
times_real.push(res.time_real);
times_user.push(res.time_user);
times_system.push(res.time_system);
exit_codes.push(extract_exit_code(status));

all_succeeded = all_succeeded && success;

Expand All @@ -328,17 +347,19 @@ pub fn run_benchmark(

progress_bar.as_ref().map(|bar| bar.set_message(&msg));

let (res, success) = time_shell_command(
let (res, status) = time_shell_command(
&options.shell,
cmd,
options.show_output,
options.failure_action,
Some(shell_spawning_time),
)?;
let success = status.success();

times_real.push(res.time_real);
times_user.push(res.time_user);
times_system.push(res.time_system);
exit_codes.push(extract_exit_code(status));

all_succeeded = all_succeeded && success;

Expand Down Expand Up @@ -438,6 +459,7 @@ pub fn run_benchmark(
t_min,
t_max,
times_real,
exit_codes,
cmd.get_parameters()
.iter()
.map(|(name, value)| ((*name).to_string(), value.to_string()))
Expand Down
8 changes: 6 additions & 2 deletions src/hyperfine/export/asciidoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ fn test_asciidoc_table_row() {
0.10745223440000001,
0.10697327940000001,
],
BTreeMap::new(), // param
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // param
);

let expms = format!(
Expand Down Expand Up @@ -151,7 +152,8 @@ fn test_asciidoc_table_row_command_escape() {
0.10745223440000001,
0.10697327940000001,
],
BTreeMap::new(), // param
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // param
);
let exps = format!(
"| `sleep 1\\|`\n\
Expand Down Expand Up @@ -185,6 +187,7 @@ fn test_asciidoc() {
5.0,
6.0,
vec![7.0, 8.0, 9.0],
vec![Some(0), Some(0), Some(0)],
{
let mut params = BTreeMap::new();
params.insert("foo".into(), "1".into());
Expand All @@ -202,6 +205,7 @@ fn test_asciidoc() {
15.0,
16.0,
vec![17.0, 18.0, 19.0],
vec![Some(0), Some(0), Some(0)],
{
let mut params = BTreeMap::new();
params.insert("foo".into(), "1".into());
Expand Down
4 changes: 3 additions & 1 deletion src/hyperfine/export/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Exporter for CsvExporter {

{
let mut headers: Vec<Cow<[u8]>> = [
// The list of times cannot be exported to the CSV file - omit it.
// The list of times and exit codes cannot be exported to the CSV file - omit them.
"command", "mean", "stddev", "median", "user", "system", "min", "max",
]
.iter()
Expand Down Expand Up @@ -68,6 +68,7 @@ fn test_csv() {
5.0,
6.0,
vec![7.0, 8.0, 9.0],
vec![Some(0), Some(0), Some(0)],
{
let mut params = BTreeMap::new();
params.insert("foo".into(), "one".into());
Expand All @@ -85,6 +86,7 @@ fn test_csv() {
15.0,
16.5,
vec![17.0, 18.0, 19.0],
vec![Some(0), Some(0), Some(0)],
{
let mut params = BTreeMap::new();
params.insert("foo".into(), "one".into());
Expand Down
152 changes: 80 additions & 72 deletions src/hyperfine/export/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,30 @@ fn test_markdown_format_ms() {

timing_results.push(BenchmarkResult::new(
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.1057, // median
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
BTreeMap::new(), // parameter
0.1057, // mean
0.0016, // std dev
0.1057, // median
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // parameter
));

timing_results.push(BenchmarkResult::new(
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
2.0050, // median
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
BTreeMap::new(), // parameter
2.0050, // mean
0.0020, // std dev
2.0050, // median
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // parameter
));

let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
Expand All @@ -142,28 +144,30 @@ fn test_markdown_format_s() {

timing_results.push(BenchmarkResult::new(
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
2.0050, // median
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
BTreeMap::new(), // parameter
2.0050, // mean
0.0020, // std dev
2.0050, // median
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // parameter
));

timing_results.push(BenchmarkResult::new(
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.1057, // median
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
BTreeMap::new(), // parameter
0.1057, // mean
0.0016, // std dev
0.1057, // median
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // parameter
));

let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
Expand All @@ -189,28 +193,30 @@ fn test_markdown_format_time_unit_s() {

timing_results.push(BenchmarkResult::new(
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.1057, // median
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
BTreeMap::new(), // parameter
0.1057, // mean
0.0016, // std dev
0.1057, // median
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // parameter
));

timing_results.push(BenchmarkResult::new(
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
2.0050, // median
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
BTreeMap::new(), // parameter
2.0050, // mean
0.0020, // std dev
2.0050, // median
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // parameter
));

let formatted = String::from_utf8(
Expand Down Expand Up @@ -242,28 +248,30 @@ fn test_markdown_format_time_unit_ms() {

timing_results.push(BenchmarkResult::new(
String::from("sleep 2"),
2.0050, // mean
0.0020, // std dev
2.0050, // median
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
BTreeMap::new(), // parameter
2.0050, // mean
0.0020, // std dev
2.0050, // median
0.0009, // user_mean
0.0012, // system_mean
2.0020, // min
2.0080, // max
vec![2.0, 2.0, 2.0], // times
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // parameter
));

timing_results.push(BenchmarkResult::new(
String::from("sleep 0.1"),
0.1057, // mean
0.0016, // std dev
0.1057, // median
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
BTreeMap::new(), // parameter
0.1057, // mean
0.0016, // std dev
0.1057, // median
0.0009, // user_mean
0.0011, // system_mean
0.1023, // min
0.1080, // max
vec![0.1, 0.1, 0.1], // times
vec![Some(0), Some(0), Some(0)], // exit codes
BTreeMap::new(), // parameter
));

let formatted = String::from_utf8(
Expand Down
1 change: 1 addition & 0 deletions src/hyperfine/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ fn create_result(name: &str, mean: Scalar) -> BenchmarkResult {
min: mean,
max: mean,
times: None,
exit_codes: Vec::new(),
parameters: BTreeMap::new(),
}
}
Expand Down
Loading