diff options
author | Greg Brown <gmb60@cam.ac.uk> | 2021-02-06 13:37:03 +0000 |
---|---|---|
committer | Greg Brown <gmb60@cam.ac.uk> | 2021-02-06 13:37:03 +0000 |
commit | 51ec48e03b2e02be02ccb9826b4638d312df67b4 (patch) | |
tree | d284d34eea27e086658170fd3cc56999cfa931c0 /chomp-bench/benches/json.rs | |
parent | bd6d4e22a3980ff937ab3e2df48c064bdc2918bd (diff) |
Add `chomp-bench` crate so benchmarks can use a build script.
Diffstat (limited to 'chomp-bench/benches/json.rs')
-rw-r--r-- | chomp-bench/benches/json.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/chomp-bench/benches/json.rs b/chomp-bench/benches/json.rs new file mode 100644 index 0000000..9d9bf36 --- /dev/null +++ b/chomp-bench/benches/json.rs @@ -0,0 +1,43 @@ +use chewed::{IterWrapper, Parser}; +use chomp_bench::json::*; +use criterion::{ + criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration, + Throughput, +}; + +const INPUTS: &[&str] = &[ + r#"true"#, + r#"[true, false]"#, + r#"{"first" : null, "second" : 123}"#, + r#"{"first": [ true, "Hello there" ], "second": [123, -12.4e-7]}"#, + r#"{"first": [ true, "Hello there" ], "second": [123, -12.4e-7], "third": {"left": "Random text", "right": ["\ud83c\udf24\ufe0f"]}}"#, +]; + +fn parse_chewed(input: &str) -> Value { + IterWrapper::new(input.chars()) + .parse::<nibble::Ast>() + .unwrap() + .into() +} + +fn parse_handwritten(input: &str) -> Value { + IterWrapper::new(input.chars()).parse().unwrap() +} + +fn bench_parse(c: &mut Criterion) { + let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic); + let mut group = c.benchmark_group("JSON"); + group.plot_config(plot_config); + for (i, input) in INPUTS.iter().enumerate() { + group.throughput(Throughput::Bytes(input.len() as u64)); + group.bench_with_input(BenchmarkId::new("Chewed", i), *input, |b, i| { + b.iter(|| parse_chewed(i)) + }); + group.bench_with_input(BenchmarkId::new("Handwritten", i), *input, |b, i| { + b.iter(|| parse_handwritten(i)) + }); + } +} + +criterion_group!(benches, bench_parse); +criterion_main!(benches); |