• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
json_bench(c: &mut criterion::Criterion)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::ContextError;
21 
22                 b.iter(|| parser_dispatch::json::<Error>.parse_peek(sample).unwrap());
23             },
24         );
25         group.bench_with_input(
26             criterion::BenchmarkId::new("empty-error", name),
27             &len,
28             |b, _| {
29                 type Error<'i> = winnow::error::EmptyError;
30 
31                 b.iter(|| {
32                     parser_dispatch::json::<Error<'_>>
33                         .parse_peek(sample)
34                         .unwrap()
35                 });
36             },
37         );
38         group.bench_with_input(criterion::BenchmarkId::new("alt", name), &len, |b, _| {
39             type Error = winnow::error::ContextError;
40 
41             b.iter(|| parser_alt::json::<Error>.parse_peek(sample).unwrap());
42         });
43         group.bench_with_input(
44             criterion::BenchmarkId::new("streaming", name),
45             &len,
46             |b, _| {
47                 type Error = winnow::error::ContextError;
48 
49                 b.iter(|| {
50                     parser_partial::json::<Error>
51                         .parse_peek(Partial::new(sample))
52                         .unwrap()
53                 });
54             },
55         );
56     }
57     group.finish();
58 }
59 
60 const SMALL: &str = "  { \"a\"\t: 42,
61   \"b\": [ \"x\", \"y\", 12 ,\"\\u2014\", \"\\uD83D\\uDE10\"] ,
62   \"c\": { \"hello\" : \"world\"
63   }
64   }  ";
65 
66 const CANADA: &str = include_str!("../../third_party/nativejson-benchmark/data/canada.json");
67 
68 criterion::criterion_group!(benches, json_bench,);
69 criterion::criterion_main!(benches);
70