summaryrefslogtreecommitdiffstats
path: root/vendor/winnow/examples/json/bench.rs
blob: d074ba52b70e1f8a676e4e5b386950d4c16a0478 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use winnow::prelude::*;
use winnow::Partial;

mod json;
mod parser;
mod parser_dispatch;
mod parser_partial;

fn json_bench(c: &mut criterion::Criterion) {
    let data = [("small", SMALL), ("canada", CANADA)];
    let mut group = c.benchmark_group("json");
    for (name, sample) in data {
        let len = sample.len();
        group.throughput(criterion::Throughput::Bytes(len as u64));

        group.bench_with_input(criterion::BenchmarkId::new("basic", name), &len, |b, _| {
            type Error<'i> = winnow::error::InputError<parser::Stream<'i>>;

            b.iter(|| parser::json::<Error>.parse_peek(sample).unwrap());
        });
        group.bench_with_input(criterion::BenchmarkId::new("unit", name), &len, |b, _| {
            type Error<'i> = ();

            b.iter(|| parser::json::<Error>.parse_peek(sample).unwrap());
        });
        group.bench_with_input(
            criterion::BenchmarkId::new("context", name),
            &len,
            |b, _| {
                type Error<'i> = winnow::error::ContextError<parser::Stream<'i>>;

                b.iter(|| parser::json::<Error>.parse_peek(sample).unwrap());
            },
        );
        group.bench_with_input(
            criterion::BenchmarkId::new("dispatch", name),
            &len,
            |b, _| {
                type Error<'i> = winnow::error::InputError<parser_dispatch::Stream<'i>>;

                b.iter(|| parser_dispatch::json::<Error>.parse_peek(sample).unwrap());
            },
        );
        group.bench_with_input(
            criterion::BenchmarkId::new("streaming", name),
            &len,
            |b, _| {
                type Error<'i> = winnow::error::InputError<parser_partial::Stream<'i>>;

                b.iter(|| {
                    parser_partial::json::<Error>
                        .parse_peek(Partial::new(sample))
                        .unwrap()
                });
            },
        );
    }
    group.finish();
}

const SMALL: &str = "  { \"a\"\t: 42,
  \"b\": [ \"x\", \"y\", 12 ,\"\\u2014\", \"\\uD83D\\uDE10\"] ,
  \"c\": { \"hello\" : \"world\"
  }
  }  ";

const CANADA: &str = include_str!("../../third_party/nativejson-benchmark/data/canada.json");

criterion::criterion_group!(benches, json_bench,);
criterion::criterion_main!(benches);