From 6f49c1a06aa8091391f21e8e92bcd7b3aabfdb17 Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Wed, 1 May 2024 16:05:17 +0530 Subject: [PATCH] bench for jq --- benches/jq.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 benches/jq.rs diff --git a/benches/jq.rs b/benches/jq.rs new file mode 100644 index 0000000000..e8c8183623 --- /dev/null +++ b/benches/jq.rs @@ -0,0 +1,40 @@ +use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; +use serde_json::json; +use tailcall::blueprint::DynamicValue; +use tailcall::serde_value_ext::ValueExt; + +struct BenchData { + id: String, + ctx: serde_json::Value, + dynamic_value: DynamicValue, +} + +fn setup() -> Vec { + let mut data = Vec::new(); + let ctx = json!({"user": {"name": "John Doe","details": {"age": 30,"city": "New York"}}}); + let template = json!({"name": "{{jq: .user.name}}"}); + let dynamic_value = DynamicValue::try_from(&template).unwrap(); + data.push(BenchData { id: "t1".to_string(), ctx, dynamic_value }); + + let ctx = json!([{"name": "Alice", "age": 25, "city": "New York"},{"name": "Bob", "age": 30, "city": "Chicago"},{"name": "Sandip", "age": 16, "city": "GoodQuestion"},{"name": "Charlie", "age": 22, "city": "San Francisco"}]); + let template = json!({"a": "{{jq: sort_by(.age) | .[0].name }}"}); + let dynamic_value = DynamicValue::try_from(&template).unwrap(); + data.push(BenchData { id: "t2".to_string(), ctx, dynamic_value }); + + data +} + +fn jq_benchmark(c: &mut Criterion) { + let data = setup(); + for datum in data { + c.bench_with_input(BenchmarkId::new("data", &datum.id), &datum, |b, datum| { + b.iter(|| { + let result = datum.dynamic_value.render_value(&datum.ctx); + black_box(result).unwrap(); + }) + }); + } +} + +criterion_group!(benches, jq_benchmark); +criterion_main!(benches); diff --git a/src/lib.rs b/src/lib.rs index 6ab74992b2..e3848e88dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ mod rest; pub mod runtime; pub mod scalar; mod schema_extension; -mod serde_value_ext; +pub mod serde_value_ext; pub mod tracing; pub mod try_fold; pub mod valid;