Skip to content

Commit

Permalink
add benches for find slicing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
xMAC94x committed May 17, 2024
1 parent 994a3ec commit 95b99a8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ thiserror = "1.0.50"

[dev-dependencies]
lazy_static = "1.0"
criterion = "0.5.1"

[[bench]]
name = "regex_bench"
harness = false
41 changes: 41 additions & 0 deletions benches/regex_bench.rs
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 95b99a8

Please sign in to comment.