summaryrefslogtreecommitdiff
path: root/chomp-bench/benches/json.rs
diff options
context:
space:
mode:
Diffstat (limited to 'chomp-bench/benches/json.rs')
-rw-r--r--chomp-bench/benches/json.rs43
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);