diff --git a/.github/workflows/benchmarks.yml b/.github/workflows/benchmarks.yml index b85f10c5c76..9a9ab11323b 100644 --- a/.github/workflows/benchmarks.yml +++ b/.github/workflows/benchmarks.yml @@ -65,7 +65,8 @@ jobs: uu_date, uu_csplit, uu_true, - uu_false + uu_false, + uu_ptx ] steps: - uses: actions/checkout@v7.0.0 diff --git a/Cargo.lock b/Cargo.lock index 2712f7b3bf2..0a9c0640127 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4018,6 +4018,7 @@ name = "uu_ptx" version = "0.9.0" dependencies = [ "clap", + "codspeed-divan-compat", "fluent", "regex", "thiserror 2.0.19", diff --git a/src/uu/ptx/Cargo.toml b/src/uu/ptx/Cargo.toml index 83225408a97..131d46382a4 100644 --- a/src/uu/ptx/Cargo.toml +++ b/src/uu/ptx/Cargo.toml @@ -26,6 +26,14 @@ uucore = { workspace = true } thiserror = { workspace = true } fluent = { workspace = true } +[dev-dependencies] +divan = { workspace = true } +uucore = { workspace = true, features = ["benchmark"] } + [[bin]] name = "ptx" path = "src/main.rs" + +[[bench]] +name = "ptx_bench" +harness = false diff --git a/src/uu/ptx/benches/ptx_bench.rs b/src/uu/ptx/benches/ptx_bench.rs new file mode 100644 index 00000000000..5bdbbe1d7d0 --- /dev/null +++ b/src/uu/ptx/benches/ptx_bench.rs @@ -0,0 +1,58 @@ +// 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. + +//! Benchmarks for `ptx`. + +use divan::{Bencher, black_box}; +use uu_ptx::uumain; +use uucore::benchmark::{run_util_function, setup_test_file, text_data}; + +fn bench_ptx(bencher: Bencher, data: &[u8], args: &[&str]) { + let file_path = setup_test_file(data); + let file_path_str = file_path.to_str().unwrap(); + + let mut full_args: Vec<&str> = args.to_vec(); + full_args.push(file_path_str); + + bencher.bench(|| { + black_box(run_util_function(uumain, &full_args)); + }); +} + +/// Build a fixed ~1 MiB input spread across num_lines lines. +fn fixed_size_data(num_lines: usize) -> Vec { + let line_len = (1024 * 1024 / num_lines).max(1); + text_data::generate_by_lines(num_lines, line_len) +} + +/// Benchmark the common case of many short lines. +#[divan::bench(args = [100_000])] +fn ptx_short_lines(bencher: Bencher, num_lines: usize) { + let data = text_data::generate_by_lines(num_lines, 80); + bench_ptx(bencher, &data, &[]); +} + +/// Benchmark a fixed ~1 MiB input spread across 100 lines. +#[divan::bench(args = [100])] +fn ptx_long_lines(bencher: Bencher, num_lines: usize) { + bench_ptx(bencher, &fixed_size_data(num_lines), &[]); +} + +/// Benchmark -r on many short lines. +#[divan::bench(args = [100_000])] +fn ptx_input_references_short_lines(bencher: Bencher, num_lines: usize) { + let data = text_data::generate_by_lines(num_lines, 80); + bench_ptx(bencher, &data, &["-r"]); +} + +/// Benchmark -r on long lines +#[divan::bench(args = [100])] +fn ptx_input_references_long_lines(bencher: Bencher, num_lines: usize) { + bench_ptx(bencher, &fixed_size_data(num_lines), &["-r"]); +} + +fn main() { + divan::main(); +}