From e85553c6b11db914847fa0e943642f8cd317a688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Fri, 17 May 2024 18:50:40 +0200 Subject: [PATCH] add benches for find slicing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit equal bench with reuse time: [510.30 ns 512.16 ns 514.26 ns] equal bench without reuse time: [21.436 µs 21.456 µs 21.479 µs] regex bench with reuse time: [58.875 µs 58.925 µs 58.975 µs] regex bench without reuse time: [85.324 µs 85.416 µs 85.517 µs] JsonPathInst generation time: [23.988 µs 24.019 µs 24.052 µs] --- Cargo.toml | 9 +++++++++ benches/equal.rs | 41 +++++++++++++++++++++++++++++++++++++++++ benches/regex.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 benches/equal.rs create mode 100644 benches/regex.rs diff --git a/Cargo.toml b/Cargo.toml index 0fd687e..d709ff7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,12 @@ thiserror = "1.0.50" [dev-dependencies] lazy_static = "1.0" +criterion = "0.5.1" + +[[bench]] +name = "regex" +harness = false + +[[bench]] +name = "equal" +harness = false diff --git a/benches/equal.rs b/benches/equal.rs new file mode 100644 index 0000000..10104dc --- /dev/null +++ b/benches/equal.rs @@ -0,0 +1,41 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use jsonpath_rust::{JsonPathInst, JsonPathQuery}; +use serde_json::json; +use std::str::FromStr; + +struct SearchData { + json: serde_json::Value, + path: JsonPathInst, +} + +const PATH: &'static str = "$.[?(@.author == 'abcd(Rees)')]"; + +fn equal_perf_test_with_reuse(cfg: &SearchData) { + let _v = jsonpath_rust::find(&cfg.path, &cfg.json); +} + +fn equal_perf_test_without_reuse() { + let json = Box::new(json!({ + "author":"abcd(Rees)", + })); + + let _v = json.path(PATH).expect("the path is correct"); +} + +pub fn criterion_benchmark(c: &mut Criterion) { + let data = SearchData { + json: json!({ + "author":"abcd(Rees)", + }), + path: JsonPathInst::from_str(PATH).unwrap(), + }; + c.bench_function("equal bench with reuse", |b| { + b.iter(|| equal_perf_test_with_reuse(&data)) + }); + c.bench_function("equal bench without reuse", |b| { + b.iter(|| equal_perf_test_without_reuse()) + }); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/benches/regex.rs b/benches/regex.rs new file mode 100644 index 0000000..6e0941d --- /dev/null +++ b/benches/regex.rs @@ -0,0 +1,48 @@ +use criterion::{criterion_group, criterion_main, Criterion}; +use jsonpath_rust::{JsonPathInst, JsonPathQuery}; +use serde_json::json; +use std::str::FromStr; + +struct SearchData { + json: serde_json::Value, + path: JsonPathInst, +} + +const PATH: &'static str = "$.[?(@.author ~= '.*(?i)d\\(Rees\\)')]"; + +fn regex_perf_test_with_reuse(cfg: &SearchData) { + let _v = jsonpath_rust::find(&cfg.path, &cfg.json); +} + +fn regex_perf_test_without_reuse() { + let json = Box::new(json!({ + "author":"abcd(Rees)", + })); + + let _v = json.path(PATH).expect("the path is correct"); +} + +fn json_path_inst_compiling() { + let _v = JsonPathInst::from_str(PATH).unwrap(); +} + +pub fn criterion_benchmark(c: &mut Criterion) { + let data = SearchData { + json: json!({ + "author":"abcd(Rees)", + }), + path: JsonPathInst::from_str(PATH).unwrap(), + }; + c.bench_function("regex bench with reuse", |b| { + b.iter(|| regex_perf_test_with_reuse(&data)) + }); + c.bench_function("regex bench without reuse", |b| { + b.iter(|| regex_perf_test_without_reuse()) + }); + c.bench_function("JsonPathInst generation", |b| { + b.iter(|| json_path_inst_compiling()) + }); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches);