Skip to content

Commit 7120f85

Browse files
committed
fold: add a benchmark
1 parent b4f8eac commit 7120f85

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/fold/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ clap = { workspace = true }
2222
uucore = { workspace = true }
2323
fluent = { workspace = true }
2424

25+
[dev-dependencies]
26+
divan = { workspace = true }
27+
tempfile = { workspace = true }
28+
uucore = { workspace = true, features = ["benchmark"] }
29+
2530
[[bin]]
2631
name = "fold"
2732
path = "src/main.rs"
33+
34+
[[bench]]
35+
name = "fold_bench"
36+
harness = false

src/uu/fold/benches/fold_bench.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// This file is part of the uutils coreutils package.
2+
//
3+
// For the full copyright and license information, please view the LICENSE
4+
// file that was distributed with this source code.
5+
6+
use divan::{Bencher, black_box};
7+
use std::fmt::Write;
8+
use uu_fold::uumain;
9+
use uucore::benchmark::{create_test_file, run_util_function};
10+
11+
/// Benchmark folding many short lines
12+
#[divan::bench(args = [100_000])]
13+
fn fold_many_lines(bencher: Bencher, num_lines: usize) {
14+
let temp_dir = tempfile::tempdir().unwrap();
15+
// Create long lines that need folding
16+
let data = (0..num_lines)
17+
.fold(String::new(), |mut acc, i| {
18+
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();
19+
acc
20+
});
21+
let file_path = create_test_file(data.as_bytes(), temp_dir.path());
22+
let file_path_str = file_path.to_str().unwrap();
23+
24+
bencher.bench(|| {
25+
black_box(run_util_function(uumain, &[file_path_str]));
26+
});
27+
}
28+
29+
/// Benchmark folding with custom width
30+
#[divan::bench(args = [50_000])]
31+
fn fold_custom_width(bencher: Bencher, num_lines: usize) {
32+
let temp_dir = tempfile::tempdir().unwrap();
33+
let data = (0..num_lines).fold(String::new(), |mut acc, i| {
34+
writeln!(
35+
&mut acc,
36+
"Line {i} with enough text to exceed width 40 characters and require folding"
37+
)
38+
.unwrap();
39+
acc
40+
});
41+
let file_path = create_test_file(data.as_bytes(), temp_dir.path());
42+
let file_path_str = file_path.to_str().unwrap();
43+
44+
bencher.bench(|| {
45+
black_box(run_util_function(uumain, &["-w", "40", file_path_str]));
46+
});
47+
}
48+
49+
fn main() {
50+
divan::main();
51+
}

0 commit comments

Comments
 (0)