diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index 205f6c1a24e..ada64746656 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -40,7 +40,8 @@ jobs: - { package: uu_shuf } - { package: uu_sort } - { package: uu_split } - - { package: uu_tsort } + - { package: uu_timeout } + - { package: uu_tsort } - { package: uu_unexpand } - { package: uu_uniq } - { package: uu_wc } diff --git a/Cargo.lock b/Cargo.lock index 277321cd990..730abd735aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3974,6 +3974,7 @@ name = "uu_timeout" version = "0.5.0" dependencies = [ "clap", + "codspeed-divan-compat", "fluent", "libc", "nix", diff --git a/src/uu/timeout/Cargo.toml b/src/uu/timeout/Cargo.toml index c6b795628f7..cb87a6b70e9 100644 --- a/src/uu/timeout/Cargo.toml +++ b/src/uu/timeout/Cargo.toml @@ -27,3 +27,11 @@ fluent = { workspace = true } [[bin]] name = "timeout" path = "src/main.rs" + +[dev-dependencies] +divan = { workspace = true } +uucore = { workspace = true, features = ["benchmark"] } + +[[bench]] +name = "timeout_bench" +harness = false diff --git a/src/uu/timeout/benches/timeout_bench.rs b/src/uu/timeout/benches/timeout_bench.rs new file mode 100644 index 00000000000..c807f893206 --- /dev/null +++ b/src/uu/timeout/benches/timeout_bench.rs @@ -0,0 +1,37 @@ +// This file is part of the uutils coreutils package. +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +#[cfg(unix)] +use divan::{Bencher, black_box}; +#[cfg(unix)] +use uu_timeout::uumain; +#[cfg(unix)] +use uucore::benchmark::run_util_function; + +#[cfg(unix)] +fn bench_timeout(bencher: Bencher, args: &[&str]) { + bencher.bench(|| { + black_box(run_util_function(uumain, args)); + }); +} + +/// Benchmark the fast path where the command exits immediately. +#[cfg(unix)] +#[divan::bench] +fn timeout_quick_exit(bencher: Bencher) { + bench_timeout(bencher, &["0.02", "true"]); +} + +/// Benchmark a command that runs longer than the threshold and receives the default signal. +#[cfg(unix)] +#[divan::bench] +fn timeout_enforced(bencher: Bencher) { + bench_timeout(bencher, &["0.02", "sleep", "0.2"]); +} + +fn main() { + #[cfg(unix)] + divan::main(); +}