| 1 |
use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 2 |
|
| 3 |
use chronoutil::RelativeDuration; |
| 4 |
|
| 5 |
fn relative_duration_format_benchmark(c: &mut Criterion) { |
| 6 |
let durations = [ |
| 7 |
"P1M", |
| 8 |
"P1Y1M1W1DT1H1M1S", |
| 9 |
"P99999999Y11M30DT23H59M59.999999999S", |
| 10 |
] |
| 11 |
.iter() |
| 12 |
.map(|s| RelativeDuration::parse_from_iso8601(s).unwrap()) |
| 13 |
.collect::<Vec<RelativeDuration>>(); |
| 14 |
|
| 15 |
let mut g = c.benchmark_group("relative_duration_format"); |
| 16 |
|
| 17 |
g.bench_function("one_specifier", |b| { |
| 18 |
b.iter(|| black_box(durations[0]).format_to_iso8601()) |
| 19 |
}); |
| 20 |
g.bench_function("all_specifiers", |b| { |
| 21 |
b.iter(|| black_box(durations[1]).format_to_iso8601()) |
| 22 |
}); |
| 23 |
g.bench_function("long_specifiers", |b| { |
| 24 |
b.iter(|| black_box(durations[2]).format_to_iso8601()) |
| 25 |
}); |
| 26 |
} |
| 27 |
|
| 28 |
fn relative_duration_parse_benchmark(c: &mut Criterion) { |
| 29 |
let durations = [ |
| 30 |
"P1M", |
| 31 |
"P1Y1M1W1DT1H1M1S", |
| 32 |
"P99999999Y11M30DT23H59M59.999999999S", |
| 33 |
]; |
| 34 |
|
| 35 |
let mut g = c.benchmark_group("relative_duration_parse"); |
| 36 |
|
| 37 |
g.bench_function("one_specifier", |b| { |
| 38 |
b.iter(|| RelativeDuration::parse_from_iso8601(black_box(durations[0]))) |
| 39 |
}); |
| 40 |
g.bench_function("all_specifiers", |b| { |
| 41 |
b.iter(|| RelativeDuration::parse_from_iso8601(black_box(durations[1]))) |
| 42 |
}); |
| 43 |
g.bench_function("long_specifiers", |b| { |
| 44 |
b.iter(|| RelativeDuration::parse_from_iso8601(black_box(durations[2]))) |
| 45 |
}); |
| 46 |
} |
| 47 |
|
| 48 |
criterion_group!( |
| 49 |
benches, |
| 50 |
relative_duration_format_benchmark, |
| 51 |
relative_duration_parse_benchmark |
| 52 |
); |
| 53 |
criterion_main!(benches); |