Debian Codegraph @

debian/rust-vendor/winnow/examples/json/bench.rs

raw
1 use winnow::prelude::*;
2 use winnow::Partial;
3
4 mod json;
5 mod parser_alt;
6 mod parser_dispatch;
7 mod parser_partial;
8
9 fn json_bench(c: &mut criterion::Criterion) {
10 let data = [("small", SMALL), ("canada", CANADA)];
11 let mut group = c.benchmark_group("json");
12 for (name, sample) in data {
13 let len = sample.len();
14 group.throughput(criterion::Throughput::Bytes(len as u64));
15
16 group.bench_with_input(
17 criterion::BenchmarkId::new("dispatch", name),
18 &len,
19 |b, _| {
20 type Error = winnow::error::ErrMode<winnow::error::ContextError>;
21
22 b.iter(|| parser_dispatch::json::<Error>.parse_peek(sample).unwrap());
23 },
24 );
25 group.bench_with_input(
26 criterion::BenchmarkId::new("modeless", name),
27 &len,
28 |b, _| {
29 type Error = winnow::error::ContextError;
30
31 b.iter(|| parser_dispatch::json::<Error>.parse_peek(sample).unwrap());
32 },
33 );
34 group.bench_with_input(
35 criterion::BenchmarkId::new("empty-error", name),
36 &len,
37 |b, _| {
38 type Error<'i> = winnow::error::EmptyError;
39
40 b.iter(|| {
41 parser_dispatch::json::<Error<'_>>
42 .parse_peek(sample)
43 .unwrap()
44 });
45 },
46 );
47 group.bench_with_input(criterion::BenchmarkId::new("alt", name), &len, |b, _| {
48 type Error = winnow::error::ContextError;
49
50 b.iter(|| parser_alt::json::<Error>.parse_peek(sample).unwrap());
51 });
52 group.bench_with_input(
53 criterion::BenchmarkId::new("streaming", name),
54 &len,
55 |b, _| {
56 type Error = winnow::error::ContextError;
57
58 b.iter(|| {
59 parser_partial::json::<Error>
60 .parse_peek(Partial::new(sample))
61 .unwrap()
62 });
63 },
64 );
65 }
66 group.finish();
67 }
68
69 const SMALL: &str = " { \"a\"\t: 42,
70 \"b\": [ \"x\", \"y\", 12 ,\"\\u2014\", \"\\uD83D\\uDE10\"] ,
71 \"c\": { \"hello\" : \"world\"
72 }
73 } ";
74
75 const CANADA: &str = include_str!("../../third_party/nativejson-benchmark/data/canada.json");
76
77 criterion::criterion_group!(benches, json_bench,);
78 criterion::criterion_main!(benches);