From eaa86a54d468dec63ca26896f39c73ca8ef9f80f Mon Sep 17 00:00:00 2001 From: Jay Herron Date: Sun, 27 Sep 2026 13:41:28 -0600 Subject: [PATCH 1/2] test: Adds resolution and encoding benchmarks --- Benchmarks/Benchmarks/CollectionSchema.swift | 48 ++++++++++++++++ Benchmarks/Benchmarks/GraphQLBenchmarks.swift | 55 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 Benchmarks/Benchmarks/CollectionSchema.swift diff --git a/Benchmarks/Benchmarks/CollectionSchema.swift b/Benchmarks/Benchmarks/CollectionSchema.swift new file mode 100644 index 00000000..6bf0456b --- /dev/null +++ b/Benchmarks/Benchmarks/CollectionSchema.swift @@ -0,0 +1,48 @@ +import GraphQL + +// Contains a basic schema that holds a single, large static array. Used for testing processing throughput and +// large object encoding + +struct CollectionItem: Codable, Sendable { + let value1: String + let value2: Int +} + +let collectionItem = try! GraphQLObjectType( + name: "CollectionItem", + fields: [ + "value1": .init( + type: GraphQLString, + resolve: { source, _, _, _ in + (source as! CollectionItem).value1 + } + ), + "value2": .init( + type: GraphQLInt, + resolve: { source, _, _, _ in + (source as! CollectionItem).value2 + } + ) + ] +) + +func collectionSchema(count: Int) -> (schema: GraphQLSchema, collection: [CollectionItem]) { + let staticCollection: [CollectionItem] = (0.. Void = { + let encoder = JSONEncoder() + Benchmark("graphql") { _ in let result = try await graphql( schema: starWarsSchema, @@ -21,4 +24,56 @@ let benchmarks: @Sendable () -> Void = { """ ) } + + // Benchmarks the time to push a large static array through the GraphQL resolution system + Benchmark("resolution") { _, schema in + let result = try await graphql( + schema: schema, + request: """ + query { + collection { + value1 + value2 + } + } + """ + ) + } setup: { + return collectionSchema(count: 10_000).schema + } + + // Benchmarks the time to encode a GraphQLResult + Benchmark("encoding:GraphQLResult") { _, result in + let result = try encoder.encode(result) + } setup: { + return try await graphql( + schema: collectionSchema(count: 100_000).schema, + request: """ + query { + collection { + value1 + value2 + } + } + """ + ) + } + + // Benchmarks the baseline time to encode a strongly typed Swift type (for comparison with encoding a GraphQLResult with `Map` types) + Benchmark("encoding:baseline") { _, result in + let result = try encoder.encode(result) + } setup: { + return BaselineResult( + query: .init( + collection: collectionSchema(count: 100_000).collection + ) + ) + } + struct BaselineResult: Codable { + let query: Query + + struct Query: Codable { + let collection: [CollectionItem] + } + } } From 1982a1e15bad3f0b04590bcfe259188dac7138c9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 27 Sep 2026 21:33:37 +0000 Subject: [PATCH 2/2] test: Adds scalar collection resolution benchmarks --- Benchmarks/Benchmarks/CollectionSchema.swift | 33 +++++++++++++++++++ Benchmarks/Benchmarks/GraphQLBenchmarks.swift | 15 +++++++++ 2 files changed, 48 insertions(+) diff --git a/Benchmarks/Benchmarks/CollectionSchema.swift b/Benchmarks/Benchmarks/CollectionSchema.swift index 6bf0456b..c0a7b9f3 100644 --- a/Benchmarks/Benchmarks/CollectionSchema.swift +++ b/Benchmarks/Benchmarks/CollectionSchema.swift @@ -46,3 +46,36 @@ func collectionSchema(count: Int) -> (schema: GraphQLSchema, collection: [Collec ) return (schema, staticCollection) } + + + +func collectionScalarSchema(count: Int) -> GraphQLSchema { + let staticCollection: [CollectionItem] = (0.. Void = { return collectionSchema(count: 10_000).schema } + // Benchmarks the large static array exposed as a custom scalar + // This avoids all the GraphQL resolution of each element item, and as such is way faster. + Benchmark("resolution:scalar") { _, schema in + let result = try await graphql( + schema: schema, + request: """ + query { + collection + } + """ + ) + } setup: { + return collectionScalarSchema(count: 10_000) + } + // Benchmarks the time to encode a GraphQLResult Benchmark("encoding:GraphQLResult") { _, result in let result = try encoder.encode(result)