mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-01 08:50:19 +00:00
Format source code
This commit is contained in:
1 parent
3144c4b96c
commit
a4cf1eaa29
4 files changed
+146
-99
No files matched your search
@@ -11,7 +11,9 @@ use hyperfine::outlier_detection::{modified_zscores, OUTLIER_THRESHOLD};
|
||||
use hyperfine::shell::execute_and_time;
|
||||
use hyperfine::timer::wallclocktimer::WallClockTimer;
|
||||
use hyperfine::timer::{TimerStart, TimerStop};
|
||||
use hyperfine::types::{BenchmarkResult, Command, CmdFailureAction, HyperfineOptions, OutputStyleOption};
|
||||
use hyperfine::types::{
|
||||
BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption,
|
||||
};
|
||||
use hyperfine::units::Second;
|
||||
use hyperfine::warnings::Warnings;
|
||||
|
||||
@@ -117,10 +119,13 @@ pub fn mean_shell_spawning_time(
|
||||
};
|
||||
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("Could not measure shell execution time. \
|
||||
Make sure you can run '{}'.", shell_cmd),
|
||||
))
|
||||
io::ErrorKind::Other,
|
||||
format!(
|
||||
"Could not measure shell execution time. \
|
||||
Make sure you can run '{}'.",
|
||||
shell_cmd
|
||||
),
|
||||
));
|
||||
}
|
||||
Ok((r, _)) => {
|
||||
times_real.push(r.time_real);
|
||||
@@ -189,7 +194,13 @@ pub fn run_benchmark(
|
||||
);
|
||||
|
||||
for _ in 0..options.warmup_count {
|
||||
let _ = time_shell_command(&options.shell, cmd, options.show_output, options.failure_action, None)?;
|
||||
let _ = time_shell_command(
|
||||
&options.shell,
|
||||
cmd,
|
||||
options.show_output,
|
||||
options.failure_action,
|
||||
None,
|
||||
)?;
|
||||
progress_bar.inc(1);
|
||||
}
|
||||
progress_bar.finish_and_clear();
|
||||
|
||||
@@ -33,7 +33,10 @@ impl Exporter for MarkdownExporter {
|
||||
}
|
||||
|
||||
fn table_header(unit_short_name: String) -> String {
|
||||
format!("| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n", unit=unit_short_name)
|
||||
format!(
|
||||
"| Command | Mean [{unit}] | Min…Max [{unit}] |\n|:---|---:|---:|\n",
|
||||
unit = unit_short_name
|
||||
)
|
||||
}
|
||||
|
||||
fn start_table(unit: Unit) -> Vec<u8> {
|
||||
@@ -49,12 +52,13 @@ fn add_table_row(dest: &mut Vec<u8>, entry: &BenchmarkResult, unit: Unit) {
|
||||
dest.extend(
|
||||
format!(
|
||||
"| `{command}` | {mean} ± {stddev} | {min}…{max} |\n",
|
||||
command=entry.command.replace("|", "\\|"),
|
||||
mean=mean_str,
|
||||
stddev=stddev_str,
|
||||
min=min_str,
|
||||
max=max_str,
|
||||
).as_bytes(),
|
||||
command = entry.command.replace("|", "\\|"),
|
||||
mean = mean_str,
|
||||
stddev = stddev_str,
|
||||
min = min_str,
|
||||
max = max_str,
|
||||
)
|
||||
.as_bytes(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -71,34 +75,36 @@ fn test_markdown_format_ms() {
|
||||
let mut timing_results = vec![];
|
||||
|
||||
timing_results.push(BenchmarkResult::new(
|
||||
String::from("sleep 0.1"),
|
||||
0.1057, // mean
|
||||
0.0016, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0011, // system_mean
|
||||
0.1023, // min
|
||||
0.1080, // max
|
||||
vec![0.1, 0.1, 0.1], // times
|
||||
));
|
||||
String::from("sleep 0.1"),
|
||||
0.1057, // mean
|
||||
0.0016, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0011, // system_mean
|
||||
0.1023, // min
|
||||
0.1080, // max
|
||||
vec![0.1, 0.1, 0.1], // times
|
||||
));
|
||||
|
||||
timing_results.push(BenchmarkResult::new(
|
||||
String::from("sleep 2"),
|
||||
2.0050, // mean
|
||||
0.0020, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0012, // system_mean
|
||||
2.0020, // min
|
||||
2.0080, // max
|
||||
vec![2.0, 2.0, 2.0], // times
|
||||
));
|
||||
String::from("sleep 2"),
|
||||
2.0050, // mean
|
||||
0.0020, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0012, // system_mean
|
||||
2.0020, // min
|
||||
2.0080, // max
|
||||
vec![2.0, 2.0, 2.0], // times
|
||||
));
|
||||
|
||||
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
|
||||
|
||||
let formatted_expected = format!(
|
||||
"{}\
|
||||
"{}\
|
||||
| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 |
|
||||
| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 |
|
||||
", table_header("ms".to_string()));
|
||||
",
|
||||
table_header("ms".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(formatted_expected, formatted);
|
||||
}
|
||||
@@ -112,34 +118,36 @@ fn test_markdown_format_s() {
|
||||
let mut timing_results = vec![];
|
||||
|
||||
timing_results.push(BenchmarkResult::new(
|
||||
String::from("sleep 2"),
|
||||
2.0050, // mean
|
||||
0.0020, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0012, // system_mean
|
||||
2.0020, // min
|
||||
2.0080, // max
|
||||
vec![2.0, 2.0, 2.0], // times
|
||||
));
|
||||
String::from("sleep 2"),
|
||||
2.0050, // mean
|
||||
0.0020, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0012, // system_mean
|
||||
2.0020, // min
|
||||
2.0080, // max
|
||||
vec![2.0, 2.0, 2.0], // times
|
||||
));
|
||||
|
||||
timing_results.push(BenchmarkResult::new(
|
||||
String::from("sleep 0.1"),
|
||||
0.1057, // mean
|
||||
0.0016, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0011, // system_mean
|
||||
0.1023, // min
|
||||
0.1080, // max
|
||||
vec![0.1, 0.1, 0.1], // times
|
||||
));
|
||||
String::from("sleep 0.1"),
|
||||
0.1057, // mean
|
||||
0.0016, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0011, // system_mean
|
||||
0.1023, // min
|
||||
0.1080, // max
|
||||
vec![0.1, 0.1, 0.1], // times
|
||||
));
|
||||
|
||||
let formatted = String::from_utf8(exporter.serialize(&timing_results, None).unwrap()).unwrap();
|
||||
|
||||
let formatted_expected = format!(
|
||||
"{}\
|
||||
"{}\
|
||||
| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 |
|
||||
| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 |
|
||||
", table_header("s".to_string()));
|
||||
",
|
||||
table_header("s".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(formatted_expected, formatted);
|
||||
}
|
||||
@@ -152,34 +160,41 @@ fn test_markdown_format_time_unit_s() {
|
||||
let mut timing_results = vec![];
|
||||
|
||||
timing_results.push(BenchmarkResult::new(
|
||||
String::from("sleep 0.1"),
|
||||
0.1057, // mean
|
||||
0.0016, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0011, // system_mean
|
||||
0.1023, // min
|
||||
0.1080, // max
|
||||
vec![0.1, 0.1, 0.1], // times
|
||||
));
|
||||
String::from("sleep 0.1"),
|
||||
0.1057, // mean
|
||||
0.0016, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0011, // system_mean
|
||||
0.1023, // min
|
||||
0.1080, // max
|
||||
vec![0.1, 0.1, 0.1], // times
|
||||
));
|
||||
|
||||
timing_results.push(BenchmarkResult::new(
|
||||
String::from("sleep 2"),
|
||||
2.0050, // mean
|
||||
0.0020, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0012, // system_mean
|
||||
2.0020, // min
|
||||
2.0080, // max
|
||||
vec![2.0, 2.0, 2.0], // times
|
||||
));
|
||||
String::from("sleep 2"),
|
||||
2.0050, // mean
|
||||
0.0020, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0012, // system_mean
|
||||
2.0020, // min
|
||||
2.0080, // max
|
||||
vec![2.0, 2.0, 2.0], // times
|
||||
));
|
||||
|
||||
let formatted = String::from_utf8(exporter.serialize(&timing_results, Some(Unit::Second)).unwrap()).unwrap();
|
||||
let formatted = String::from_utf8(
|
||||
exporter
|
||||
.serialize(&timing_results, Some(Unit::Second))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let formatted_expected = format!(
|
||||
"{}\
|
||||
"{}\
|
||||
| `sleep 0.1` | 0.106 ± 0.002 | 0.102…0.108 |
|
||||
| `sleep 2` | 2.005 ± 0.002 | 2.002…2.008 |
|
||||
", table_header("s".to_string()));
|
||||
",
|
||||
table_header("s".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(formatted_expected, formatted);
|
||||
}
|
||||
@@ -193,34 +208,41 @@ fn test_markdown_format_time_unit_ms() {
|
||||
let mut timing_results = vec![];
|
||||
|
||||
timing_results.push(BenchmarkResult::new(
|
||||
String::from("sleep 2"),
|
||||
2.0050, // mean
|
||||
0.0020, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0012, // system_mean
|
||||
2.0020, // min
|
||||
2.0080, // max
|
||||
vec![2.0, 2.0, 2.0], // times
|
||||
));
|
||||
String::from("sleep 2"),
|
||||
2.0050, // mean
|
||||
0.0020, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0012, // system_mean
|
||||
2.0020, // min
|
||||
2.0080, // max
|
||||
vec![2.0, 2.0, 2.0], // times
|
||||
));
|
||||
|
||||
timing_results.push(BenchmarkResult::new(
|
||||
String::from("sleep 0.1"),
|
||||
0.1057, // mean
|
||||
0.0016, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0011, // system_mean
|
||||
0.1023, // min
|
||||
0.1080, // max
|
||||
vec![0.1, 0.1, 0.1], // times
|
||||
));
|
||||
String::from("sleep 0.1"),
|
||||
0.1057, // mean
|
||||
0.0016, // std dev
|
||||
0.0009, // user_mean
|
||||
0.0011, // system_mean
|
||||
0.1023, // min
|
||||
0.1080, // max
|
||||
vec![0.1, 0.1, 0.1], // times
|
||||
));
|
||||
|
||||
let formatted = String::from_utf8(exporter.serialize(&timing_results, Some(Unit::MilliSecond)).unwrap()).unwrap();
|
||||
let formatted = String::from_utf8(
|
||||
exporter
|
||||
.serialize(&timing_results, Some(Unit::MilliSecond))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let formatted_expected = format!(
|
||||
"{}\
|
||||
"{}\
|
||||
| `sleep 2` | 2005.0 ± 2.0 | 2002.0…2008.0 |
|
||||
| `sleep 0.1` | 105.7 ± 1.6 | 102.3…108.0 |
|
||||
", table_header("ms".to_string()));
|
||||
",
|
||||
table_header("ms".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(formatted_expected, formatted);
|
||||
}
|
||||
+12
-2
@@ -19,7 +19,12 @@ pub struct ExecuteResult {
|
||||
|
||||
/// Execute the given command and return a timing summary
|
||||
#[cfg(windows)]
|
||||
pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str) -> io::Result<ExecuteResult> {
|
||||
pub fn execute_and_time(
|
||||
stdout: Stdio,
|
||||
stderr: Stdio,
|
||||
command: &str,
|
||||
shell: &str,
|
||||
) -> io::Result<ExecuteResult> {
|
||||
let mut child = run_shell_command(stdout, stderr, command, shell)?;
|
||||
let cpu_timer = get_cpu_timer(&child);
|
||||
let status = child.wait()?;
|
||||
@@ -34,7 +39,12 @@ pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str
|
||||
|
||||
/// Execute the given command and return a timing summary
|
||||
#[cfg(not(windows))]
|
||||
pub fn execute_and_time(stdout: Stdio, stderr: Stdio, command: &str, shell: &str) -> io::Result<ExecuteResult> {
|
||||
pub fn execute_and_time(
|
||||
stdout: Stdio,
|
||||
stderr: Stdio,
|
||||
command: &str,
|
||||
shell: &str,
|
||||
) -> io::Result<ExecuteResult> {
|
||||
let cpu_timer = get_cpu_timer();
|
||||
|
||||
let status = run_shell_command(stdout, stderr, command, shell)?;
|
||||
|
||||
+6
-2
@@ -58,7 +58,8 @@ pub fn error(message: &str) -> ! {
|
||||
|
||||
/// Runs the benchmark for the given commands
|
||||
fn run(commands: &Vec<Command>, options: &HyperfineOptions) -> io::Result<Vec<BenchmarkResult>> {
|
||||
let shell_spawning_time = mean_shell_spawning_time(&options.shell, &options.output_style, options.show_output)?;
|
||||
let shell_spawning_time =
|
||||
mean_shell_spawning_time(&options.shell, &options.output_style, options.show_output)?;
|
||||
|
||||
let mut timing_results = vec![];
|
||||
|
||||
@@ -191,7 +192,10 @@ fn build_hyperfine_options(matches: &ArgMatches) -> Result<HyperfineOptions, Opt
|
||||
_ => {}
|
||||
};
|
||||
|
||||
options.shell = matches.value_of("shell").unwrap_or(&options.shell).to_string();
|
||||
options.shell = matches
|
||||
.value_of("shell")
|
||||
.unwrap_or(&options.shell)
|
||||
.to_string();
|
||||
|
||||
if matches.is_present("ignore-failure") {
|
||||
options.failure_action = CmdFailureAction::Ignore;
|
||||
|
||||
Reference in new issue
Block a user