Skip to content

Commit be25344

Browse files
authored
Merge pull request #8800 from sylvestre/bench-expand
expand: add a benchmark
2 parents b48fb17 + c17f0ac commit be25344

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-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/expand/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ uucore = { workspace = true }
2424
thiserror = { 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 = "expand"
2934
path = "src/main.rs"
35+
36+
[[bench]]
37+
name = "expand_bench"
38+
harness = false
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_expand::uumain;
9+
use uucore::benchmark::{create_test_file, run_util_function};
10+
11+
/// Helper function to run expand benchmark with generated data
12+
fn bench_expand(bencher: Bencher, data: impl AsRef<[u8]>, args: &[&str]) {
13+
let temp_dir = tempfile::tempdir().unwrap();
14+
let file_path = create_test_file(data.as_ref(), temp_dir.path());
15+
let file_path_str = file_path.to_str().unwrap();
16+
17+
let mut all_args = vec![];
18+
all_args.extend_from_slice(args);
19+
all_args.push(file_path_str);
20+
21+
bencher.bench(|| {
22+
black_box(run_util_function(uumain, &all_args));
23+
});
24+
}
25+
26+
/// Benchmark expanding tabs on files with many short lines
27+
#[divan::bench(args = [100_000])]
28+
fn expand_many_lines(bencher: Bencher, num_lines: usize) {
29+
let data = (0..num_lines).fold(String::new(), |mut acc, i| {
30+
writeln!(&mut acc, "line{i}\tvalue{}\tdata{}", i * 2, i * 3).unwrap();
31+
acc
32+
});
33+
bench_expand(bencher, data, &[]);
34+
}
35+
36+
/// Benchmark expanding tabs with custom tab stops
37+
#[divan::bench(args = [50_000])]
38+
fn expand_custom_tabstops(bencher: Bencher, num_lines: usize) {
39+
let data = (0..num_lines).fold(String::new(), |mut acc, i| {
40+
writeln!(&mut acc, "a\tb\tc\td\te{i}").unwrap();
41+
acc
42+
});
43+
bench_expand(bencher, data, &["--tabs=4,8,12"]);
44+
}
45+
46+
fn main() {
47+
divan::main();
48+
}

0 commit comments

Comments
 (0)