From 95b99a83a63fbcb0015554386d6a7da64333d7eb 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 regex bench without cache time: [85.023 µs 85.199 µs 85.434 µs] Found 18 outliers among 100 measurements (18.00%) 4 (4.00%) high mild 14 (14.00%) high severe regex bench with cache time: [550.84 ns 551.27 ns 551.69 ns] Found 2 outliers among 100 measurements (2.00%) 2 (2.00%) high severe --- Cargo.toml | 5 +++++ benches/regex_bench.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 benches/regex_bench.rs diff --git a/Cargo.toml b/Cargo.toml index 0fd687e..0c0ebe3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,8 @@ thiserror = "1.0.50" [dev-dependencies] lazy_static = "1.0" +criterion = "0.5.1" + +[[bench]] +name = "regex_bench" +harness = false diff --git a/benches/regex_bench.rs b/benches/regex_bench.rs new file mode 100644 index 0000000..4471219 --- /dev/null +++ b/benches/regex_bench.rs @@ -0,0 +1,41 @@ +use criterion::{black_box, 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 path_with_cache(cfg: &SearchData) { + let _v = jsonpath_rust::find(&cfg.path, &cfg.json); +} + +fn path_without_cache() { + 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("$..book[?(@.author size 10)].title").unwrap(), + }; + c.bench_function("regex bench without cache", |b| { + b.iter(|| path_without_cache()) + }); + c.bench_function("regex bench with cache", |b| { + b.iter(|| path_with_cache(&data)) + }); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches);