Skip to content

Commit

Permalink
bench for jq
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed May 1, 2024
1 parent 8ff3241 commit 6f49c1a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions benches/jq.rs
Original file line number Diff line number Diff line change
@@ -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<BenchData> {
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);
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

1 comment on commit 6f49c1a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 7.76ms 3.55ms 94.26ms 72.29%
Req/Sec 3.26k 261.61 3.73k 85.42%

389596 requests in 30.01s, 1.95GB read

Requests/sec: 12981.19

Transfer/sec: 66.63MB

Please sign in to comment.