| 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::get_bench_args; |
| 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] |
| 23 |
fn file_iso_dates(bencher: Bencher) { |
| 24 |
let count = 1_000; |
| 25 |
let file = setup_date_file(count, "2023-05-10 12:00:00"); |
| 26 |
let path = file.path(); |
| 27 |
|
| 28 |
bencher |
| 29 |
.with_inputs(|| get_bench_args(&[&"-f", &path]).into_iter()) |
| 30 |
.bench_values(|args| black_box(uumain(args))); |
| 31 |
} |
| 32 |
|
| 33 |
/// Benchmarks processing a file containing dates with Timezone abbreviations. |
| 34 |
#[divan::bench] |
| 35 |
fn file_tz_abbreviations(bencher: Bencher) { |
| 36 |
let count = 1_000; |
| 37 |
// "EST" triggers the abbreviation lookup and double-parsing logic |
| 38 |
let file = setup_date_file(count, "2023-05-10 12:00:00 EST"); |
| 39 |
let path = file.path(); |
| 40 |
|
| 41 |
bencher |
| 42 |
.with_inputs(|| get_bench_args(&[&"-f", &path]).into_iter()) |
| 43 |
.bench_values(|args| black_box(uumain(args))); |
| 44 |
} |
| 45 |
|
| 46 |
/// Benchmarks formatting speed using a custom output format. |
| 47 |
#[divan::bench] |
| 48 |
fn file_custom_format(bencher: Bencher) { |
| 49 |
let count = 1_000; |
| 50 |
let file = setup_date_file(count, "2023-05-10 12:00:00"); |
| 51 |
let path = file.path(); |
| 52 |
|
| 53 |
bencher |
| 54 |
.with_inputs(|| get_bench_args(&[&"-f", &path, &"+%A %d %B %Y"]).into_iter()) |
| 55 |
.bench_values(|args| black_box(uumain(args))); |
| 56 |
} |
| 57 |
|
| 58 |
/// Benchmarks the overhead of starting the utility for a single date (no file). |
| 59 |
#[divan::bench] |
| 60 |
fn single_date_now(bencher: Bencher) { |
| 61 |
bencher |
| 62 |
.with_inputs(|| get_bench_args(&[]).into_iter()) |
| 63 |
.bench_values(|args| black_box(uumain(args))); |
| 64 |
} |
| 65 |
|
| 66 |
/// Benchmarks parsing a complex relative date string passed as an argument. |
| 67 |
#[divan::bench] |
| 68 |
fn complex_relative_date(bencher: Bencher) { |
| 69 |
bencher |
| 70 |
.with_inputs(|| get_bench_args(&[&"--date=last friday 12:00 + 2 days"]).into_iter()) |
| 71 |
.bench_values(|args| black_box(uumain(args))); |
| 72 |
} |
| 73 |
|
| 74 |
fn main() { |
| 75 |
divan::main(); |
| 76 |
} |