diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index 0e5607e4a..1bdb64a04 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -48,6 +48,7 @@ jobs: uu_shuf, uu_sort, uu_split, + uu_tee, uu_timeout, uu_tsort, uu_unexpand, diff --git a/Cargo.lock b/Cargo.lock index c8ddc5f31..4a89d7d02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4256,8 +4256,10 @@ name = "uu_tee" version = "0.8.0" dependencies = [ "clap", + "codspeed-divan-compat", "fluent", "rustix", + "tempfile", "uucore", ] diff --git a/src/uu/tee/Cargo.toml b/src/uu/tee/Cargo.toml index 703b43089..e52fe7bc0 100644 --- a/src/uu/tee/Cargo.toml +++ b/src/uu/tee/Cargo.toml @@ -24,9 +24,17 @@ clap = { workspace = true } uucore = { workspace = true, features = ["libc", "parser", "signals"] } fluent = { workspace = true } -[target.'cfg(unix)'.dev-dependencies] -rustix = { workspace = true } +[dev-dependencies] +divan = { workspace = true } +rustix = { workspace = true, features = ["stdio", "fs"] } +tempfile = { workspace = true } +uucore = { workspace = true, features = ["benchmark"] } [[bin]] name = "tee" path = "src/main.rs" + + +[[bench]] +name = "tee_bench" +harness = false diff --git a/src/uu/tee/benches/tee_bench.rs b/src/uu/tee/benches/tee_bench.rs new file mode 100644 index 000000000..d6352ded3 --- /dev/null +++ b/src/uu/tee/benches/tee_bench.rs @@ -0,0 +1,27 @@ +#[cfg(unix)] +use divan::{Bencher, black_box}; +#[cfg(unix)] +use uu_tee::uumain; +#[cfg(unix)] +use uucore::benchmark::{run_util_function, setup_test_file}; + +#[cfg(unix)] +#[divan::bench(args = [10_000_000])] +fn tee_stdin_file(bencher: Bencher, size_bytes: usize) { + let data = vec![b'a'; size_bytes]; + let file_path = setup_test_file(&data); + let file = std::fs::File::open(file_path).unwrap(); + let stdin_bak = rustix::io::dup(rustix::stdio::stdin()).unwrap(); + + bencher.bench_local(|| { + use rustix::stdio::dup2_stdin; + rustix::fs::seek(&file, rustix::fs::SeekFrom::Start(0)).unwrap(); + dup2_stdin(&file).unwrap(); // should be 1 thread + black_box(run_util_function(uumain, &[])); + dup2_stdin(&stdin_bak).unwrap(); // should be 1 thread + }); +} + +fn main() { + divan::main(); +}