Skip to content

Commit 17ddf29

Browse files
authored
unexpand: add a benchmark (#8813)
2 parents f860610 + 8d12700 commit 17ddf29

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-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/unexpand/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ unicode-width = { workspace = true }
2424
uucore = { workspace = true }
2525
fluent = { workspace = true }
2626

27+
[dev-dependencies]
28+
divan = { workspace = true }
29+
tempfile = { workspace = true }
30+
uucore = { workspace = true, features = ["benchmark"] }
31+
2732
[[bin]]
2833
name = "unexpand"
2934
path = "src/main.rs"
35+
36+
[[bench]]
37+
name = "unexpand_bench"
38+
harness = false
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 uu_unexpand::uumain;
8+
use uucore::benchmark::{create_test_file, run_util_function};
9+
10+
/// Generate text data with leading spaces (typical unexpand use case)
11+
fn generate_indented_text(num_lines: usize) -> Vec<u8> {
12+
let mut data = Vec::new();
13+
for i in 0..num_lines {
14+
// Add varying amounts of leading spaces (4, 8, 12, etc.)
15+
let indent = (i % 4 + 1) * 4;
16+
data.extend(vec![b' '; indent]);
17+
data.extend_from_slice(b"This is a line of text with leading spaces\n");
18+
}
19+
data
20+
}
21+
22+
/// Benchmark unexpanding many lines with leading spaces (most common use case)
23+
#[divan::bench(args = [100_000])]
24+
fn unexpand_many_lines(bencher: Bencher, num_lines: usize) {
25+
let temp_dir = tempfile::tempdir().unwrap();
26+
let data = generate_indented_text(num_lines);
27+
let file_path = create_test_file(&data, temp_dir.path());
28+
let file_path_str = file_path.to_str().unwrap();
29+
30+
bencher.bench(|| {
31+
black_box(run_util_function(uumain, &[file_path_str]));
32+
});
33+
}
34+
35+
/// Benchmark large file with spaces (tests performance on large files)
36+
#[divan::bench(args = [10])]
37+
fn unexpand_large_file(bencher: Bencher, size_mb: usize) {
38+
let temp_dir = tempfile::tempdir().unwrap();
39+
40+
// Generate approximately size_mb worth of indented lines
41+
let line_size = 50; // approximate bytes per line
42+
let num_lines = (size_mb * 1024 * 1024) / line_size;
43+
let data = generate_indented_text(num_lines);
44+
let file_path = create_test_file(&data, temp_dir.path());
45+
let file_path_str = file_path.to_str().unwrap();
46+
47+
bencher.bench(|| {
48+
black_box(run_util_function(uumain, &[file_path_str]));
49+
});
50+
}
51+
52+
fn main() {
53+
divan::main();
54+
}

0 commit comments

Comments
 (0)