diff --git a/Cargo.lock b/Cargo.lock index c40ccccf9..101ae8847 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3476,7 +3476,9 @@ name = "uu_fold" version = "0.2.2" dependencies = [ "clap", + "codspeed-divan-compat", "fluent", + "tempfile", "uucore", ] diff --git a/src/uu/fold/Cargo.toml b/src/uu/fold/Cargo.toml index 8e26936e1..644d78b41 100644 --- a/src/uu/fold/Cargo.toml +++ b/src/uu/fold/Cargo.toml @@ -22,6 +22,15 @@ clap = { workspace = true } uucore = { workspace = true } fluent = { workspace = true } +[dev-dependencies] +divan = { workspace = true } +tempfile = { workspace = true } +uucore = { workspace = true, features = ["benchmark"] } + [[bin]] name = "fold" path = "src/main.rs" + +[[bench]] +name = "fold_bench" +harness = false diff --git a/src/uu/fold/benches/fold_bench.rs b/src/uu/fold/benches/fold_bench.rs new file mode 100644 index 000000000..abd69525f --- /dev/null +++ b/src/uu/fold/benches/fold_bench.rs @@ -0,0 +1,51 @@ +// 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. + +use divan::{Bencher, black_box}; +use std::fmt::Write; +use uu_fold::uumain; +use uucore::benchmark::{create_test_file, run_util_function}; + +/// Benchmark folding many short lines +#[divan::bench(args = [100_000])] +fn fold_many_lines(bencher: Bencher, num_lines: usize) { + let temp_dir = tempfile::tempdir().unwrap(); + // Create long lines that need folding + let data = (0..num_lines) + .fold(String::new(), |mut acc, i| { + writeln!(&mut acc, "This is a very long line number {i} that definitely needs to be folded at the default width of 80 columns").unwrap(); + acc + }); + let file_path = create_test_file(data.as_bytes(), temp_dir.path()); + let file_path_str = file_path.to_str().unwrap(); + + bencher.bench(|| { + black_box(run_util_function(uumain, &[file_path_str])); + }); +} + +/// Benchmark folding with custom width +#[divan::bench(args = [50_000])] +fn fold_custom_width(bencher: Bencher, num_lines: usize) { + let temp_dir = tempfile::tempdir().unwrap(); + let data = (0..num_lines).fold(String::new(), |mut acc, i| { + writeln!( + &mut acc, + "Line {i} with enough text to exceed width 40 characters and require folding" + ) + .unwrap(); + acc + }); + let file_path = create_test_file(data.as_bytes(), temp_dir.path()); + let file_path_str = file_path.to_str().unwrap(); + + bencher.bench(|| { + black_box(run_util_function(uumain, &["-w", "40", file_path_str])); + }); +} + +fn main() { + divan::main(); +}