summaryrefslogtreecommitdiffstats
path: root/third_party/rust/chrono/benches
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/chrono/benches
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/chrono/benches')
-rw-r--r--third_party/rust/chrono/benches/chrono.rs116
-rw-r--r--third_party/rust/chrono/benches/serde.rs30
2 files changed, 146 insertions, 0 deletions
diff --git a/third_party/rust/chrono/benches/chrono.rs b/third_party/rust/chrono/benches/chrono.rs
new file mode 100644
index 0000000000..1c640634ac
--- /dev/null
+++ b/third_party/rust/chrono/benches/chrono.rs
@@ -0,0 +1,116 @@
+//! Benchmarks for chrono that just depend on std
+
+extern crate chrono;
+extern crate criterion;
+
+use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
+
+use chrono::prelude::*;
+use chrono::{DateTime, FixedOffset, Utc, __BenchYearFlags};
+
+fn bench_datetime_parse_from_rfc2822(c: &mut Criterion) {
+ c.bench_function("bench_datetime_parse_from_rfc2822", |b| {
+ b.iter(|| {
+ let str = black_box("Wed, 18 Feb 2015 23:16:09 +0000");
+ DateTime::parse_from_rfc2822(str).unwrap()
+ })
+ });
+}
+
+fn bench_datetime_parse_from_rfc3339(c: &mut Criterion) {
+ c.bench_function("bench_datetime_parse_from_rfc3339", |b| {
+ b.iter(|| {
+ let str = black_box("2015-02-18T23:59:60.234567+05:00");
+ DateTime::parse_from_rfc3339(str).unwrap()
+ })
+ });
+}
+
+fn bench_datetime_from_str(c: &mut Criterion) {
+ c.bench_function("bench_datetime_from_str", |b| {
+ b.iter(|| {
+ use std::str::FromStr;
+ let str = black_box("2019-03-30T18:46:57.193Z");
+ DateTime::<Utc>::from_str(str).unwrap()
+ })
+ });
+}
+
+fn bench_datetime_to_rfc2822(c: &mut Criterion) {
+ let pst = FixedOffset::east(8 * 60 * 60);
+ let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_000);
+ c.bench_function("bench_datetime_to_rfc2822", |b| b.iter(|| black_box(dt).to_rfc2822()));
+}
+
+fn bench_datetime_to_rfc3339(c: &mut Criterion) {
+ let pst = FixedOffset::east(8 * 60 * 60);
+ let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_000);
+ c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339()));
+}
+
+fn bench_year_flags_from_year(c: &mut Criterion) {
+ c.bench_function("bench_year_flags_from_year", |b| {
+ b.iter(|| {
+ for year in -999i32..1000 {
+ __BenchYearFlags::from_year(year);
+ }
+ })
+ });
+}
+
+/// Returns the number of multiples of `div` in the range `start..end`.
+///
+/// If the range `start..end` is back-to-front, i.e. `start` is greater than `end`, the
+/// behaviour is defined by the following equation:
+/// `in_between(start, end, div) == - in_between(end, start, div)`.
+///
+/// When `div` is 1, this is equivalent to `end - start`, i.e. the length of `start..end`.
+///
+/// # Panics
+///
+/// Panics if `div` is not positive.
+fn in_between(start: i32, end: i32, div: i32) -> i32 {
+ assert!(div > 0, "in_between: nonpositive div = {}", div);
+ let start = (start.div_euclid(div), start.rem_euclid(div));
+ let end = (end.div_euclid(div), end.rem_euclid(div));
+ // The lowest multiple of `div` greater than or equal to `start`, divided.
+ let start = start.0 + (start.1 != 0) as i32;
+ // The lowest multiple of `div` greater than or equal to `end`, divided.
+ let end = end.0 + (end.1 != 0) as i32;
+ end - start
+}
+
+/// Alternative implementation to `Datelike::num_days_from_ce`
+fn num_days_from_ce_alt<Date: Datelike>(date: &Date) -> i32 {
+ let year = date.year();
+ let diff = move |div| in_between(1, year, div);
+ // 365 days a year, one more in leap years. In the gregorian calendar, leap years are all
+ // the multiples of 4 except multiples of 100 but including multiples of 400.
+ date.ordinal() as i32 + 365 * diff(1) + diff(4) - diff(100) + diff(400)
+}
+
+fn bench_num_days_from_ce(c: &mut Criterion) {
+ let mut group = c.benchmark_group("num_days_from_ce");
+ for year in &[1, 500, 2000, 2019] {
+ let d = NaiveDate::from_ymd(*year, 1, 1);
+ group.bench_with_input(BenchmarkId::new("new", year), &d, |b, y| {
+ b.iter(|| num_days_from_ce_alt(y))
+ });
+ group.bench_with_input(BenchmarkId::new("classic", year), &d, |b, y| {
+ b.iter(|| y.num_days_from_ce())
+ });
+ }
+}
+
+criterion_group!(
+ benches,
+ bench_datetime_parse_from_rfc2822,
+ bench_datetime_parse_from_rfc3339,
+ bench_datetime_from_str,
+ bench_datetime_to_rfc2822,
+ bench_datetime_to_rfc3339,
+ bench_year_flags_from_year,
+ bench_num_days_from_ce,
+);
+
+criterion_main!(benches);
diff --git a/third_party/rust/chrono/benches/serde.rs b/third_party/rust/chrono/benches/serde.rs
new file mode 100644
index 0000000000..860b06e1ae
--- /dev/null
+++ b/third_party/rust/chrono/benches/serde.rs
@@ -0,0 +1,30 @@
+extern crate chrono;
+extern crate criterion;
+
+use criterion::{black_box, criterion_group, criterion_main, Criterion};
+
+use chrono::NaiveDateTime;
+
+fn bench_ser_naivedatetime_string(c: &mut Criterion) {
+ c.bench_function("bench_ser_naivedatetime_string", |b| {
+ let dt: NaiveDateTime = "2000-01-01T00:00:00".parse().unwrap();
+ b.iter(|| {
+ black_box(serde_json::to_string(&dt)).unwrap();
+ });
+ });
+}
+
+fn bench_ser_naivedatetime_writer(c: &mut Criterion) {
+ c.bench_function("bench_ser_naivedatetime_writer", |b| {
+ let mut s: Vec<u8> = Vec::with_capacity(20);
+ let dt: NaiveDateTime = "2000-01-01T00:00:00".parse().unwrap();
+ b.iter(|| {
+ let s = &mut s;
+ s.clear();
+ black_box(serde_json::to_writer(s, &dt)).unwrap();
+ });
+ });
+}
+
+criterion_group!(benches, bench_ser_naivedatetime_writer, bench_ser_naivedatetime_string);
+criterion_main!(benches);