Skip to content

Commit 03f160c

Browse files
authored
nl: add a benchmark (#8808)
1 parent 1331ff1 commit 03f160c

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

Cargo.lock

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

src/uu/nl/Cargo.toml

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

26+
[dev-dependencies]
27+
divan = { workspace = true }
28+
tempfile = { workspace = true }
29+
uucore = { workspace = true, features = ["benchmark"] }
30+
2631
[[bin]]
2732
name = "nl"
2833
path = "src/main.rs"
34+
35+
[[bench]]
36+
name = "nl_bench"
37+
harness = false

src/uu/nl/benches/nl_bench.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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_nl::uumain;
8+
use uucore::benchmark::{create_test_file, run_util_function, text_data};
9+
10+
/// Benchmark numbering many lines (default mode - most common use case)
11+
#[divan::bench(args = [100_000])]
12+
fn nl_many_lines(bencher: Bencher, num_lines: usize) {
13+
let temp_dir = tempfile::tempdir().unwrap();
14+
let data = text_data::generate_by_lines(num_lines, 80);
15+
let file_path = create_test_file(&data, temp_dir.path());
16+
let file_path_str = file_path.to_str().unwrap();
17+
18+
bencher.bench(|| {
19+
black_box(run_util_function(uumain, &[file_path_str]));
20+
});
21+
}
22+
23+
/// Benchmark large file with -ba option (number all lines - most common argument)
24+
#[divan::bench(args = [10])]
25+
fn nl_large_file(bencher: Bencher, size_mb: usize) {
26+
let temp_dir = tempfile::tempdir().unwrap();
27+
let data = text_data::generate_by_size(size_mb, 80);
28+
let file_path = create_test_file(&data, temp_dir.path());
29+
let file_path_str = file_path.to_str().unwrap();
30+
31+
bencher.bench(|| {
32+
black_box(run_util_function(uumain, &["-ba", file_path_str]));
33+
});
34+
}
35+
36+
fn main() {
37+
divan::main();
38+
}

0 commit comments

Comments
 (0)