Skip to content

Commit

Permalink
add a benchmark for JSON where errors are not generated
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed May 5, 2024
1 parent 5773e90 commit c8819b0
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion benchmarks/benches/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use nom::{
number::double,
number::recognize_float,
sequence::{delimited, preceded, separated_pair},
Complete, Emit, IResult, Mode, OutputM, Parser,
Check, Complete, Emit, IResult, Mode, OutputM, Parser,
};

use std::{collections::HashMap, marker::PhantomData, num::ParseIntError};
Expand Down Expand Up @@ -271,6 +271,28 @@ fn json_bench(c: &mut Criterion) {
});
}

fn json_bench_error_check(c: &mut Criterion) {
let data = " { \"a\"\t: 42,
\"b\": [ \"x\", \"y\", 12 ,\"\\u2014\", \"\\uD83D\\uDE10\"] ,
\"c\": { \"hello\" : \"world\"
}
} ";

// test once to make sure it parses correctly
json::<Error<&str>>()
.process::<OutputM<Emit, Check, Complete>>(data)
.unwrap();

// println!("data:\n{:?}", json(data));
c.bench_function("json", |b| {
b.iter(|| {
json::<Error<&str>>()
.process::<OutputM<Emit, Check, Complete>>(data)
.unwrap()
});
});
}

static CANADA: &str = include_str!("../canada.json");
fn canada_json(c: &mut Criterion) {
// test once to make sure it parses correctly
Expand Down Expand Up @@ -399,6 +421,7 @@ fn std_float_bytes(c: &mut Criterion) {
criterion_group!(
benches,
json_bench,
json_bench_error_check,
verbose_json,
canada_json,
verbose_canada_json,
Expand Down

0 comments on commit c8819b0

Please sign in to comment.