Merge pull request #369 from quodlibetor/finish-criterion

Finish moving all benchmarks to criterion
This commit is contained in:
Brandon W Maister 2019-11-30 19:45:37 -05:00 committed by GitHub
commit 495243ae3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 63 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 = ["criterion"]
[dependencies] [dependencies]
time = { version = "0.1.39", optional = true } time = { version = "0.1.39", optional = true }
@ -37,6 +37,8 @@ num-integer = { version = "0.1.36", default-features = false }
num-traits = { version = "0.2", default-features = false } num-traits = { version = "0.2", default-features = false }
rustc-serialize = { version = "0.3.20", optional = true } rustc-serialize = { version = "0.3.20", optional = true }
serde = { version = "1.0.99", default-features = false, optional = true } serde = { version = "1.0.99", default-features = false, optional = true }
# technically a dev-dependency, but they can't be optional
criterion = { version = "0.3", optional = true}
[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies] [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
wasm-bindgen = { version = "0.2", optional = true } wasm-bindgen = { version = "0.2", optional = true }
@ -48,7 +50,6 @@ serde_derive = { version = "1", default-features = false }
bincode = { version = "0.8.0" } bincode = { version = "0.8.0" }
num-iter = { version = "0.1.35", default-features = false } num-iter = { version = "0.1.35", default-features = false }
doc-comment = "0.3" doc-comment = "0.3"
criterion = "0.3"
[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies] [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dev-dependencies]
wasm-bindgen-test = "0.2" wasm-bindgen-test = "0.2"
@ -60,7 +61,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) {