|
| 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