Skip to content

Commit 211a36d

Browse files
committed
bench: add benchmarks for parsing ISO 8601 durations
I'm kind of surprised I hadn't added these yet. But I think among Jiff, Chrono and `time`, Jiff is the only one to support them. I'm going to be touching the implementation (in persuit of adding support for parsing/printing `std::time::Duration` directly), so I want to make sure I am at least not regressing perf.
1 parent aae54ca commit 211a36d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

bench/src/parse.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{benchmark, convert::ConvertFrom};
88
pub(super) fn define(c: &mut Criterion) {
99
parse_civil_datetime(c);
1010
parse_friendly(c);
11+
parse_iso8601_duration(c);
1112
parse_rfc2822(c);
1213
parse_strptime(c);
1314
}
@@ -217,6 +218,66 @@ fn parse_friendly(c: &mut Criterion) {
217218
}
218219
}
219220

221+
/// Benchmarks parsing an ISO 8601 duration. I think Jiff is alone in being
222+
/// able to parse ISO 8601 durations (among itself, chrono and time).
223+
fn parse_iso8601_duration(c: &mut Criterion) {
224+
use jiff::{SignedDuration, Span, ToSpan};
225+
226+
const NAME: &str = "parse/iso8601_duration";
227+
const TINY: &str = "PT2S";
228+
const SHORT: &str = "PT2H30M";
229+
const MEDIUM: &str = "P2DT5H30M";
230+
const LONG: &str = "P2Y1M15DT5H59M1S";
231+
const LONG_TIME: &str = "PT5H59M1.123456789S";
232+
233+
let benches = [
234+
("tiny", TINY, 2.seconds()),
235+
("short", SHORT, 2.hours().minutes(30)),
236+
("medium", MEDIUM, 2.days().hours(5).minutes(30)),
237+
(
238+
"long",
239+
LONG,
240+
2.years().months(1).days(15).hours(5).minutes(59).seconds(1),
241+
),
242+
(
243+
"long-time",
244+
LONG_TIME,
245+
5.hours()
246+
.minutes(59)
247+
.seconds(1)
248+
.milliseconds(123)
249+
.microseconds(456)
250+
.nanoseconds(789),
251+
),
252+
];
253+
for (kind, input, expected) in benches {
254+
benchmark(c, format!("{NAME}/{kind}/span/jiff"), |b| {
255+
b.iter(|| {
256+
let got: Span = input.parse().unwrap();
257+
assert_eq!(got.fieldwise(), expected.fieldwise());
258+
})
259+
});
260+
}
261+
262+
let benches = [
263+
("tiny", TINY, SignedDuration::new(2, 0)),
264+
("short", SHORT, SignedDuration::new(2 * 60 * 60 + 30 * 60, 0)),
265+
(
266+
"long-time",
267+
LONG_TIME,
268+
SignedDuration::new(5 * 3600 + 59 * 60 + 1, 123_456_789),
269+
),
270+
];
271+
for (kind, input, expected) in benches {
272+
benchmark(c, format!("{NAME}/{kind}/duration/jiff"), |b| {
273+
b.iter(|| {
274+
let got: SignedDuration = input.parse().unwrap();
275+
assert_eq!(got, expected);
276+
})
277+
});
278+
}
279+
}
280+
220281
/// Measures the time it takes to parse an RFC 2822 datetime as a timestamp.
221282
fn parse_rfc2822(c: &mut Criterion) {
222283
const NAME: &str = "parse/rfc2822";

0 commit comments

Comments
 (0)