Finish moving all benchmarks to criterion

I didn't delete some, apparently, and an internal struct had to be made public
to get its benchmark to work.
This commit is contained in:
Brandon W Maister 2019-11-30 18:11:03 -05:00
parent b553798f86
commit 4f1c35827f
5 changed files with 24 additions and 62 deletions

View File

@ -29,7 +29,7 @@ alloc = []
std = [] std = []
clock = ["time", "std"] clock = ["time", "std"]
wasmbind = ["wasm-bindgen", "js-sys"] wasmbind = ["wasm-bindgen", "js-sys"]
bench = ["std"] __internal_bench = []
[dependencies] [dependencies]
time = { version = "0.1.39", optional = true } time = { version = "0.1.39", optional = true }
@ -60,7 +60,8 @@ all-features = true
all-features = true all-features = true
[[bench]] [[bench]]
name = "parsing" name = "chrono"
required-features = ["__internal_bench"]
harness = false harness = false
[[bench]] [[bench]]

View File

@ -1,10 +1,12 @@
//! Benchmarks for chrono that just depend on std
extern crate chrono; extern crate chrono;
extern crate criterion; extern crate criterion;
use criterion::{black_box, criterion_group, criterion_main, Criterion}; use criterion::{black_box, criterion_group, criterion_main, Criterion};
use chrono::prelude::*; use chrono::prelude::*;
use chrono::{Utc, FixedOffset, DateTime}; use chrono::{Utc, FixedOffset, DateTime, YearFlags};
fn bench_datetime_parse_from_rfc2822(c: &mut Criterion) { fn bench_datetime_parse_from_rfc2822(c: &mut Criterion) {
c.bench_function("bench_datetime_parse_from_rfc2822", |b| { c.bench_function("bench_datetime_parse_from_rfc2822", |b| {
@ -50,13 +52,23 @@ fn bench_datetime_to_rfc3339(c: &mut Criterion) {
}); });
} }
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 {
YearFlags::from_year(year);
}
}));
}
criterion_group!( criterion_group!(
benches, benches,
bench_datetime_parse_from_rfc2822, bench_datetime_parse_from_rfc2822,
bench_datetime_parse_from_rfc3339, bench_datetime_parse_from_rfc3339,
bench_datetime_from_str, bench_datetime_from_str,
bench_datetime_to_rfc2822, bench_datetime_to_rfc2822,
bench_datetime_to_rfc3339 bench_datetime_to_rfc3339,
bench_year_flags_from_year,
); );
criterion_main!(benches); criterion_main!(benches);

View File

@ -2217,52 +2217,4 @@ mod tests {
assert_eq!(format!(" {} ", ymd_formatted), format!("{:^17}", ymd)); assert_eq!(format!(" {} ", ymd_formatted), format!("{:^17}", ymd));
} }
#[cfg(feature = "bench")]
#[bench]
fn bench_datetime_parse_from_rfc2822(bh: &mut test::Bencher) {
bh.iter(|| {
let str = test::black_box("Wed, 18 Feb 2015 23:16:09 +0000");
DateTime::parse_from_rfc2822(str).unwrap()
});
}
#[cfg(feature = "bench")]
#[bench]
fn bench_datetime_parse_from_rfc3339(bh: &mut test::Bencher) {
bh.iter(|| {
let str = test::black_box("2015-02-18T23:59:60.234567+05:00");
DateTime::parse_from_rfc3339(str).unwrap()
});
}
#[cfg(feature = "bench")]
#[bench]
fn bench_datetime_from_str(bh: &mut test::Bencher) {
use std::str::FromStr;
bh.iter(|| {
let str = test::black_box("2019-03-30T18:46:57.193Z");
DateTime::<Utc>::from_str(str).unwrap()
});
}
#[cfg(feature = "bench")]
#[bench]
fn bench_datetime_to_rfc2822(bh: &mut test::Bencher) {
let pst = FixedOffset::east(8 * 60 * 60);
let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_000);
bh.iter(|| {
test::black_box(dt).to_rfc2822()
});
}
#[cfg(feature = "bench")]
#[bench]
fn bench_datetime_to_rfc3339(bh: &mut test::Bencher) {
let pst = FixedOffset::east(8 * 60 * 60);
let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_000);
bh.iter(|| {
test::black_box(dt).to_rfc3339()
});
}
} }

View File

@ -477,7 +477,10 @@ pub mod naive {
//! (e.g. [`TimeZone`](../offset/trait.TimeZone.html)), //! (e.g. [`TimeZone`](../offset/trait.TimeZone.html)),
//! but can be also used for the simpler date and time handling. //! but can be also used for the simpler date and time handling.
#[cfg(not(feature = "__internal_bench"))]
mod internals; mod internals;
#[cfg(feature = "__internal_bench")]
pub mod internals;
mod date; mod date;
mod isoweek; mod isoweek;
mod time; mod time;
@ -510,6 +513,9 @@ mod datetime;
pub mod format; pub mod format;
mod round; mod round;
#[cfg(feature = "__internal_bench")]
pub use naive::internals::YearFlags;
/// Serialization/Deserialization in alternate formats /// Serialization/Deserialization in alternate formats
/// ///
/// The various modules in here are intended to be used with serde's [`with` /// The various modules in here are intended to be used with serde's [`with`

View File

@ -14,6 +14,7 @@
//! so that the user-facing `NaiveDate` can validate the input as late as possible. //! so that the user-facing `NaiveDate` can validate the input as late as possible.
#![allow(dead_code)] // some internal methods have been left for consistency #![allow(dead_code)] // some internal methods have been left for consistency
#![cfg_attr(feature = "__internal_bench", allow(missing_docs))]
use core::{i32, fmt}; use core::{i32, fmt};
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
@ -516,16 +517,6 @@ mod tests {
assert_eq!(GF.nisoweeks(), 52); assert_eq!(GF.nisoweeks(), 52);
} }
#[cfg(feature = "bench")]
#[bench]
fn bench_year_flags_from_year(bh: &mut test::Bencher) {
bh.iter(|| {
for year in -999i32..1000 {
YearFlags::from_year(year);
}
});
}
#[test] #[test]
fn test_of() { fn test_of() {
fn check(expected: bool, flags: YearFlags, ordinal1: u32, ordinal2: u32) { fn check(expected: bool, flags: YearFlags, ordinal1: u32, ordinal2: u32) {