From 9ba23b4cd3c29b4da1a553494fc7a33bb12dff1b Mon Sep 17 00:00:00 2001 From: Shashi Kant Date: Thu, 9 May 2024 09:07:01 +0000 Subject: [PATCH] chore: adding benchmark for `handle_request` (#1886) Co-authored-by: Tushar Mathur --- benches/handle_request_bench.rs | 40 +++++++++++++++++++++++++++++++++ benches/tailcall_benches.rs | 2 ++ 2 files changed, 42 insertions(+) create mode 100644 benches/handle_request_bench.rs diff --git a/benches/handle_request_bench.rs b/benches/handle_request_bench.rs new file mode 100644 index 0000000000..612931d1f0 --- /dev/null +++ b/benches/handle_request_bench.rs @@ -0,0 +1,40 @@ +use std::sync::Arc; + +use criterion::Criterion; +use hyper::Request; +use tailcall::cli::server::server_config::ServerConfig; +use tailcall::{handle_request, Blueprint, Config, ConfigModule, GraphQLRequest, Validator}; + +static QUERY: &str = r#"{"query":"query{posts{title}}"}"#; + +pub fn benchmark_handle_request(c: &mut Criterion) { + let tokio_runtime = tokio::runtime::Runtime::new().unwrap(); + let sdl = std::fs::read_to_string("./ci-benchmark/benchmark.graphql").unwrap(); + let config_module: ConfigModule = Config::from_sdl(sdl.as_str()).to_result().unwrap().into(); + + let blueprint = Blueprint::try_from(&config_module).unwrap(); + let endpoints = config_module.extensions.endpoint_set; + + let server_config = tokio_runtime + .block_on(ServerConfig::new(blueprint, endpoints)) + .unwrap(); + let server_config = Arc::new(server_config); + + c.bench_function("test_handle_request", |b| { + let server_config = server_config.clone(); + b.iter(|| { + let server_config = server_config.clone(); + tokio_runtime.block_on(async move { + let req = Request::builder() + .method("POST") + .uri("http://localhost:8000/graphql") + .body(hyper::Body::from(QUERY)) + .unwrap(); + + let _ = handle_request::(req, server_config.app_ctx.clone()) + .await + .unwrap(); + }); + }) + }); +} diff --git a/benches/tailcall_benches.rs b/benches/tailcall_benches.rs index d4f0a02e0d..4bd032fd56 100644 --- a/benches/tailcall_benches.rs +++ b/benches/tailcall_benches.rs @@ -1,6 +1,7 @@ use criterion::{criterion_group, criterion_main, Criterion}; mod data_loader_bench; +mod handle_request_bench; mod impl_path_string_for_evaluation_context; mod json_like_bench; mod protobuf_convert_output; @@ -13,6 +14,7 @@ fn all_benchmarks(c: &mut Criterion) { json_like_bench::benchmark_group_by(c); protobuf_convert_output::benchmark_convert_output(c); request_template_bench::benchmark_to_request(c); + handle_request_bench::benchmark_handle_request(c); } criterion_group! {