|
| 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 | +//! Benchmarks for sort with C locale (fast byte-wise comparison). |
| 7 | +//! |
| 8 | +//! Note: The locale is set in main() BEFORE any benchmark runs because |
| 9 | +//! the locale is cached on first access via OnceLock and cannot be changed afterwards. |
| 10 | +
|
| 11 | +use divan::{Bencher, black_box}; |
| 12 | +use tempfile::NamedTempFile; |
| 13 | +use uu_sort::uumain; |
| 14 | +use uucore::benchmark::{run_util_function, setup_test_file, text_data}; |
| 15 | + |
| 16 | +/// Benchmark ASCII-only data sorting with C locale (byte comparison) |
| 17 | +#[divan::bench] |
| 18 | +fn sort_ascii_c_locale(bencher: Bencher) { |
| 19 | + let data = text_data::generate_ascii_data_simple(100_000); |
| 20 | + let file_path = setup_test_file(&data); |
| 21 | + let output_file = NamedTempFile::new().unwrap(); |
| 22 | + let output_path = output_file.path().to_str().unwrap().to_string(); |
| 23 | + |
| 24 | + bencher.bench(|| { |
| 25 | + black_box(run_util_function( |
| 26 | + uumain, |
| 27 | + &["-o", &output_path, file_path.to_str().unwrap()], |
| 28 | + )); |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +/// Benchmark mixed ASCII/Unicode data with C locale (byte comparison) |
| 33 | +#[divan::bench] |
| 34 | +fn sort_mixed_c_locale(bencher: Bencher) { |
| 35 | + let data = text_data::generate_mixed_locale_data(50_000); |
| 36 | + let file_path = setup_test_file(&data); |
| 37 | + let output_file = NamedTempFile::new().unwrap(); |
| 38 | + let output_path = output_file.path().to_str().unwrap().to_string(); |
| 39 | + |
| 40 | + bencher.bench(|| { |
| 41 | + black_box(run_util_function( |
| 42 | + uumain, |
| 43 | + &["-o", &output_path, file_path.to_str().unwrap()], |
| 44 | + )); |
| 45 | + }); |
| 46 | +} |
| 47 | + |
| 48 | +/// Benchmark German locale-specific data with C locale (byte comparison) |
| 49 | +#[divan::bench] |
| 50 | +fn sort_german_c_locale(bencher: Bencher) { |
| 51 | + let data = text_data::generate_german_locale_data(50_000); |
| 52 | + let file_path = setup_test_file(&data); |
| 53 | + let output_file = NamedTempFile::new().unwrap(); |
| 54 | + let output_path = output_file.path().to_str().unwrap().to_string(); |
| 55 | + |
| 56 | + bencher.bench(|| { |
| 57 | + black_box(run_util_function( |
| 58 | + uumain, |
| 59 | + &["-o", &output_path, file_path.to_str().unwrap()], |
| 60 | + )); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +fn main() { |
| 65 | + // Set C locale BEFORE any benchmarks run. |
| 66 | + // This must happen before divan::main() because the locale is cached |
| 67 | + // on first access via OnceLock and cannot be changed afterwards. |
| 68 | + unsafe { |
| 69 | + std::env::set_var("LC_ALL", "C"); |
| 70 | + } |
| 71 | + divan::main(); |
| 72 | +} |
0 commit comments