Skip to content

Commit 971b5d8

Browse files
CrazyRokasylvestre
andauthored
date: add benchmark (#9911)
* date: add benchmark * date: register benchmark in github actions list --------- Co-authored-by: Sylvestre Ledru <[email protected]>
1 parent 0566dfc commit 971b5d8

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

.github/workflows/benchmarks.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ jobs:
4545
- { package: uu_uniq }
4646
- { package: uu_wc }
4747
- { package: uu_factor }
48+
- { package: uu_date }
4849
steps:
4950
- uses: actions/checkout@v6
5051
with:

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ windows-sys = { workspace = true, features = [
4141
[[bin]]
4242
name = "date"
4343
path = "src/main.rs"
44+
45+
[dev-dependencies]
46+
divan = { workspace = true }
47+
tempfile = { workspace = true }
48+
uucore = { workspace = true, features = ["benchmark"] }
49+
50+
[[bench]]
51+
name = "date_bench"
52+
harness = false

src/uu/date/benches/date_bench.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)