jsonpath/benches/bench.rs

71 lines
1.5 KiB
Rust
Raw Normal View History

2019-02-19 08:20:59 +09:00
#![feature(test)]
2019-03-07 18:44:06 +09:00
extern crate jsonpath_lib as jsonpath;
2019-02-19 08:20:59 +09:00
extern crate serde_json;
extern crate test;
use std::io::Read;
use serde_json::Value;
use self::test::Bencher;
fn read_json(path: &str) -> String {
let mut f = std::fs::File::open(path).unwrap();
let mut contents = String::new();
f.read_to_string(&mut contents).unwrap();
contents
}
fn get_string() -> String {
read_json("./benches/example.json")
}
fn get_json() -> Value {
let string = get_string();
serde_json::from_str(string.as_str()).unwrap()
}
fn get_path() -> &'static str {
r#"$..book[?(@.price<30 && @.category=="fiction")]"#
}
2019-02-19 08:20:59 +09:00
#[bench]
2019-03-14 22:30:42 +09:00
fn bench_selector(b: &mut Bencher) {
let json = get_json();
2019-03-14 22:30:42 +09:00
let mut selector = jsonpath::selector(&json);
2019-02-19 08:20:59 +09:00
b.iter(move || {
for _ in 1..100 {
let _ = selector(get_path()).unwrap();
2019-03-14 22:30:42 +09:00
}
});
}
#[bench]
fn bench_select_val(b: &mut Bencher) {
let json = get_json();
2019-03-14 22:30:42 +09:00
b.iter(move || {
for _ in 1..100 {
let _ = jsonpath::select(&json, get_path()).unwrap();
}
});
}
#[bench]
fn bench_select_str(b: &mut Bencher) {
let json = get_string();
b.iter(move || {
for _ in 1..100 {
let _ = jsonpath::select_str(&json, get_path()).unwrap();
2019-03-14 22:30:42 +09:00
}
});
}
#[bench]
fn bench_compile(b: &mut Bencher) {
let json = get_json();
let mut template = jsonpath::compile(get_path());
2019-03-14 22:30:42 +09:00
b.iter(move || {
for _ in 1..100 {
2019-03-14 22:30:42 +09:00
let _ = template(&json).unwrap();
2019-02-19 08:20:59 +09:00
}
});
}