From 4f1c35827f19bab8ca2293cda1b772cb99e5a66f Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Sat, 30 Nov 2019 18:11:03 -0500 Subject: [PATCH] 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. --- Cargo.toml | 5 ++-- benches/{parsing.rs => chrono.rs} | 16 +++++++++-- src/datetime.rs | 48 ------------------------------- src/lib.rs | 6 ++++ src/naive/internals.rs | 11 +------ 5 files changed, 24 insertions(+), 62 deletions(-) rename benches/{parsing.rs => chrono.rs} (81%) diff --git a/Cargo.toml b/Cargo.toml index 945901c..429bf25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ alloc = [] std = [] clock = ["time", "std"] wasmbind = ["wasm-bindgen", "js-sys"] -bench = ["std"] +__internal_bench = [] [dependencies] time = { version = "0.1.39", optional = true } @@ -60,7 +60,8 @@ all-features = true all-features = true [[bench]] -name = "parsing" +name = "chrono" +required-features = ["__internal_bench"] harness = false [[bench]] diff --git a/benches/parsing.rs b/benches/chrono.rs similarity index 81% rename from benches/parsing.rs rename to benches/chrono.rs index 4c051ef..1bd66af 100644 --- a/benches/parsing.rs +++ b/benches/chrono.rs @@ -1,10 +1,12 @@ +//! Benchmarks for chrono that just depend on std + extern crate chrono; extern crate criterion; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use chrono::prelude::*; -use chrono::{Utc, FixedOffset, DateTime}; +use chrono::{Utc, FixedOffset, DateTime, YearFlags}; fn bench_datetime_parse_from_rfc2822(c: &mut Criterion) { 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!( benches, bench_datetime_parse_from_rfc2822, bench_datetime_parse_from_rfc3339, bench_datetime_from_str, bench_datetime_to_rfc2822, - bench_datetime_to_rfc3339 + bench_datetime_to_rfc3339, + bench_year_flags_from_year, ); criterion_main!(benches); diff --git a/src/datetime.rs b/src/datetime.rs index 70400fd..d4de471 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -2217,52 +2217,4 @@ mod tests { 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::::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() - }); - } } diff --git a/src/lib.rs b/src/lib.rs index ef76a7f..6d2e151 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -477,7 +477,10 @@ pub mod naive { //! (e.g. [`TimeZone`](../offset/trait.TimeZone.html)), //! but can be also used for the simpler date and time handling. + #[cfg(not(feature = "__internal_bench"))] mod internals; + #[cfg(feature = "__internal_bench")] + pub mod internals; mod date; mod isoweek; mod time; @@ -510,6 +513,9 @@ mod datetime; pub mod format; mod round; +#[cfg(feature = "__internal_bench")] +pub use naive::internals::YearFlags; + /// Serialization/Deserialization in alternate formats /// /// The various modules in here are intended to be used with serde's [`with` diff --git a/src/naive/internals.rs b/src/naive/internals.rs index d043163..b98787b 100644 --- a/src/naive/internals.rs +++ b/src/naive/internals.rs @@ -14,6 +14,7 @@ //! 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 +#![cfg_attr(feature = "__internal_bench", allow(missing_docs))] use core::{i32, fmt}; use num_traits::FromPrimitive; @@ -516,16 +517,6 @@ mod tests { 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] fn test_of() { fn check(expected: bool, flags: YearFlags, ordinal1: u32, ordinal2: u32) {