From 276a612d3784dcb928a37712f0b500249097e2fc Mon Sep 17 00:00:00 2001 From: Biswarghya Biswas Date: Thu, 2 May 2024 20:15:15 +0530 Subject: [PATCH] refactor: merge benchmarks in `tailcall_benches.rs` (#1834) --- .devcontainer/devcontainer.json | 5 +++- Cargo.toml | 21 ++--------------- benches/data_loader_bench.rs | 11 ++------- ...impl_path_string_for_evaluation_context.rs | 7 ++---- benches/json_like_bench.rs | 9 +++----- benches/protobuf_convert_output.rs | 7 ++---- benches/request_template_bench.rs | 11 ++------- benches/tailcall_benches.rs | 23 +++++++++++++++++++ 8 files changed, 40 insertions(+), 54 deletions(-) create mode 100644 benches/tailcall_benches.rs diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index d8641749b5..bb8c490e0d 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -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" + } } } diff --git a/Cargo.toml b/Cargo.toml index 28a2df8861..c35564aaaf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 \ No newline at end of file diff --git a/benches/data_loader_bench.rs b/benches/data_loader_bench.rs index bc7398ba53..59b47a49e9 100644 --- a/benches/data_loader_bench.rs +++ b/benches/data_loader_bench.rs @@ -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; @@ -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)) }); @@ -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); diff --git a/benches/impl_path_string_for_evaluation_context.rs b/benches/impl_path_string_for_evaluation_context.rs index d6c0669cc0..fbd622f03e 100644 --- a/benches/impl_path_string_for_evaluation_context.rs +++ b/benches/impl_path_string_for_evaluation_context.rs @@ -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; @@ -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(); @@ -271,6 +271,3 @@ fn bench_main(c: &mut Criterion) { }); } } - -criterion_group!(benches, bench_main); -criterion_main!(benches); diff --git a/benches/json_like_bench.rs b/benches/json_like_bench.rs index 945fcd8ba5..85ec1916b5 100644 --- a/benches/json_like_bench.rs +++ b/benches/json_like_bench.rs @@ -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!({ @@ -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"}}, @@ -47,6 +47,3 @@ fn benchmark_group_by(c: &mut Criterion) { }) }); } - -criterion_group!(benches, benchmark_batched_body, benchmark_group_by); -criterion_main!(benches); diff --git a/benches/protobuf_convert_output.rs b/benches/protobuf_convert_output.rs index ff40f53978..ae8bc13739 100644 --- a/benches/protobuf_convert_output.rs +++ b/benches/protobuf_convert_output.rs @@ -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; @@ -42,7 +42,7 @@ fn create_dummy_value(n: usize, m: usize) -> Result { 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(); @@ -65,6 +65,3 @@ fn benchmark_convert_output(c: &mut Criterion) { }) }); } - -criterion_group!(benches, benchmark_convert_output); -criterion_main!(benches); diff --git a/benches/request_template_bench.rs b/benches/request_template_bench.rs index bc67c1486f..4ab3d94694 100644 --- a/benches/request_template_bench.rs +++ b/benches/request_template_bench.rs @@ -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; @@ -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(), )) @@ -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); diff --git a/benches/tailcall_benches.rs b/benches/tailcall_benches.rs new file mode 100644 index 0000000000..d4f0a02e0d --- /dev/null +++ b/benches/tailcall_benches.rs @@ -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);