|
| 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::io::Write; |
| 8 | +use tempfile::NamedTempFile; |
| 9 | +use uu_date::uumain; |
| 10 | +use uucore::benchmark::run_util_function; |
| 11 | + |
| 12 | +/// Helper to create a temporary file containing N lines of date strings. |
| 13 | +fn setup_date_file(lines: usize, date_format: &str) -> NamedTempFile { |
| 14 | + let mut file = NamedTempFile::new().unwrap(); |
| 15 | + for _ in 0..lines { |
| 16 | + writeln!(file, "{date_format}").unwrap(); |
| 17 | + } |
| 18 | + file |
| 19 | +} |
| 20 | + |
| 21 | +/// Benchmarks processing a file containing simple ISO dates. |
| 22 | +#[divan::bench(args = [100, 1_000, 10_000])] |
| 23 | +fn file_iso_dates(bencher: Bencher, count: usize) { |
| 24 | + let file = setup_date_file(count, "2023-05-10 12:00:00"); |
| 25 | + let path = file.path().to_str().unwrap(); |
| 26 | + |
| 27 | + bencher.bench(|| { |
| 28 | + black_box(run_util_function(uumain, &["-f", path])); |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +/// Benchmarks processing a file containing dates with Timezone abbreviations. |
| 33 | +#[divan::bench(args = [100, 1_000, 10_000])] |
| 34 | +fn file_tz_abbreviations(bencher: Bencher, count: usize) { |
| 35 | + // "EST" triggers the abbreviation lookup and double-parsing logic |
| 36 | + let file = setup_date_file(count, "2023-05-10 12:00:00 EST"); |
| 37 | + let path = file.path().to_str().unwrap(); |
| 38 | + |
| 39 | + bencher.bench(|| { |
| 40 | + black_box(run_util_function(uumain, &["-f", path])); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +/// Benchmarks formatting speed using a custom output format. |
| 45 | +#[divan::bench(args = [1_000])] |
| 46 | +fn file_custom_format(bencher: Bencher, count: usize) { |
| 47 | + let file = setup_date_file(count, "2023-05-10 12:00:00"); |
| 48 | + let path = file.path().to_str().unwrap(); |
| 49 | + |
| 50 | + bencher.bench(|| { |
| 51 | + black_box(run_util_function(uumain, &["-f", path, "+%A %d %B %Y"])); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +/// Benchmarks the overhead of starting the utility for a single date (no file). |
| 56 | +#[divan::bench] |
| 57 | +fn single_date_now(bencher: Bencher) { |
| 58 | + bencher.bench(|| { |
| 59 | + black_box(run_util_function(uumain, &[])); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +/// Benchmarks parsing a complex relative date string passed as an argument. |
| 64 | +#[divan::bench] |
| 65 | +fn complex_relative_date(bencher: Bencher) { |
| 66 | + bencher.bench(|| { |
| 67 | + black_box(run_util_function( |
| 68 | + uumain, |
| 69 | + &["--date=last friday 12:00 + 2 days"], |
| 70 | + )); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +fn main() { |
| 75 | + divan::main(); |
| 76 | +} |
0 commit comments