Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calendar benchmarks #1887

Merged
merged 21 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
58b1d0e
Initial drafting of calendar benchmarks
andrewpollack May 16, 2022
a7fcfd8
Extracting tests into external function
andrewpollack May 17, 2022
b568781
Extracting tests into external function
andrewpollack May 17, 2022
c94cb01
Pulled benches into own function
andrewpollack May 17, 2022
4306a93
Adding datetime benches
andrewpollack May 17, 2022
4b9670d
Merge branch 'main' into i/1791/calendar-benches
andrewpollack May 18, 2022
e6695c0
Refactoring code to be more generic
andrewpollack May 18, 2022
621a02d
Adding comments
andrewpollack May 18, 2022
0d92d91
Make outliers into close to generic
andrewpollack May 18, 2022
867003a
Adding nanoseconds to test data
andrewpollack May 18, 2022
3b307fd
Removing nanosecond from ethiopic datetime
andrewpollack May 19, 2022
15ecf15
Removing nano from gregorian datetime creation, new_gregorian_datetim…
andrewpollack May 19, 2022
7b8ecd1
Adding issue link
andrewpollack May 20, 2022
e24613f
Merge branch 'main' into i/1791/calendar-benches
andrewpollack May 20, 2022
3b00440
Merge branch 'main' into i/1791/calendar-benches
andrewpollack May 20, 2022
a01cc2a
Adjusting datetime sample numbers to be coptic friendly
andrewpollack May 20, 2022
734f394
Substituting vector of mutable for mutable array
andrewpollack May 20, 2022
a1da840
PR followups
andrewpollack May 20, 2022
b2ef236
Merge branch 'main' into i/1791/calendar-benches
andrewpollack Jun 1, 2022
b2d12d7
Removing unnecessary comment
andrewpollack Jun 1, 2022
d4256a1
Adjusting ethiopic instantiation
andrewpollack Jun 1, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions components/calendar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,21 @@ zerovec = { version = "0.7", path = "../../utils/zerovec", default-features = fa
crabbake = { version = "0.4", path = "../../experimental/crabbake", optional = true, features = ["derive"] }

[dev-dependencies]
criterion = "0.3"
icu = { path = "../icu", default-features = false }
icu_benchmark_macros = { version = "0.6", path = "../../tools/benchmark/macros" }
icu_calendar = { version = "0.6", path = "../calendar", features = ["serde"] }
icu_testdata = { version = "0.6", path = "../../provider/testdata" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[[bench]]
name = "date"
harness = false

[[bench]]
name = "datetime"
harness = false

[[example]]
name = "iso_date_manipulations"
Expand Down
159 changes: 159 additions & 0 deletions components/calendar/benches/date.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

mod fixtures;

use crate::fixtures::structs::DateFixture;
use criterion::{
black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion,
};
use icu_calendar::{
iso::IsoDay, iso::IsoMonth, iso::IsoYear, AsCalendar, Calendar, Date, DateDuration,
};

fn bench_date<A: AsCalendar>(date: &mut Date<A>) {
// black_box used to avoid compiler optimization.
// Arithmetic
date.add(DateDuration::new(
black_box(1),
black_box(2),
black_box(3),
black_box(4),
));

// Retrieving vals
let _ = black_box(date.year().number);
let _ = black_box(date.month().number);
let _ = black_box(date.day_of_month().0);

// Conversion to ISO.
let _ = black_box(date.to_iso());
}

#[cfg(feature = "bench")]
fn bench_calendar<C: Clone + Calendar>(
group: &mut BenchmarkGroup<WallTime>,
name: &str,
fxs: &DateFixture,
calendar: C,
calendar_date_init: impl Fn(i32, u8, u8) -> Date<C>,
) {
group.bench_function(name, |b| {
b.iter(|| {
for fx in &fxs.0 {
// Instantion from int
let mut instantiated_date_calendar = calendar_date_init(fx.year, fx.month, fx.day);

// Conversion from ISO
let date_iso = Date::new_iso_date_from_integers(fx.year, fx.month, fx.day).unwrap();
let mut converted_date_calendar = Date::new_from_iso(date_iso, calendar.clone());

bench_date(&mut instantiated_date_calendar);
bench_date(&mut converted_date_calendar);
}
})
});
}

fn bench_calendar_iso_types<C: Clone + Calendar>(
group: &mut BenchmarkGroup<WallTime>,
name: &str,
fxs: &DateFixture,
calendar: C,
calendar_date_init: impl Fn(IsoYear, IsoMonth, IsoDay) -> Date<C>,
) {
use std::convert::TryFrom;

group.bench_function(name, |b| {
b.iter(|| {
for fx in &fxs.0 {
// Conversion from ISO
let date_iso = Date::new_iso_date_from_integers(fx.year, fx.month, fx.day).unwrap();
let mut converted_date_calendar = Date::new_from_iso(date_iso, calendar.clone());

// Instantion from ISO
let iso_year = IsoYear(fx.year);
let iso_month = IsoMonth::try_from(fx.month).unwrap();
let iso_day = IsoDay::try_from(fx.day).unwrap();
let mut iso_insantiated_date_calendar =
calendar_date_init(iso_year, iso_month, iso_day);

bench_date(&mut iso_insantiated_date_calendar);
bench_date(&mut converted_date_calendar);
}
})
});
}

fn date_benches(c: &mut Criterion) {
let mut group = c.benchmark_group("date");
let fxs = fixtures::get_dates_fixture().unwrap();

bench_calendar_iso_types(
&mut group,
"calendar/overview",
&fxs,
icu::calendar::iso::Iso,
|y, m, d| Date::new_iso_date(y, m, d).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/buddhist",
&fxs,
icu::calendar::buddhist::Buddhist,
|y, m, d| Date::new_buddhist_date(y, m, d).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/coptic",
&fxs,
icu::calendar::coptic::Coptic,
|y, m, d| Date::new_coptic_date(y, m, d).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/ethiopic",
&fxs,
icu::calendar::ethiopic::Ethiopic::new(),
|y, m, d| Date::new_ethiopic_date(y, m, d).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/indian",
&fxs,
icu::calendar::indian::Indian,
|y, m, d| Date::new_indian_date(y, m, d).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/julian",
&fxs,
icu::calendar::julian::Julian,
|y, m, d| Date::new_julian_date(y, m, d).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar_iso_types(
&mut group,
"calendar/gregorian",
&fxs,
icu::calendar::gregorian::Gregorian,
|y, m, d| Date::new_gregorian_date(y, m, d).unwrap(),
);

group.finish();
}

criterion_group!(benches, date_benches);
criterion_main!(benches);
137 changes: 137 additions & 0 deletions components/calendar/benches/datetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

mod fixtures;

use crate::fixtures::structs::DateFixture;
use criterion::{
black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion,
};
use icu_calendar::{types::Time, AsCalendar, Calendar, DateDuration, DateTime};

fn bench_datetime<A: AsCalendar>(datetime: &mut DateTime<A>) {
// black_box used to avoid compiler optimization.
// Arithmetic.
datetime.date.add(DateDuration::new(
black_box(1),
black_box(2),
black_box(3),
black_box(4),
));
datetime.time = Time::try_new(black_box(14), black_box(30), black_box(0), black_box(0))
.expect("Failed to initialize Time instance.");

// Retrieving vals
let _ = black_box(datetime.date.year().number);
let _ = black_box(datetime.date.month().number);
let _ = black_box(datetime.date.day_of_month().0);
let _ = black_box(datetime.time.hour);
let _ = black_box(datetime.time.minute);
let _ = black_box(datetime.time.second);

// Conversion to ISO.
let _ = black_box(datetime.to_iso());
}

fn bench_calendar<C: Clone + Calendar>(
group: &mut BenchmarkGroup<WallTime>,
name: &str,
fxs: &DateFixture,
calendar: C,
calendar_datetime_init: impl Fn(i32, u8, u8, u8, u8, u8) -> DateTime<C>,
) {
group.bench_function(name, |b| {
b.iter(|| {
for fx in &fxs.0 {
// Instantion from int
let mut instantiated_datetime_calendar = calendar_datetime_init(
fx.year, fx.month, fx.day, fx.hour, fx.minute, fx.second,
);

// Conversion from ISO
let datetime_iso = DateTime::new_iso_datetime_from_integers(
fx.year, fx.month, fx.day, fx.hour, fx.minute, fx.second,
)
.unwrap();
let mut converted_datetime_calendar =
DateTime::new_from_iso(datetime_iso, calendar.clone());

bench_datetime(&mut instantiated_datetime_calendar);
bench_datetime(&mut converted_datetime_calendar);
}
})
});
}

fn datetime_benches(c: &mut Criterion) {
let mut group = c.benchmark_group("datetime");
let fxs = fixtures::get_dates_fixture().unwrap();

bench_calendar(
&mut group,
"calendar/overview",
&fxs,
icu::calendar::iso::Iso,
|y, m, d, h, min, s| DateTime::new_iso_datetime_from_integers(y, m, d, h, min, s).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/buddhist",
&fxs,
icu::calendar::buddhist::Buddhist,
|y, m, d, h, min, s| DateTime::new_buddhist_datetime(y, m, d, h, min, s).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/coptic",
&fxs,
icu::calendar::coptic::Coptic,
|y, m, d, h, min, s| DateTime::new_coptic_datetime(y, m, d, h, min, s).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/ethiopic",
&fxs,
icu::calendar::ethiopic::Ethiopic::new(),
|y, m, d, h, min, s| DateTime::new_ethiopic_datetime(y, m, d, h, min, s).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/gregorian",
&fxs,
icu::calendar::gregorian::Gregorian,
|y, m, d, h, min, s| DateTime::new_gregorian_datetime(y, m, d, h, min, s).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/indian",
&fxs,
icu::calendar::indian::Indian,
|y, m, d, h, min, s| DateTime::new_indian_datetime(y, m, d, h, min, s).unwrap(),
);

#[cfg(feature = "bench")]
bench_calendar(
&mut group,
"calendar/julian",
&fxs,
icu::calendar::julian::Julian,
|y, m, d, h, min, s| DateTime::new_julian_datetime(y, m, d, h, min, s).unwrap(),
);

group.finish();
}

criterion_group!(benches, datetime_benches);
criterion_main!(benches);
Loading