mirror of
https://github.com/sharkdp/hyperfine.git
synced 2026-11-25 13:51:30 +00:00
Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c40de45f8 | ||
|
|
29a0ef164d | ||
|
|
bb834ca5aa | ||
|
|
87f3905678 | ||
|
|
0a61e330cf | ||
|
|
4917c557e4 | ||
|
|
a2d7094282 | ||
|
|
9bf42b337d | ||
|
|
a760e75f2f | ||
|
|
a6c301099b | ||
|
|
88a9150b4c | ||
|
|
094d0121ef | ||
|
|
c974f9b4d9 | ||
|
|
e1fa3ace8d | ||
|
|
0d6ade930a |
No files matched your search
@@ -88,6 +88,22 @@ option:
|
|||||||
hyperfine -L compiler g++,clang++ '{compiler} -O2 main.cpp'
|
hyperfine -L compiler g++,clang++ '{compiler} -O2 main.cpp'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
A common use case is comparing the same command across multiple Git branches. Use `--setup`
|
||||||
|
to switch branches once before each set of timing runs, so the branch switch is not part of
|
||||||
|
the measured command:
|
||||||
|
```sh
|
||||||
|
hyperfine \
|
||||||
|
--parameter-list branch main,performance-improvements \
|
||||||
|
--setup 'git switch {branch}' \
|
||||||
|
'python main.py'
|
||||||
|
```
|
||||||
|
|
||||||
|
If you need a unique value for each individual run of a benchmark command, hyperfine also exposes
|
||||||
|
the zero-based `$HYPERFINE_ITERATION` environment variable inside the benchmarked command itself:
|
||||||
|
```sh
|
||||||
|
hyperfine 'my-command > output-${HYPERFINE_ITERATION}.log'
|
||||||
|
```
|
||||||
|
|
||||||
### Intermediate shell
|
### Intermediate shell
|
||||||
|
|
||||||
By default, commands are executed using `sh` on Unix (resolved through `PATH`) or `cmd.exe` on Windows.
|
By default, commands are executed using `sh` on Unix (resolved through `PATH`) or `cmd.exe` on Windows.
|
||||||
|
|||||||
@@ -2,6 +2,15 @@ use std::collections::BTreeMap;
|
|||||||
|
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
||||||
|
/// Parameter value and whether it was used in the command line template
|
||||||
|
#[derive(Debug, Default, Clone, Serialize, PartialEq)]
|
||||||
|
#[serde(transparent)]
|
||||||
|
pub struct Parameter {
|
||||||
|
pub value: String,
|
||||||
|
#[serde(skip)]
|
||||||
|
pub is_unused: bool,
|
||||||
|
}
|
||||||
|
|
||||||
/// Set of values that will be exported.
|
/// Set of values that will be exported.
|
||||||
// NOTE: `serde` is used for JSON serialization, but not for CSV serialization due to the
|
// NOTE: `serde` is used for JSON serialization, but not for CSV serialization due to the
|
||||||
// `parameters` map. Update `src/hyperfine/export/csv.rs` with new fields, as appropriate.
|
// `parameters` map. Update `src/hyperfine/export/csv.rs` with new fields, as appropriate.
|
||||||
@@ -49,5 +58,5 @@ pub struct BenchmarkResult {
|
|||||||
|
|
||||||
/// Parameter values for this benchmark
|
/// Parameter values for this benchmark
|
||||||
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
|
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
|
||||||
pub parameters: BTreeMap<String, String>,
|
pub parameters: BTreeMap<String, Parameter>,
|
||||||
}
|
}
|
||||||
+10
-2
@@ -19,7 +19,7 @@ use crate::output::warnings::{OutlierWarningOptions, Warnings};
|
|||||||
use crate::parameter::ParameterNameAndValue;
|
use crate::parameter::ParameterNameAndValue;
|
||||||
use crate::quantity::{byte, const_time_from_seconds, ratio, second, FormatQuantity, Time, Zero};
|
use crate::quantity::{byte, const_time_from_seconds, ratio, second, FormatQuantity, Time, Zero};
|
||||||
use crate::util::exit_code::extract_exit_code;
|
use crate::util::exit_code::extract_exit_code;
|
||||||
use benchmark_result::BenchmarkResult;
|
use benchmark_result::{BenchmarkResult, Parameter};
|
||||||
use measurement::{Measurement, Measurements};
|
use measurement::{Measurement, Measurements};
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
@@ -474,7 +474,15 @@ impl<'a> Benchmark<'a> {
|
|||||||
.command
|
.command
|
||||||
.get_parameters()
|
.get_parameters()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(name, value)| (name.to_string(), value.to_string()))
|
.map(|(name, value)| {
|
||||||
|
(
|
||||||
|
name.to_string(),
|
||||||
|
Parameter {
|
||||||
|
value: value.to_string(),
|
||||||
|
is_unused: self.command.is_parameter_unused(name),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-2
@@ -1,8 +1,10 @@
|
|||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
|
|
||||||
use clap::{
|
use clap::{
|
||||||
builder::NonEmptyStringValueParser, crate_version, Arg, ArgAction, ArgMatches, Command,
|
builder::styling::{AnsiColor, Effects},
|
||||||
ValueHint,
|
builder::NonEmptyStringValueParser,
|
||||||
|
builder::Styles,
|
||||||
|
crate_version, Arg, ArgAction, ArgMatches, Command, ValueHint,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn get_cli_arguments<'a, I, T>(args: I) -> ArgMatches
|
pub fn get_cli_arguments<'a, I, T>(args: I) -> ArgMatches
|
||||||
@@ -14,10 +16,17 @@ where
|
|||||||
command.get_matches_from(args)
|
command.get_matches_from(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const STYLES: Styles = Styles::styled()
|
||||||
|
.header(AnsiColor::Green.on_default().effects(Effects::BOLD))
|
||||||
|
.usage(AnsiColor::Green.on_default().effects(Effects::BOLD))
|
||||||
|
.literal(AnsiColor::Cyan.on_default().effects(Effects::BOLD))
|
||||||
|
.placeholder(AnsiColor::Cyan.on_default());
|
||||||
|
|
||||||
/// Build the clap command for parsing command line arguments
|
/// Build the clap command for parsing command line arguments
|
||||||
fn build_command() -> Command {
|
fn build_command() -> Command {
|
||||||
Command::new("hyperfine")
|
Command::new("hyperfine")
|
||||||
.version(crate_version!())
|
.version(crate_version!())
|
||||||
|
.styles(STYLES)
|
||||||
.next_line_help(true)
|
.next_line_help(true)
|
||||||
.hide_possible_values(true)
|
.hide_possible_values(true)
|
||||||
.about("A command-line benchmarking tool.")
|
.about("A command-line benchmarking tool.")
|
||||||
|
|||||||
+30
-5
@@ -53,7 +53,7 @@ impl Exporter for CsvExporter {
|
|||||||
fields.push(Cow::Owned(format!("{f:.CSV_PRECISION$}").into_bytes()))
|
fields.push(Cow::Owned(format!("{f:.CSV_PRECISION$}").into_bytes()))
|
||||||
}
|
}
|
||||||
for v in res.parameters.values() {
|
for v in res.parameters.values() {
|
||||||
fields.push(Cow::Borrowed(v.as_bytes()))
|
fields.push(Cow::Borrowed(v.value.as_bytes()))
|
||||||
}
|
}
|
||||||
writer.write_record(fields)?;
|
writer.write_record(fields)?;
|
||||||
}
|
}
|
||||||
@@ -64,6 +64,7 @@ impl Exporter for CsvExporter {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_csv() {
|
fn test_csv() {
|
||||||
|
use crate::benchmark::benchmark_result::Parameter;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
let exporter = CsvExporter::default();
|
let exporter = CsvExporter::default();
|
||||||
|
|
||||||
@@ -83,8 +84,20 @@ fn test_csv() {
|
|||||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||||
parameters: {
|
parameters: {
|
||||||
let mut params = BTreeMap::new();
|
let mut params = BTreeMap::new();
|
||||||
params.insert("foo".into(), "one".into());
|
params.insert(
|
||||||
params.insert("bar".into(), "two".into());
|
"foo".into(),
|
||||||
|
Parameter {
|
||||||
|
value: "one".into(),
|
||||||
|
is_unused: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
params.insert(
|
||||||
|
"bar".into(),
|
||||||
|
Parameter {
|
||||||
|
value: "two".into(),
|
||||||
|
is_unused: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
params
|
params
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -103,8 +116,20 @@ fn test_csv() {
|
|||||||
exit_codes: vec![Some(0), Some(0), Some(0)],
|
exit_codes: vec![Some(0), Some(0), Some(0)],
|
||||||
parameters: {
|
parameters: {
|
||||||
let mut params = BTreeMap::new();
|
let mut params = BTreeMap::new();
|
||||||
params.insert("foo".into(), "one".into());
|
params.insert(
|
||||||
params.insert("bar".into(), "seven".into());
|
"foo".into(),
|
||||||
|
Parameter {
|
||||||
|
value: "one".into(),
|
||||||
|
is_unused: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
params.insert(
|
||||||
|
"bar".into(),
|
||||||
|
Parameter {
|
||||||
|
value: "seven".into(),
|
||||||
|
is_unused: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
params
|
params
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
+29
-1
@@ -24,7 +24,19 @@ impl MarkupExporter for MarkdownExporter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn command(&self, cmd: &str) -> String {
|
fn command(&self, cmd: &str) -> String {
|
||||||
format!("`{cmd}`")
|
let longest_backtick_run = cmd
|
||||||
|
.split(|character| character != '`')
|
||||||
|
.map(str::len)
|
||||||
|
.max()
|
||||||
|
.unwrap_or(0);
|
||||||
|
|
||||||
|
if longest_backtick_run == 0 {
|
||||||
|
format!("`{cmd}`")
|
||||||
|
} else {
|
||||||
|
// Use a longer delimiter and keep command backticks separate from it.
|
||||||
|
let delimiter = "`".repeat(longest_backtick_run + 1);
|
||||||
|
format!("{delimiter} {cmd} {delimiter}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,3 +56,19 @@ fn test_markdown_formatter_table_divider() {
|
|||||||
let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);
|
let divider = formatter.table_divider(&[Alignment::Left, Alignment::Right, Alignment::Left]);
|
||||||
assert_eq!(divider, "|:---|---:|:---|\n");
|
assert_eq!(divider, "|:---|---:|:---|\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_markdown_formatter_command_with_backticks() {
|
||||||
|
let formatter = MarkdownExporter::default();
|
||||||
|
|
||||||
|
for (command, expected) in [
|
||||||
|
("echo `uname`", "`` echo `uname` ``"),
|
||||||
|
("echo ``quoted``", "``` echo ``quoted`` ```"),
|
||||||
|
("`` `quoted` ``", "``` `` `quoted` `` ```"),
|
||||||
|
("`", "`` ` ``"),
|
||||||
|
("echo ```", "```` echo ``` ````"),
|
||||||
|
(" echo `uname` ", "`` echo `uname` ``"),
|
||||||
|
] {
|
||||||
|
assert_eq!(formatter.command(command), expected, "{command:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -106,7 +106,7 @@ pub fn standard_deviation<Q: UnsafeRawValue>(values: &[Q], mean: Q) -> Q {
|
|||||||
Q::unsafe_from_raw_value((sum / (values.len() as f64 - 1.0)).sqrt())
|
Q::unsafe_from_raw_value((sum / (values.len() as f64 - 1.0)).sqrt())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute modifized Z-scores for a given sample. A (unmodified) Z-score is defined by
|
/// Compute modified Z-scores for a given sample. A (unmodified) Z-score is defined by
|
||||||
/// `(x_i - x_mean)/x_stddev` whereas the modified Z-score is defined by `(x_i - x_median)/MAD`
|
/// `(x_i - x_mean)/x_stddev` whereas the modified Z-score is defined by `(x_i - x_median)/MAD`
|
||||||
/// where MAD is the median absolute deviation.
|
/// where MAD is the median absolute deviation.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -929,6 +929,27 @@ fn invalid_command_options_preserve_export_files() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn markdown_export_preserves_backticks_and_pipes_in_command_names() {
|
||||||
|
let _settings = snapshot_settings().bind_to_scope();
|
||||||
|
assert_cmd_snapshot!(hyperfine_debug()
|
||||||
|
.arg("--style=none")
|
||||||
|
.arg("--export-markdown=-")
|
||||||
|
.arg("--command-name=echo `uname` | cat")
|
||||||
|
.arg("sleep 1"), @r"
|
||||||
|
success: true
|
||||||
|
exit_code: 0
|
||||||
|
----- stdout -----
|
||||||
|
|
||||||
|
| Command | Mean [s] | Min [s] | Max [s] | Relative |
|
||||||
|
|:---|---:|---:|---:|---:|
|
||||||
|
| `` echo `uname` \| cat `` | 1.000 ± 0.000 | 1.000 | 1.000 | 1.00 |
|
||||||
|
|
||||||
|
|
||||||
|
----- stderr -----
|
||||||
|
");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unused_parameters_are_shown_in_benchmark_name() {
|
fn unused_parameters_are_shown_in_benchmark_name() {
|
||||||
hyperfine()
|
hyperfine()
|
||||||
|
|||||||
Reference in new issue
Block a user