diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8537414d8..92086f52f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -100,7 +100,8 @@ jobs: toolchain: stable components: clippy - - run: cargo clippy -p juniper_benchmarks --benches -- -D warnings + - run: cargo clippy -p juniper_benchmarks --benches --tests -- -D warnings + - run: cargo test -p juniper_benchmarks - run: cargo bench -p juniper_benchmarks feature: diff --git a/benches/Cargo.toml b/benches/Cargo.toml index fb9e0e1a9..6a3c16700 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -5,12 +5,20 @@ edition = "2024" authors = ["Christoph Herzog "] publish = false +[lib] +# Keeps the `libtest` harness from rejecting Criterion arguments in `cargo bench`. +bench = false + [dependencies] +apollo-compiler = "1.33" +async-graphql = { version = "7.2", default-features = false } +futures = "0.3" juniper = { path = "../juniper" } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" [dev-dependencies] criterion = "0.8" -tokio = { version = "1.0", features = ["rt-multi-thread"] } [lints.clippy] allow_attributes = "warn" @@ -24,5 +32,5 @@ unsafe_code = "forbid" unused_crate_dependencies = "warn" [[bench]] -name = "benchmark" +name = "comparison" harness = false diff --git a/benches/benches/benchmark.rs b/benches/benches/benchmark.rs deleted file mode 100644 index 8cb371538..000000000 --- a/benches/benches/benchmark.rs +++ /dev/null @@ -1,80 +0,0 @@ -use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; - -use juniper::InputValue; -use juniper_benchmarks as j; - -fn bench_sync_vs_async_users_flat_instant(c: &mut Criterion) { - // language=GraphQL - const ASYNC_QUERY: &str = r#" - query Query($id: Int) { - users_async_instant(ids: [$id]!) { - id - kind - username - email - } - } - "#; - - // language=GraphQL - const SYNC_QUERY: &str = r#" - query Query($id: Int) { - users_sync_instant(ids: [$id]!) { - id - kind - username - email - } - } - "#; - - let mut group = c.benchmark_group("Sync vs Async - Users Flat - Instant"); - for count in [1, 10] { - group.bench_function(BenchmarkId::new("Sync", count), |b| { - let ids = (0..count).map(InputValue::scalar).collect::>(); - let ids = InputValue::list(ids); - b.iter(|| { - j::execute_sync( - SYNC_QUERY, - vec![("ids".to_owned(), ids.clone())].into_iter().collect(), - ) - }) - }); - - group.bench_function(BenchmarkId::new("Async - Single Thread", count), |b| { - let rt = tokio::runtime::Builder::new_current_thread() - .build() - .unwrap(); - - let ids = (0..count).map(InputValue::scalar).collect::>(); - let ids = InputValue::list(ids); - - b.iter(|| { - let f = j::execute( - ASYNC_QUERY, - vec![("ids".to_owned(), ids.clone())].into_iter().collect(), - ); - rt.block_on(f) - }) - }); - - group.bench_function(BenchmarkId::new("Async - Threadpool", count), |b| { - let rt = tokio::runtime::Builder::new_multi_thread().build().unwrap(); - - let ids = (0..count).map(InputValue::scalar).collect::>(); - let ids = InputValue::list(ids); - - b.iter(|| { - let f = j::execute( - ASYNC_QUERY, - vec![("ids".to_owned(), ids.clone())].into_iter().collect(), - ); - rt.block_on(f) - }) - }); - } - group.finish(); -} - -criterion_group!(benches, bench_sync_vs_async_users_flat_instant); -criterion_main!(benches); diff --git a/benches/benches/comparison.rs b/benches/benches/comparison.rs new file mode 100644 index 000000000..bdf4d81be --- /dev/null +++ b/benches/benches/comparison.rs @@ -0,0 +1,45 @@ +//! Compares the engines on every operation in [`operation::ALL`]. + +#![allow( + unused_crate_dependencies, + reason = "the library uses the other dependencies" +)] + +use criterion::{ + BenchmarkGroup, Criterion, criterion_group, criterion_main, measurement::WallTime, +}; +use juniper_benchmarks::{ + engine::{ + Engine, + apollo_compiler::ApolloCompiler, + async_graphql::AsyncGraphql, + juniper::{JuniperAsync, JuniperAsyncFn, JuniperSync}, + }, + operation::{self, Operation}, +}; + +fn compare(c: &mut Criterion) { + for operation in operation::ALL { + operation.check(); + let request = operation.request(); + let mut group = c.benchmark_group(operation.name); + bench::(&mut group, operation, &request); + bench::(&mut group, operation, &request); + bench::(&mut group, operation, &request); + bench::(&mut group, operation, &request); + bench::(&mut group, operation, &request); + group.finish(); + } +} + +/// Measures the `E` engine on the `operation`, if the engine supports the operation. +fn bench(group: &mut BenchmarkGroup<'_, WallTime>, operation: Operation, request: &str) { + if !operation.runs_on::() { + return; + } + let engine = E::default(); + group.bench_function(E::NAME, |b| b.iter(|| engine.execute(request))); +} + +criterion_group!(benches, compare); +criterion_main!(benches); diff --git a/benches/src/data.rs b/benches/src/data.rs new file mode 100644 index 000000000..9f2d20418 --- /dev/null +++ b/benches/src/data.rs @@ -0,0 +1,200 @@ +//! Data served by every engine. +//! +//! All data is `'static`. Resolvers only read fields and follow references, so the benchmarks +//! measure the engines and not the data access. + +use std::sync::LazyLock; + +/// Number of users in [`users()`]. +pub const USER_COUNT: usize = 1_000; + +/// User of a social network. +#[derive(Debug)] +pub struct User { + pub id: i32, + pub name: String, + pub email: String, + pub active: bool, + pub score: f64, + pub role: Role, +} + +impl User { + fn new(id: i32) -> Self { + let roles = [Role::Admin, Role::Moderator, Role::Member, Role::Guest]; + Self { + id, + name: format!("User {id}"), + email: format!("user{id}@example.com"), + active: id % 2 == 0, + score: f64::from(id) / 2.0, + role: roles[id.unsigned_abs() as usize % roles.len()], + } + } + + /// Returns up to `count` users that follow this one in [`users()`]. + /// + /// The sequence wraps around to the start of [`users()`]. A negative `count` returns no users. + pub fn friends(&self, count: i32) -> impl Iterator { + let all = users(); + let start = self.id.unsigned_abs() as usize + 1; + let count = usize::try_from(count).unwrap_or_default(); + (start..start + count).map(move |index| &all[index % all.len()]) + } +} + +/// Access level of a [`User`]. +#[derive(Clone, Copy, Debug, Eq, PartialEq, async_graphql::Enum, juniper::GraphQLEnum)] +pub enum Role { + Admin, + Moderator, + Member, + Guest, +} + +/// Returns all users, ordered by ID from `0`. +pub fn users() -> &'static [User] { + static USERS: LazyLock> = LazyLock::new(|| { + let count = i32::try_from(USER_COUNT).expect("`USER_COUNT` fits in `i32`"); + (0..count).map(User::new).collect() + }); + &USERS +} + +/// Returns up to `count` first users. A negative `count` returns no users. +pub fn first_users(count: i32) -> impl Iterator { + users() + .iter() + .take(usize::try_from(count).unwrap_or_default()) +} + +/// Film of the original Star Wars trilogy. +#[derive(Clone, Copy, Debug, Eq, PartialEq, async_graphql::Enum, juniper::GraphQLEnum)] +pub enum Episode { + NewHope, + Empire, + Jedi, +} + +/// Character of the Star Wars films. +#[derive(Clone, Copy, Debug)] +pub enum Character { + Human(&'static Human), + Droid(&'static Droid), +} + +/// Human character of the Star Wars films. +#[derive(Debug)] +pub struct Human { + pub id: &'static str, + pub name: &'static str, + pub friends: &'static [Character], + pub appears_in: &'static [Episode], + pub home_planet: Option<&'static str>, +} + +/// Droid character of the Star Wars films. +#[derive(Debug)] +pub struct Droid { + pub id: &'static str, + pub name: &'static str, + pub friends: &'static [Character], + pub appears_in: &'static [Episode], + pub primary_function: &'static str, +} + +/// Human characters in the order of their IDs. +pub static HUMANS: [&Human; 5] = [&LUKE, &VADER, &HAN, &LEIA, &TARKIN]; + +/// Droid characters in the order of their IDs. +pub static DROIDS: [&Droid; 2] = [&THREEPIO, &ARTOO]; + +/// Returns the hero of `episode`, or the hero of the whole saga if `episode` is [`None`]. +pub fn hero(episode: Option) -> Character { + if episode == Some(Episode::Empire) { + Character::Human(&LUKE) + } else { + Character::Droid(&ARTOO) + } +} + +const ALL_EPISODES: &[Episode] = &[Episode::NewHope, Episode::Empire, Episode::Jedi]; + +static LUKE: Human = Human { + id: "1000", + name: "Luke Skywalker", + friends: &[ + Character::Human(&HAN), + Character::Human(&LEIA), + Character::Droid(&THREEPIO), + Character::Droid(&ARTOO), + ], + appears_in: ALL_EPISODES, + home_planet: Some("Tatooine"), +}; + +static VADER: Human = Human { + id: "1001", + name: "Darth Vader", + friends: &[Character::Human(&TARKIN)], + appears_in: ALL_EPISODES, + home_planet: Some("Tatooine"), +}; + +static HAN: Human = Human { + id: "1002", + name: "Han Solo", + friends: &[ + Character::Human(&LUKE), + Character::Human(&LEIA), + Character::Droid(&ARTOO), + ], + appears_in: ALL_EPISODES, + home_planet: None, +}; + +static LEIA: Human = Human { + id: "1003", + name: "Leia Organa", + friends: &[ + Character::Human(&LUKE), + Character::Human(&HAN), + Character::Droid(&THREEPIO), + Character::Droid(&ARTOO), + ], + appears_in: ALL_EPISODES, + home_planet: Some("Alderaan"), +}; + +static TARKIN: Human = Human { + id: "1004", + name: "Wilhuff Tarkin", + friends: &[Character::Human(&VADER)], + appears_in: &[Episode::NewHope], + home_planet: None, +}; + +static THREEPIO: Droid = Droid { + id: "2000", + name: "C-3PO", + friends: &[ + Character::Human(&LUKE), + Character::Human(&HAN), + Character::Human(&LEIA), + Character::Droid(&ARTOO), + ], + appears_in: ALL_EPISODES, + primary_function: "Protocol", +}; + +static ARTOO: Droid = Droid { + id: "2001", + name: "R2-D2", + friends: &[ + Character::Human(&LUKE), + Character::Human(&HAN), + Character::Human(&LEIA), + ], + appears_in: ALL_EPISODES, + primary_function: "Astromech", +}; diff --git a/benches/src/engine/apollo_compiler/mod.rs b/benches/src/engine/apollo_compiler/mod.rs new file mode 100644 index 000000000..5d5315f59 --- /dev/null +++ b/benches/src/engine/apollo_compiler/mod.rs @@ -0,0 +1,249 @@ +//! [`apollo_compiler`] engine. +//! +//! `apollo-compiler` defines a schema with SDL and resolves fields through hand-written +//! [`ObjectValue`] implementations. + +use apollo_compiler::{ + ExecutableDocument, Schema, + resolvers::{Execution, FieldError, ObjectValue, ResolveInfo, ResolvedValue}, + response::{JsonMap, JsonValue}, + validation::Valid, +}; +use serde::Deserialize; + +use super::Engine; +use crate::data::{self, Episode, Role}; + +/// [`apollo_compiler`] engine with synchronous resolvers. +/// +/// Each request parses and validates the document, and executes it with +/// [`Execution::execute_sync()`]. +pub struct ApolloCompiler { + schema: Valid, +} + +impl Default for ApolloCompiler { + fn default() -> Self { + Self { + schema: Schema::parse_and_validate(include_str!("schema.graphql"), "schema.graphql") + .expect("valid schema"), + } + } +} + +impl Engine for ApolloCompiler { + const NAME: &'static str = "apollo_compiler"; + const SUPPORTS_IS_ONE_OF: bool = false; + + fn execute(&self, request: &str) -> String { + let request: Request = serde_json::from_str(request).expect("valid request"); + let document = match ExecutableDocument::parse_and_validate( + &self.schema, + request.query, + "request.graphql", + ) { + Ok(document) => document, + Err(invalid) => return error_response(invalid.errors), + }; + let response = Execution::new(&self.schema, &document) + .operation_name(request.operation_name.as_deref()) + .and_then(|execution| { + execution + .raw_variable_values(&request.variables) + .enable_schema_introspection(true) + .execute_sync(&Query) + }); + match response { + Ok(response) => serde_json::to_string(&response).expect("response serializes to JSON"), + Err(error) => error_response(error.message()), + } + } +} + +/// GraphQL-over-HTTP request body. +#[derive(Deserialize)] +struct Request { + query: String, + + #[serde(rename = "operationName")] + operation_name: Option, + + #[serde(default)] + variables: JsonMap, +} + +/// Returns the JSON body of a response with the `error` message. +fn error_response(error: impl std::fmt::Display) -> String { + serde_json::json!({ "errors": [{ "message": error.to_string() }] }).to_string() +} + +/// Root query type. +struct Query; + +impl ObjectValue for Query { + fn type_name(&self) -> &str { + "Query" + } + + fn resolve_field<'a>( + &'a self, + info: &'a ResolveInfo<'a>, + ) -> Result, FieldError> { + match info.field_name() { + "hello" => Ok(ResolvedValue::leaf("world")), + "users" => Ok(ResolvedValue::list( + data::first_users(int_argument(info, "first")) + .map(|user| ResolvedValue::object(User(user))), + )), + "hero" => { + let episode = info + .arguments() + .get("episode") + .and_then(JsonValue::as_str) + .map(episode_from_name); + Ok(character(data::hero(episode))) + } + "humans" => Ok(ResolvedValue::list( + data::HUMANS + .into_iter() + .map(|human| ResolvedValue::object(Human(human))), + )), + "droids" => Ok(ResolvedValue::list( + data::DROIDS + .into_iter() + .map(|droid| ResolvedValue::object(Droid(droid))), + )), + _ => Err(self.unknown_field_error(info)), + } + } +} + +struct User(&'static data::User); + +impl ObjectValue for User { + fn type_name(&self) -> &str { + "User" + } + + fn resolve_field<'a>( + &'a self, + info: &'a ResolveInfo<'a>, + ) -> Result, FieldError> { + match info.field_name() { + "id" => Ok(ResolvedValue::leaf(self.0.id)), + "name" => Ok(ResolvedValue::leaf(self.0.name.as_str())), + "email" => Ok(ResolvedValue::leaf(self.0.email.as_str())), + "active" => Ok(ResolvedValue::leaf(self.0.active)), + "score" => Ok(ResolvedValue::leaf(self.0.score)), + "role" => Ok(ResolvedValue::leaf(role_name(self.0.role))), + "friends" => Ok(ResolvedValue::list( + self.0 + .friends(int_argument(info, "first")) + .map(|user| ResolvedValue::object(User(user))), + )), + _ => Err(self.unknown_field_error(info)), + } + } +} + +struct Human(&'static data::Human); + +impl ObjectValue for Human { + fn type_name(&self) -> &str { + "Human" + } + + fn resolve_field<'a>( + &'a self, + info: &'a ResolveInfo<'a>, + ) -> Result, FieldError> { + match info.field_name() { + "id" => Ok(ResolvedValue::leaf(self.0.id)), + "name" => Ok(ResolvedValue::leaf(self.0.name)), + "friends" => Ok(ResolvedValue::list( + self.0.friends.iter().copied().map(character), + )), + "appearsIn" => Ok(episodes(self.0.appears_in)), + "homePlanet" => Ok(ResolvedValue::leaf( + self.0.home_planet.map_or(JsonValue::Null, JsonValue::from), + )), + _ => Err(self.unknown_field_error(info)), + } + } +} + +struct Droid(&'static data::Droid); + +impl ObjectValue for Droid { + fn type_name(&self) -> &str { + "Droid" + } + + fn resolve_field<'a>( + &'a self, + info: &'a ResolveInfo<'a>, + ) -> Result, FieldError> { + match info.field_name() { + "id" => Ok(ResolvedValue::leaf(self.0.id)), + "name" => Ok(ResolvedValue::leaf(self.0.name)), + "friends" => Ok(ResolvedValue::list( + self.0.friends.iter().copied().map(character), + )), + "appearsIn" => Ok(episodes(self.0.appears_in)), + "primaryFunction" => Ok(ResolvedValue::leaf(self.0.primary_function)), + _ => Err(self.unknown_field_error(info)), + } + } +} + +/// Returns the value of a `character` for a field of the `Character` interface type. +fn character<'a>(character: data::Character) -> ResolvedValue<'a> { + match character { + data::Character::Human(human) => ResolvedValue::object(Human(human)), + data::Character::Droid(droid) => ResolvedValue::object(Droid(droid)), + } +} + +/// Returns a list value of the GraphQL names of the `episodes`. +fn episodes<'a>(episodes: &'static [Episode]) -> ResolvedValue<'a> { + ResolvedValue::list( + episodes + .iter() + .map(|episode| ResolvedValue::leaf(episode_name(*episode))), + ) +} + +/// Returns the `Int!` argument with the `name`. Validation guarantees its presence. +fn int_argument(info: &ResolveInfo<'_>, name: &str) -> i32 { + info.arguments() + .get(name) + .and_then(JsonValue::as_i64) + .and_then(|value| i32::try_from(value).ok()) + .expect("validated `Int!` argument") +} + +fn role_name(role: Role) -> &'static str { + match role { + Role::Admin => "ADMIN", + Role::Moderator => "MODERATOR", + Role::Member => "MEMBER", + Role::Guest => "GUEST", + } +} + +fn episode_name(episode: Episode) -> &'static str { + match episode { + Episode::NewHope => "NEW_HOPE", + Episode::Empire => "EMPIRE", + Episode::Jedi => "JEDI", + } +} + +fn episode_from_name(name: &str) -> Episode { + match name { + "NEW_HOPE" => Episode::NewHope, + "EMPIRE" => Episode::Empire, + "JEDI" => Episode::Jedi, + _ => unreachable!("validation rejects unknown `Episode` values"), + } +} diff --git a/benches/src/engine/apollo_compiler/schema.graphql b/benches/src/engine/apollo_compiler/schema.graphql new file mode 100644 index 000000000..e3fc5922a --- /dev/null +++ b/benches/src/engine/apollo_compiler/schema.graphql @@ -0,0 +1,53 @@ +type Query { + hello: String! + users(first: Int!): [User!]! + hero(episode: Episode): Character! + humans: [Human!]! + droids: [Droid!]! +} + +type User { + id: Int! + name: String! + email: String! + active: Boolean! + score: Float! + role: Role! + friends(first: Int!): [User!]! +} + +enum Role { + ADMIN + MODERATOR + MEMBER + GUEST +} + +interface Character { + id: String! + name: String! + friends: [Character!]! + appearsIn: [Episode!]! +} + +type Human implements Character { + id: String! + name: String! + friends: [Character!]! + appearsIn: [Episode!]! + homePlanet: String +} + +type Droid implements Character { + id: String! + name: String! + friends: [Character!]! + appearsIn: [Episode!]! + primaryFunction: String! +} + +enum Episode { + NEW_HOPE + EMPIRE + JEDI +} diff --git a/benches/src/engine/async_graphql.rs b/benches/src/engine/async_graphql.rs new file mode 100644 index 000000000..52e36ec2b --- /dev/null +++ b/benches/src/engine/async_graphql.rs @@ -0,0 +1,164 @@ +//! [`async_graphql`] engine. + +use async_graphql::{EmptyMutation, EmptySubscription, Interface, Object, Request, Schema}; +use futures::executor::block_on; + +use super::Engine; +use crate::data::{self, Episode, Role}; + +/// [`async_graphql`] engine. +pub struct AsyncGraphql { + schema: Schema, +} + +impl Default for AsyncGraphql { + fn default() -> Self { + Self { + schema: Schema::new(Query, EmptyMutation, EmptySubscription), + } + } +} + +impl Engine for AsyncGraphql { + const NAME: &'static str = "async_graphql"; + + fn execute(&self, request: &str) -> String { + let request: Request = serde_json::from_str(request).expect("valid request"); + let response = block_on(self.schema.execute(request)); + serde_json::to_string(&response).expect("response serializes to JSON") + } +} + +/// Root query type. +pub struct Query; + +#[Object] +impl Query { + async fn hello(&self) -> &str { + "world" + } + + async fn users(&self, first: i32) -> Vec { + data::first_users(first).map(User).collect() + } + + async fn hero(&self, episode: Option) -> Character { + data::hero(episode).into() + } + + async fn humans(&self) -> Vec { + data::HUMANS.into_iter().map(Human).collect() + } + + async fn droids(&self) -> Vec { + data::DROIDS.into_iter().map(Droid).collect() + } +} + +struct User(&'static data::User); + +#[Object] +impl User { + async fn id(&self) -> i32 { + self.0.id + } + + async fn name(&self) -> &str { + &self.0.name + } + + async fn email(&self) -> &str { + &self.0.email + } + + async fn active(&self) -> bool { + self.0.active + } + + async fn score(&self) -> f64 { + self.0.score + } + + async fn role(&self) -> Role { + self.0.role + } + + async fn friends(&self, first: i32) -> Vec { + self.0.friends(first).map(User).collect() + } +} + +#[derive(Interface)] +#[expect( + clippy::duplicated_attributes, + reason = "fields of the same type repeat `ty`" +)] +#[graphql( + field(name = "id", ty = "&str"), + field(name = "name", ty = "&str"), + field(name = "friends", ty = "Vec"), + field(name = "appears_in", ty = "&[Episode]") +)] +enum Character { + Human(Human), + Droid(Droid), +} + +impl From for Character { + fn from(character: data::Character) -> Self { + match character { + data::Character::Human(human) => Self::Human(Human(human)), + data::Character::Droid(droid) => Self::Droid(Droid(droid)), + } + } +} + +struct Human(&'static data::Human); + +#[Object] +impl Human { + async fn id(&self) -> &str { + self.0.id + } + + async fn name(&self) -> &str { + self.0.name + } + + async fn friends(&self) -> Vec { + self.0.friends.iter().copied().map(Into::into).collect() + } + + async fn appears_in(&self) -> &[Episode] { + self.0.appears_in + } + + async fn home_planet(&self) -> Option<&str> { + self.0.home_planet + } +} + +struct Droid(&'static data::Droid); + +#[Object] +impl Droid { + async fn id(&self) -> &str { + self.0.id + } + + async fn name(&self) -> &str { + self.0.name + } + + async fn friends(&self) -> Vec { + self.0.friends.iter().copied().map(Into::into).collect() + } + + async fn appears_in(&self) -> &[Episode] { + self.0.appears_in + } + + async fn primary_function(&self) -> &str { + self.0.primary_function + } +} diff --git a/benches/src/engine/juniper/async_resolvers.rs b/benches/src/engine/juniper/async_resolvers.rs new file mode 100644 index 000000000..4d3d3b46f --- /dev/null +++ b/benches/src/engine/juniper/async_resolvers.rs @@ -0,0 +1,3 @@ +//! Benchmark schema with `async fn` resolvers on all fields. + +super::schema!(async); diff --git a/benches/src/engine/juniper/mod.rs b/benches/src/engine/juniper/mod.rs new file mode 100644 index 000000000..bd91569a8 --- /dev/null +++ b/benches/src/engine/juniper/mod.rs @@ -0,0 +1,236 @@ +//! [`juniper`] engines. +//! +//! The engines serve the same schema with two kinds of resolvers. The [`sync_resolvers`] schema +//! has synchronous resolvers. Synchronous resolvers are the usual style for `juniper`. The +//! [`async_resolvers`] schema has `async fn` resolvers on all fields. `async-graphql` supports +//! only this style. + +pub mod async_resolvers; +pub mod sync_resolvers; + +use futures::executor::block_on; +use juniper::{EmptyMutation, EmptySubscription, GraphQLTypeAsync, RootNode, http::GraphQLRequest}; + +use super::Engine; + +/// [`juniper`] engine with the [`sync_resolvers`] schema. +/// +/// Executes requests with [`GraphQLRequest::execute()`] if `ASYNC` is `true`, and with +/// [`GraphQLRequest::execute_sync()`] otherwise. +pub struct Juniper { + schema: Schema, +} + +/// [`Juniper`] engine with asynchronous execution. +pub type JuniperAsync = Juniper; + +/// [`Juniper`] engine with synchronous execution. +pub type JuniperSync = Juniper; + +impl Default for Juniper { + fn default() -> Self { + Self { + schema: Schema::new( + sync_resolvers::Query, + EmptyMutation::new(), + EmptySubscription::new(), + ), + } + } +} + +impl Engine for Juniper { + const NAME: &'static str = if ASYNC { "juniper" } else { "juniper_sync" }; + + fn execute(&self, request: &str) -> String { + if ASYNC { + execute_async(&self.schema, request) + } else { + let request: GraphQLRequest = serde_json::from_str(request).expect("valid request"); + let response = request.execute_sync(&self.schema, &()); + serde_json::to_string(&response).expect("response serializes to JSON") + } + } +} + +/// [`juniper`] engine with the [`async_resolvers`] schema and asynchronous execution. +pub struct JuniperAsyncFn { + schema: Schema, +} + +impl Default for JuniperAsyncFn { + fn default() -> Self { + Self { + schema: Schema::new( + async_resolvers::Query, + EmptyMutation::new(), + EmptySubscription::new(), + ), + } + } +} + +impl Engine for JuniperAsyncFn { + const NAME: &'static str = "juniper_async_fn"; + + fn execute(&self, request: &str) -> String { + execute_async(&self.schema, request) + } +} + +type Schema = RootNode; + +/// Executes the JSON `request` body with [`GraphQLRequest::execute()`] and returns the JSON +/// response body. +fn execute_async(schema: &Schema, request: &str) -> String +where + Query: GraphQLTypeAsync, +{ + let request: GraphQLRequest = serde_json::from_str(request).expect("valid request"); + let response = block_on(request.execute(schema, &())); + serde_json::to_string(&response).expect("response serializes to JSON") +} + +/// Defines the types of the benchmark schema in the calling module. +/// +/// The `async` argument makes all resolvers `async fn`. One definition keeps the schemas with +/// synchronous and asynchronous resolvers the same. +macro_rules! schema { + ($($asyncness:tt)?) => { + use juniper::{graphql_interface, graphql_object}; + + use crate::data::{self, Episode, Role}; + + /// Root query type. + pub struct Query; + + #[graphql_object] + impl Query { + $($asyncness)? fn hello() -> &'static str { + "world" + } + + $($asyncness)? fn users(first: i32) -> Vec { + data::first_users(first).map(User).collect() + } + + $($asyncness)? fn hero(episode: Option) -> CharacterValue { + data::hero(episode).into() + } + + $($asyncness)? fn humans() -> Vec { + data::HUMANS.into_iter().map(Human).collect() + } + + $($asyncness)? fn droids() -> Vec { + data::DROIDS.into_iter().map(Droid).collect() + } + } + + struct User(&'static data::User); + + #[graphql_object] + impl User { + $($asyncness)? fn id(&self) -> i32 { + self.0.id + } + + $($asyncness)? fn name(&self) -> &str { + &self.0.name + } + + $($asyncness)? fn email(&self) -> &str { + &self.0.email + } + + $($asyncness)? fn active(&self) -> bool { + self.0.active + } + + $($asyncness)? fn score(&self) -> f64 { + self.0.score + } + + $($asyncness)? fn role(&self) -> Role { + self.0.role + } + + $($asyncness)? fn friends(&self, first: i32) -> Vec { + self.0.friends(first).map(User).collect() + } + } + + #[graphql_interface(for = [Human, Droid])] + #[expect(dead_code, reason = "GraphQL schema definition")] + trait Character { + $($asyncness)? fn id(&self) -> &str; + + $($asyncness)? fn name(&self) -> &str; + + $($asyncness)? fn friends(&self) -> Vec; + + $($asyncness)? fn appears_in(&self) -> &[Episode]; + } + + impl From for CharacterValue { + fn from(character: data::Character) -> Self { + match character { + data::Character::Human(human) => Human(human).into(), + data::Character::Droid(droid) => Droid(droid).into(), + } + } + } + + struct Human(&'static data::Human); + + #[graphql_object(impl = CharacterValue)] + impl Human { + $($asyncness)? fn id(&self) -> &str { + self.0.id + } + + $($asyncness)? fn name(&self) -> &str { + self.0.name + } + + $($asyncness)? fn friends(&self) -> Vec { + self.0.friends.iter().copied().map(Into::into).collect() + } + + $($asyncness)? fn appears_in(&self) -> &[Episode] { + self.0.appears_in + } + + $($asyncness)? fn home_planet(&self) -> Option<&str> { + self.0.home_planet + } + } + + struct Droid(&'static data::Droid); + + #[graphql_object(impl = CharacterValue)] + impl Droid { + $($asyncness)? fn id(&self) -> &str { + self.0.id + } + + $($asyncness)? fn name(&self) -> &str { + self.0.name + } + + $($asyncness)? fn friends(&self) -> Vec { + self.0.friends.iter().copied().map(Into::into).collect() + } + + $($asyncness)? fn appears_in(&self) -> &[Episode] { + self.0.appears_in + } + + $($asyncness)? fn primary_function(&self) -> &str { + self.0.primary_function + } + } + }; +} + +use schema; diff --git a/benches/src/engine/juniper/sync_resolvers.rs b/benches/src/engine/juniper/sync_resolvers.rs new file mode 100644 index 000000000..c94886ca7 --- /dev/null +++ b/benches/src/engine/juniper/sync_resolvers.rs @@ -0,0 +1,3 @@ +//! Benchmark schema with synchronous resolvers. + +super::schema!(); diff --git a/benches/src/engine/mod.rs b/benches/src/engine/mod.rs new file mode 100644 index 000000000..29048f3ca --- /dev/null +++ b/benches/src/engine/mod.rs @@ -0,0 +1,39 @@ +//! GraphQL engines under benchmark. + +pub mod apollo_compiler; +pub mod async_graphql; +pub mod juniper; + +/// GraphQL engine that serves the schema of this crate. +pub trait Engine: Default { + /// Name of the engine in benchmark IDs. + const NAME: &'static str; + + /// The engine supports the `isOneOf` field of `__Type`. + /// + /// The September 2025 GraphQL specification added this field. + const SUPPORTS_IS_ONE_OF: bool = true; + + /// Executes a GraphQL-over-HTTP JSON `request` body and returns the JSON response body. + /// + /// # Panics + /// + /// If `request` is not a valid request body. + fn execute(&self, request: &str) -> String; + + /// Executes `request` with [`Engine::execute()`] and parses the response. + /// + /// # Panics + /// + /// If `request` is not a valid request body, or if the response contains errors. + fn response(&self, request: &str) -> serde_json::Value { + let response: serde_json::Value = + serde_json::from_str(&self.execute(request)).expect("response is valid JSON"); + assert!( + response.get("errors").is_none(), + "`{}` returned errors: {response}", + Self::NAME, + ); + response + } +} diff --git a/benches/src/lib.rs b/benches/src/lib.rs index 1426bcb84..e743f822b 100644 --- a/benches/src/lib.rs +++ b/benches/src/lib.rs @@ -1,110 +1,16 @@ -#![cfg_attr(test, allow(unused_crate_dependencies, reason = "benches"))] - -use juniper::{ - DefaultScalarValue, EmptyMutation, EmptySubscription, ExecutionError, FieldError, GraphQLEnum, - GraphQLObject, RootNode, Value, Variables, graphql_object, -}; - -pub type QueryResult = Result< - ( - Value, - Vec>, - ), - String, ->; - -pub struct Context; - -impl Context { - fn new() -> Self { - Self - } -} - -impl juniper::Context for Context {} - -#[derive(GraphQLEnum)] -pub enum Gender { - Male, - Female, - Other, -} - -#[derive(GraphQLEnum)] -pub enum UserKind { - SuperAdmin, - Admin, - Moderator, - User, - Guest, -} - -#[derive(GraphQLObject)] -pub struct User { - pub id: i32, - pub kind: UserKind, - pub username: String, - pub email: String, - pub gender: Option, -} +//! Benchmarks of [`juniper`] against [`async_graphql`] and [`apollo_compiler`]. +//! +//! Every [`Engine`] serves the same schema and the same data. The `comparison` benchmark runs +//! every [`Operation`] through every engine. One run starts with a JSON request body and ends with +//! a JSON response body. A GraphQL server does the same work for each request, without HTTP. +//! +//! Run the benchmark with `cargo bench -p juniper_benchmarks`. +//! +//! [`Engine`]: engine::Engine +//! [`Operation`]: operation::Operation -impl User { - fn new(id: i32) -> Self { - Self { - id, - kind: UserKind::Admin, - username: "userx".into(), - email: "userx@domain.com".into(), - gender: Some(Gender::Female), - } - } -} - -pub struct Query; - -#[graphql_object(context = Context)] -impl Query { - fn user_sync_instant(id: i32) -> Result { - Ok(User::new(id)) - } - - fn users_sync_instant(ids: Option>) -> Result, FieldError> { - if let Some(ids) = ids { - let users = ids.into_iter().map(User::new).collect(); - Ok(users) - } else { - Ok(vec![]) - } - } - - async fn user_async_instant(id: i32) -> Result { - Ok(User::new(id)) - } - - async fn users_async_instant(ids: Option>) -> Result, FieldError> { - if let Some(ids) = ids { - let users = ids.into_iter().map(User::new).collect(); - Ok(users) - } else { - Ok(vec![]) - } - } -} - -pub fn new_schema() -> RootNode, EmptySubscription> { - RootNode::new(Query, EmptyMutation::new(), EmptySubscription::new()) -} - -pub fn execute_sync(query: &str, vars: Variables) -> QueryResult { - let root = new_schema(); - let ctx = Context::new(); - juniper::execute_sync(query, None, &root, &vars, &ctx).map_err(|e| format!("{e:?}")) -} +#![cfg_attr(test, allow(unused_crate_dependencies, reason = "benches"))] -pub async fn execute(query: &str, vars: Variables) -> QueryResult { - let root = new_schema(); - let ctx = Context::new(); - juniper::execute(query, None, &root, &vars, &ctx) - .await - .map_err(|e| format!("{e:?}")) -} +pub mod data; +pub mod engine; +pub mod operation; diff --git a/benches/src/operation.rs b/benches/src/operation.rs new file mode 100644 index 000000000..9863645a2 --- /dev/null +++ b/benches/src/operation.rs @@ -0,0 +1,215 @@ +//! Operations run by the benchmarks. + +use crate::engine::{ + Engine, + apollo_compiler::ApolloCompiler, + async_graphql::AsyncGraphql, + juniper::{JuniperAsync, JuniperAsyncFn, JuniperSync}, +}; + +/// GraphQL operation with its variables. +#[derive(Clone, Copy, Debug)] +pub struct Operation { + /// Name of the operation in benchmark IDs. + pub name: &'static str, + + /// GraphQL document with a single operation. + pub document: &'static str, + + /// Variables of the operation as a JSON object. + pub variables: &'static str, + + /// All engines return the same response. + /// + /// Introspection responses differ because each engine adds its own built-in types and + /// directives to the schema. + pub same_response: bool, + + /// The operation selects the `isOneOf` field of `__Type`. + /// + /// The September 2025 specification added this field. Only engines with + /// [`Engine::SUPPORTS_IS_ONE_OF`] run the operation. + pub selects_is_one_of: bool, +} + +impl Operation { + /// Returns the GraphQL-over-HTTP JSON request body of this operation. + /// + /// # Panics + /// + /// If [`Operation::variables`] is not valid JSON. + pub fn request(&self) -> String { + let variables: serde_json::Value = + serde_json::from_str(self.variables).expect("variables are valid JSON"); + serde_json::json!({ "query": self.document, "variables": variables }).to_string() + } + + /// Indicates whether the `E` engine supports all features that this operation uses. + pub fn runs_on(&self) -> bool { + !self.selects_is_one_of || E::SUPPORTS_IS_ONE_OF + } + + /// Executes this operation once with every engine that supports it, and checks the + /// responses. + /// + /// The `juniper` engines serve one schema, so they always return the same response. + /// + /// # Panics + /// + /// If a response contains errors, or if the engines return different responses for an + /// operation with [`Operation::same_response`]. + pub fn check(&self) { + let request = self.request(); + let juniper = JuniperAsync::default().response(&request); + assert_eq!( + JuniperSync::default().response(&request), + juniper, + "synchronous and asynchronous `juniper` responses differ for `{}`", + self.name, + ); + assert_eq!( + JuniperAsyncFn::default().response(&request), + juniper, + "`juniper` responses with `async fn` and synchronous resolvers differ for `{}`", + self.name, + ); + self.check_engine::(&request, &juniper); + self.check_engine::(&request, &juniper); + } + + /// Executes this operation once with the `E` engine, if the engine supports it, and checks + /// the response against the `juniper` response. + fn check_engine(&self, request: &str, juniper: &serde_json::Value) { + if !self.runs_on::() { + return; + } + let response = E::default().response(request); + if self.same_response { + assert_eq!( + &response, + juniper, + "`{}` and `juniper` responses differ for `{}`", + E::NAME, + self.name, + ); + } + } +} + +/// Single scalar field. Measures the fixed cost of a request. +pub const MINIMAL: Operation = Operation { + name: "minimal", + document: include_str!("operation/minimal.graphql"), + variables: "{}", + same_response: true, + selects_is_one_of: false, +}; + +/// List of 1000 objects with 6 scalar fields each. Measures the cost of a field. +pub const FLAT_LIST: Operation = Operation { + name: "flat_list", + document: include_str!("operation/flat_list.graphql"), + variables: "{}", + same_response: true, + selects_is_one_of: false, +}; + +/// Three levels of lists with 2220 objects in total. Measures the cost of a nested object. +pub const NESTED_LISTS: Operation = Operation { + name: "nested_lists", + document: include_str!("operation/nested_lists.graphql"), + variables: "{}", + same_response: true, + selects_is_one_of: false, +}; + +/// Query of the `async-graphql` benchmark with plain lists instead of connections. +pub const STAR_WARS: Operation = Operation { + name: "star_wars", + document: include_str!("operation/star_wars.graphql"), + variables: "{}", + same_response: true, + selects_is_one_of: false, +}; + +/// Fragments on an interface, variables, and directives. +pub const FRAGMENTS: Operation = Operation { + name: "fragments", + document: include_str!("operation/fragments.graphql"), + variables: r#"{"episode": "EMPIRE", "withFriends": true}"#, + same_response: true, + selects_is_one_of: false, +}; + +/// Introspection query of GraphiQL. +pub const INTROSPECTION: Operation = Operation { + name: "introspection", + document: include_str!("operation/introspection.graphql"), + variables: "{}", + same_response: false, + selects_is_one_of: true, +}; + +/// Introspection query of GraphiQL without the `isOneOf` field. +/// +/// Engines without the `isOneOf` field run this operation instead of [`INTROSPECTION`]. +pub const INTROSPECTION_WITHOUT_ONE_OF: Operation = Operation { + name: "introspection_without_one_of", + document: include_str!("operation/introspection_without_one_of.graphql"), + variables: "{}", + same_response: false, + selects_is_one_of: false, +}; + +/// All operations in benchmark order. +pub const ALL: [Operation; 7] = [ + MINIMAL, + FLAT_LIST, + NESTED_LISTS, + STAR_WARS, + FRAGMENTS, + INTROSPECTION, + INTROSPECTION_WITHOUT_ONE_OF, +]; + +#[cfg(test)] +mod tests { + use super::{ALL, INTROSPECTION, INTROSPECTION_WITHOUT_ONE_OF, Operation}; + use crate::engine::{apollo_compiler::ApolloCompiler, juniper::JuniperAsync}; + + #[test] + fn engines_agree() { + for operation in ALL { + operation.check(); + } + } + + #[test] + #[should_panic(expected = "returned errors")] + fn check_rejects_errors() { + Operation { + name: "unknown_field", + document: "{ unknown }", + variables: "{}", + same_response: true, + selects_is_one_of: false, + } + .check(); + } + + mod runs_on { + use super::{ApolloCompiler, INTROSPECTION, INTROSPECTION_WITHOUT_ONE_OF, JuniperAsync}; + + #[test] + fn engine_with_is_one_of_runs_both_introspections() { + assert!(INTROSPECTION.runs_on::()); + assert!(INTROSPECTION_WITHOUT_ONE_OF.runs_on::()); + } + + #[test] + fn engine_without_is_one_of_skips_introspection_with_it() { + assert!(!INTROSPECTION.runs_on::()); + assert!(INTROSPECTION_WITHOUT_ONE_OF.runs_on::()); + } + } +} diff --git a/benches/src/operation/flat_list.graphql b/benches/src/operation/flat_list.graphql new file mode 100644 index 000000000..fa9de5822 --- /dev/null +++ b/benches/src/operation/flat_list.graphql @@ -0,0 +1,10 @@ +{ + users(first: 1000) { + id + name + email + active + score + role + } +} diff --git a/benches/src/operation/fragments.graphql b/benches/src/operation/fragments.graphql new file mode 100644 index 000000000..56daf33a9 --- /dev/null +++ b/benches/src/operation/fragments.graphql @@ -0,0 +1,24 @@ +query Hero($episode: Episode, $withFriends: Boolean!) { + hero(episode: $episode) { + ...CharacterFields + ... on Human { + homePlanet + } + ... on Droid { + primaryFunction + } + friends @include(if: $withFriends) { + ...CharacterFields + friends { + ...CharacterFields + } + } + } +} + +fragment CharacterFields on Character { + __typename + id + name + appearsIn +} diff --git a/benches/src/operation/introspection.graphql b/benches/src/operation/introspection.graphql new file mode 100644 index 000000000..997106650 --- /dev/null +++ b/benches/src/operation/introspection.graphql @@ -0,0 +1,105 @@ +query IntrospectionQuery { + __schema { + description + queryType { name kind } + mutationType { name kind } + subscriptionType { name kind } + types { + ...FullType + } + directives { + name + description + isRepeatable + locations + args(includeDeprecated: true) { + ...InputValue + } + } + } +} + +fragment FullType on __Type { + kind + name + description + specifiedByURL + isOneOf + fields(includeDeprecated: true) { + name + description + args(includeDeprecated: true) { + ...InputValue + } + type { + ...TypeRef + } + isDeprecated + deprecationReason + } + inputFields(includeDeprecated: true) { + ...InputValue + } + interfaces { + ...TypeRef + } + enumValues(includeDeprecated: true) { + name + description + isDeprecated + deprecationReason + } + possibleTypes { + ...TypeRef + } +} + +fragment InputValue on __InputValue { + name + description + type { ...TypeRef } + defaultValue + isDeprecated + deprecationReason +} + +fragment TypeRef on __Type { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + } + } + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/benches/src/operation/introspection_without_one_of.graphql b/benches/src/operation/introspection_without_one_of.graphql new file mode 100644 index 000000000..6fd5cb6a8 --- /dev/null +++ b/benches/src/operation/introspection_without_one_of.graphql @@ -0,0 +1,104 @@ +query IntrospectionQuery { + __schema { + description + queryType { name kind } + mutationType { name kind } + subscriptionType { name kind } + types { + ...FullType + } + directives { + name + description + isRepeatable + locations + args(includeDeprecated: true) { + ...InputValue + } + } + } +} + +fragment FullType on __Type { + kind + name + description + specifiedByURL + fields(includeDeprecated: true) { + name + description + args(includeDeprecated: true) { + ...InputValue + } + type { + ...TypeRef + } + isDeprecated + deprecationReason + } + inputFields(includeDeprecated: true) { + ...InputValue + } + interfaces { + ...TypeRef + } + enumValues(includeDeprecated: true) { + name + description + isDeprecated + deprecationReason + } + possibleTypes { + ...TypeRef + } +} + +fragment InputValue on __InputValue { + name + description + type { ...TypeRef } + defaultValue + isDeprecated + deprecationReason +} + +fragment TypeRef on __Type { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + ofType { + kind + name + } + } + } + } + } + } + } + } + } +} \ No newline at end of file diff --git a/benches/src/operation/minimal.graphql b/benches/src/operation/minimal.graphql new file mode 100644 index 000000000..eb8f4922a --- /dev/null +++ b/benches/src/operation/minimal.graphql @@ -0,0 +1,3 @@ +{ + hello +} diff --git a/benches/src/operation/nested_lists.graphql b/benches/src/operation/nested_lists.graphql new file mode 100644 index 000000000..102e390b2 --- /dev/null +++ b/benches/src/operation/nested_lists.graphql @@ -0,0 +1,14 @@ +{ + users(first: 20) { + id + name + friends(first: 10) { + id + name + friends(first: 10) { + id + name + } + } + } +} diff --git a/benches/src/operation/star_wars.graphql b/benches/src/operation/star_wars.graphql new file mode 100644 index 000000000..957eb203a --- /dev/null +++ b/benches/src/operation/star_wars.graphql @@ -0,0 +1,30 @@ +{ + humans { + id + name + friends { + id + name + friends { + id + name + } + } + appearsIn + homePlanet + } + droids { + id + name + friends { + id + name + friends { + id + name + } + } + appearsIn + primaryFunction + } +} diff --git a/juniper/Cargo.toml b/juniper/Cargo.toml index 32b0989eb..15aea6c77 100644 --- a/juniper/Cargo.toml +++ b/juniper/Cargo.toml @@ -83,7 +83,6 @@ void = { version = "1.0.2", optional = true } [dev-dependencies] arcstr = { version = "1.1", features = ["serde"] } -bencher = "0.1.2" chrono = { version = "0.4.30", features = ["alloc"], default-features = false } compact_str = { version = "0.10", features = ["serde"] } jiff = { version = "0.2", features = ["tzdb-bundle-always"], default-features = false } @@ -103,9 +102,3 @@ missing_docs = "warn" non_ascii_idents = "forbid" unexpected_cfgs = { level = "warn", check-cfg = ['cfg(nightly)'] } unused_crate_dependencies = "warn" - -[[bench]] -name = "bench" -harness = false -path = "benches/bench.rs" -required-features = ["expose-test-schema"] diff --git a/juniper/benches/bench.rs b/juniper/benches/bench.rs deleted file mode 100644 index 58a25157c..000000000 --- a/juniper/benches/bench.rs +++ /dev/null @@ -1,59 +0,0 @@ -//! Benchmarks of `juniper` crate. - -#![allow(unused_crate_dependencies, reason = "single benchmark")] -#![expect( - missing_docs, - reason = "`bencher` macros generate undocumented functions" -)] - -use bencher::{Bencher, benchmark_group, benchmark_main}; -use juniper::{ - DefaultScalarValue, EmptyMutation, EmptySubscription, RootNode, execute_sync, graphql_vars, - tests::fixtures::starwars::schema::{Database, Query}, -}; - -fn query_type_name(b: &mut Bencher) { - let database = Database::new(); - let schema: RootNode< - Query, - EmptyMutation, - EmptySubscription, - DefaultScalarValue, - > = RootNode::new( - Query, - EmptyMutation::::new(), - EmptySubscription::::new(), - ); - - // language=GraphQL - let query = "query IntrospectionQueryTypeQuery { - __schema { - queryType { - name - } - } - }"; - - b.iter(|| execute_sync(query, None, &schema, &graphql_vars! {}, &database)); -} - -fn introspection_query(b: &mut Bencher) { - let database = Database::new(); - let schema: RootNode< - Query, - EmptyMutation, - EmptySubscription, - DefaultScalarValue, - > = RootNode::new( - Query, - EmptyMutation::::new(), - EmptySubscription::::new(), - ); - - let query = include_str!("../src/introspection/query.graphql"); - - b.iter(|| execute_sync(query, None, &schema, &graphql_vars! {}, &database)); -} - -benchmark_group!(queries, query_type_name, introspection_query); -benchmark_main!(queries); diff --git a/juniper/src/lib.rs b/juniper/src/lib.rs index aeee370e1..ade0ba02b 100644 --- a/juniper/src/lib.rs +++ b/juniper/src/lib.rs @@ -9,10 +9,6 @@ extern crate core; extern crate self as juniper; -#[cfg(test)] -mod for_benches_only { - use bencher as _; -} #[cfg(test)] mod for_feature_gated_tests_only { #[cfg(not(feature = "chrono"))]