diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index 17c37e41a93..9c8c180bf45 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -48,6 +48,7 @@ jobs: uu_shuf, uu_sort, uu_split, + uu_timeout, uu_tsort, uu_unexpand, uu_uniq, diff --git a/Cargo.lock b/Cargo.lock index 4cff737eed3..1bfb8a47a37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4287,6 +4287,7 @@ name = "uu_timeout" version = "0.8.0" dependencies = [ "clap", + "codspeed-divan-compat", "fluent", "libc", "nix", diff --git a/src/uu/timeout/Cargo.toml b/src/uu/timeout/Cargo.toml index 90a0b378c4a..ecc10470195 100644 --- a/src/uu/timeout/Cargo.toml +++ b/src/uu/timeout/Cargo.toml @@ -30,3 +30,11 @@ nix = { workspace = true, features = ["signal"] } [[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..511955f6f0b --- /dev/null +++ b/src/uu/timeout/benches/timeout_bench.rs @@ -0,0 +1,34 @@ +// 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; + +/// Benchmark the fast path where the command exits immediately. +#[cfg(unix)] +#[divan::bench] +fn timeout_quick_exit(bencher: Bencher) { + bencher.bench(|| { + black_box(run_util_function(uumain, &["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) { + bencher.bench(|| { + black_box(run_util_function(uumain, &["0.02", "sleep", "0.2"])); + }); +} + +fn main() { + #[cfg(unix)] + divan::main(); +}