Skip to content

Commit 055b1e2

Browse files
committed
shuf: add benchmarks
To test: uutils#7585
1 parent 35f7db9 commit 055b1e2

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

.github/workflows/benchmarks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
- { package: uu_numfmt }
3838
- { package: uu_rm }
3939
- { package: uu_seq }
40+
- { package: uu_shuf }
4041
- { package: uu_sort }
4142
- { package: uu_split }
4243
- { package: uu_tsort }

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/shuf/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,12 @@ fluent = { workspace = true }
2727
[[bin]]
2828
name = "shuf"
2929
path = "src/main.rs"
30+
31+
[[bench]]
32+
name = "shuf_bench"
33+
harness = false
34+
35+
[dev-dependencies]
36+
divan = { workspace = true }
37+
tempfile = { workspace = true }
38+
uucore = { workspace = true, features = ["benchmark"] }

src/uu/shuf/benches/shuf_bench.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_shuf::uumain;
8+
use uucore::benchmark::{run_util_function, setup_test_file, text_data};
9+
10+
/// Benchmark shuffling lines from a file
11+
/// Tests the default mode with a large number of lines
12+
#[divan::bench(args = [100_000])]
13+
fn shuf_lines(bencher: Bencher, num_lines: usize) {
14+
let data = text_data::generate_by_lines(num_lines, 80);
15+
let file_path = setup_test_file(&data);
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 shuffling a numeric range with -i
24+
/// Tests the input-range mode which uses a different algorithm
25+
#[divan::bench(args = [1_000_000])]
26+
fn shuf_input_range(bencher: Bencher, range_size: usize) {
27+
let range_arg = format!("1-{range_size}");
28+
29+
bencher.bench(|| {
30+
black_box(run_util_function(uumain, &["-i", &range_arg]));
31+
});
32+
}
33+
34+
/// Benchmark shuffling with repeat (sampling with replacement)
35+
/// Tests the -r flag combined with -n to output a specific count
36+
#[divan::bench(args = [50_000])]
37+
fn shuf_repeat_sampling(bencher: Bencher, num_lines: usize) {
38+
let data = text_data::generate_by_lines(10_000, 80);
39+
let file_path = setup_test_file(&data);
40+
let file_path_str = file_path.to_str().unwrap();
41+
let count = format!("{num_lines}");
42+
43+
bencher.bench(|| {
44+
black_box(run_util_function(
45+
uumain,
46+
&["-r", "-n", &count, file_path_str],
47+
));
48+
});
49+
}
50+
51+
fn main() {
52+
divan::main();
53+
}

0 commit comments

Comments
 (0)