| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
use fundu::TimeUnit::{self, *}; |
| 7 |
use fundu::{Duration, DurationParser, ParseError}; |
| 8 |
use iai_callgrind::{black_box, main}; |
| 9 |
|
| 10 |
type Result<T> = std::result::Result<T, ParseError>; |
| 11 |
|
| 12 |
const INPUT_NO_TIME_UNIT: &str = "1"; |
| 13 |
const INPUT_NANO_SECOND: &str = "1ns"; |
| 14 |
const INPUT_SECOND: &str = "1y"; |
| 15 |
const INPUT_YEAR: &str = "1y"; |
| 16 |
|
| 17 |
#[inline(never)] |
| 18 |
#[export_name = "__iai_setup::setup_parser_with_all_time_units"] |
| 19 |
fn setup_parser_with_all_time_units<'a>(default_unit: TimeUnit) -> DurationParser<'a> { |
| 20 |
let mut parser = DurationParser::with_all_time_units(); |
| 21 |
parser.default_unit(default_unit); |
| 22 |
parser |
| 23 |
} |
| 24 |
|
| 25 |
#[inline(never)] |
| 26 |
fn parsing_nano_second_when_no_time_unit() -> Result<Duration> { |
| 27 |
let time_unit = black_box(NanoSecond); |
| 28 |
let parser = setup_parser_with_all_time_units(time_unit); |
| 29 |
black_box(parser).parse(black_box(INPUT_NO_TIME_UNIT)) |
| 30 |
} |
| 31 |
|
| 32 |
#[inline(never)] |
| 33 |
fn parsing_nano_second_when_time_unit() -> Result<Duration> { |
| 34 |
let time_unit = black_box(NanoSecond); |
| 35 |
let parser = setup_parser_with_all_time_units(time_unit); |
| 36 |
black_box(parser).parse(black_box(INPUT_NANO_SECOND)) |
| 37 |
} |
| 38 |
|
| 39 |
#[inline(never)] |
| 40 |
fn parsing_second_when_no_time_unit() -> Result<Duration> { |
| 41 |
let time_unit = black_box(Second); |
| 42 |
let parser = setup_parser_with_all_time_units(time_unit); |
| 43 |
black_box(parser).parse(black_box(INPUT_NO_TIME_UNIT)) |
| 44 |
} |
| 45 |
|
| 46 |
#[inline(never)] |
| 47 |
fn parsing_second_when_time_unit() -> Result<Duration> { |
| 48 |
let time_unit = black_box(Second); |
| 49 |
let parser = setup_parser_with_all_time_units(time_unit); |
| 50 |
black_box(parser).parse(black_box(INPUT_SECOND)) |
| 51 |
} |
| 52 |
|
| 53 |
#[inline(never)] |
| 54 |
fn parsing_year_when_no_time_unit() -> Result<Duration> { |
| 55 |
let time_unit = black_box(Year); |
| 56 |
let parser = setup_parser_with_all_time_units(time_unit); |
| 57 |
black_box(parser).parse(black_box(INPUT_NO_TIME_UNIT)) |
| 58 |
} |
| 59 |
|
| 60 |
#[inline(never)] |
| 61 |
fn parsing_year_when_time_unit() -> Result<Duration> { |
| 62 |
let time_unit = black_box(Year); |
| 63 |
let parser = setup_parser_with_all_time_units(time_unit); |
| 64 |
black_box(parser).parse(black_box(INPUT_YEAR)) |
| 65 |
} |
| 66 |
|
| 67 |
main!( |
| 68 |
callgrind_args = |
| 69 |
"toggle-collect=iai_callgrind::black_box", |
| 70 |
"toggle-collect=__iai_setup::setup_parser_with_all_time_units"; |
| 71 |
functions = |
| 72 |
parsing_nano_second_when_no_time_unit, |
| 73 |
parsing_nano_second_when_time_unit, |
| 74 |
parsing_second_when_no_time_unit, |
| 75 |
parsing_second_when_time_unit, |
| 76 |
parsing_year_when_no_time_unit, |
| 77 |
parsing_year_when_time_unit |
| 78 |
); |