Skip to content

Commit

Permalink
refactor: merge benchmarks in tailcall_benches.rs (#1834)
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyfuhrer authored May 2, 2024
1 parent 74d2621 commit 276a612
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 54 deletions.
5 changes: 4 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"image": "mcr.microsoft.com/devcontainers/universal:2",
"features": {
"ghcr.io/devcontainers/features/rust:1": {}
"ghcr.io/devcontainers/features/rust:1": {
"version": "1.75",
"profile": "default"
}
}
}
21 changes: 2 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -229,26 +229,9 @@ codegen-units = 1
opt-level = 'z'

[[bench]]
name = "json_like_bench"
harness = false

[[bench]]
name = "request_template_bench"
harness = false

[[bench]]
name = "data_loader_bench"
harness = false

[[bench]]
name = "impl_path_string_for_evaluation_context"
harness = false

[[bench]]
name = "protobuf_convert_output"
name = "tailcall_benches"
harness = false

[[test]]
name = "execution_spec"
harness = false

harness = false
11 changes: 2 additions & 9 deletions benches/data_loader_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::sync::Arc;
use async_graphql::futures_util::future::join_all;
use async_graphql_value::ConstValue;
use async_trait::async_trait;
use criterion::{criterion_group, criterion_main, Criterion};
use criterion::Criterion;
use hyper::body::Bytes;
use reqwest::Request;
use tailcall::config::Batch;
Expand Down Expand Up @@ -68,7 +68,7 @@ impl tailcall::Cache for Cache {
}
}

fn benchmark_data_loader(c: &mut Criterion) {
pub fn benchmark_data_loader(c: &mut Criterion) {
c.bench_function("test_data_loader", |b| {
b.iter(|| {
let client = Arc::new(MockHttpClient { request_count: Arc::new(AtomicUsize::new(0)) });
Expand Down Expand Up @@ -111,10 +111,3 @@ fn benchmark_data_loader(c: &mut Criterion) {
})
});
}

criterion_group! {
name = benches;
config = Criterion::default();
targets = benchmark_data_loader
}
criterion_main!(benches);
7 changes: 2 additions & 5 deletions benches/impl_path_string_for_evaluation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::time::Duration;
use async_graphql::context::SelectionField;
use async_graphql::{Name, Value};
use async_trait::async_trait;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use criterion::{BenchmarkId, Criterion};
use http_cache_reqwest::{Cache, CacheMode, HttpCache, HttpCacheOptions, MokaManager};
use hyper::body::Bytes;
use hyper::header::HeaderValue;
Expand Down Expand Up @@ -251,7 +251,7 @@ fn request_context() -> RequestContext {
.upstream(upstream)
}

fn bench_main(c: &mut Criterion) {
pub fn bench_main(c: &mut Criterion) {
let mut req_ctx = request_context().allowed_headers(TEST_HEADERS.clone());

req_ctx.server.vars = TEST_VARS.clone();
Expand All @@ -271,6 +271,3 @@ fn bench_main(c: &mut Criterion) {
});
}
}

criterion_group!(benches, bench_main);
criterion_main!(benches);
9 changes: 3 additions & 6 deletions benches/json_like_bench.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{black_box, Criterion};
use serde_json::json;
use tailcall::json::JsonLike;

fn benchmark_batched_body(c: &mut Criterion) {
pub fn benchmark_batched_body(c: &mut Criterion) {
c.bench_function("test_batched_body", |b| {
b.iter(|| {
let input = json!({
Expand Down Expand Up @@ -30,7 +30,7 @@ fn benchmark_batched_body(c: &mut Criterion) {
});
}

fn benchmark_group_by(c: &mut Criterion) {
pub fn benchmark_group_by(c: &mut Criterion) {
let input = json!({
"data": [
{"type": "A", "value": {"id": "1"}},
Expand All @@ -47,6 +47,3 @@ fn benchmark_group_by(c: &mut Criterion) {
})
});
}

criterion_group!(benches, benchmark_batched_body, benchmark_group_by);
criterion_main!(benches);
7 changes: 2 additions & 5 deletions benches/protobuf_convert_output.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;

use anyhow::Result;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{black_box, Criterion};
use rand::{thread_rng, Fill};
use serde_json::{json, Value};
use tailcall::blueprint::GrpcMethod;
Expand Down Expand Up @@ -42,7 +42,7 @@ fn create_dummy_value(n: usize, m: usize) -> Result<Value> {
Ok(value)
}

fn benchmark_convert_output(c: &mut Criterion) {
pub fn benchmark_convert_output(c: &mut Criterion) {
let proto_file_path = Path::new(PROTO_DIR).join(PROTO_FILE);
let file_descriptor_set = protox::compile([proto_file_path], ["."]).unwrap();
let protobuf_set = ProtobufSet::from_proto_file(file_descriptor_set).unwrap();
Expand All @@ -65,6 +65,3 @@ fn benchmark_convert_output(c: &mut Criterion) {
})
});
}

criterion_group!(benches, benchmark_convert_output);
criterion_main!(benches);
11 changes: 2 additions & 9 deletions benches/request_template_bench.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use criterion::{black_box, Criterion};
use derive_setters::Setters;
use hyper::HeaderMap;
use serde_json::json;
Expand Down Expand Up @@ -30,7 +30,7 @@ impl HasHeaders for Context {
&self.headers
}
}
fn benchmark_to_request(c: &mut Criterion) {
pub fn benchmark_to_request(c: &mut Criterion) {
let tmpl_mustache = RequestTemplate::try_from(Endpoint::new(
"http://localhost:3000/{{args.b}}?a={{args.a}}&b={{args.b}}&c={{args.c}}".to_string(),
))
Expand Down Expand Up @@ -59,10 +59,3 @@ fn benchmark_to_request(c: &mut Criterion) {
})
});
}

criterion_group! {
name = benches;
config = Criterion::default();
targets = benchmark_to_request
}
criterion_main!(benches);
23 changes: 23 additions & 0 deletions benches/tailcall_benches.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use criterion::{criterion_group, criterion_main, Criterion};

mod data_loader_bench;
mod impl_path_string_for_evaluation_context;
mod json_like_bench;
mod protobuf_convert_output;
mod request_template_bench;

fn all_benchmarks(c: &mut Criterion) {
data_loader_bench::benchmark_data_loader(c);
impl_path_string_for_evaluation_context::bench_main(c);
json_like_bench::benchmark_batched_body(c);
json_like_bench::benchmark_group_by(c);
protobuf_convert_output::benchmark_convert_output(c);
request_template_bench::benchmark_to_request(c);
}

criterion_group! {
name = benches;
config = Criterion::default();
targets = all_benchmarks
}
criterion_main!(benches);

1 comment on commit 276a612

@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.60ms 3.94ms 181.31ms 77.18%
Req/Sec 3.35k 276.65 3.94k 85.83%

400116 requests in 30.02s, 2.01GB read

Requests/sec: 13330.44

Transfer/sec: 68.42MB

Please sign in to comment.