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);