Skip to content

Commit f973812

Browse files
committed
Add first cut of documentation
1 parent f9d6eec commit f973812

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

documentation/profiler.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: "Profiling requests"
3+
date: 2025-08-05T12:52:46+10:00
4+
description: How to avoid the dreaded N+1 calls for data and make your graphql system more efficient
5+
---
6+
7+
# Profiling GraphQL requests
8+
9+
We've introduced a new query profiling feature to help you understand the performance characteristics of your GraphQL executions. It provides detailed insights into data fetcher calls, data loader usage, and execution timing. This guide will show you how to use it and interpret the results.
10+
11+
The Profiler is available in all versions after v25.0.beta-5. If you're not using a beta version, it will be included in the official v25.0 release.
12+
13+
## Enabling the Profiler
14+
15+
To enable profiling for a GraphQL execution, you need to set a flag on your `ExecutionInput`. It's as simple as calling `.profileExecution(true)` when building it.
16+
17+
```java
18+
import graphql.ExecutionInput;
19+
import graphql.GraphQL;
20+
21+
// ...
22+
GraphQL graphql = GraphQL.newGraphQL(schema).build();
23+
24+
ExecutionInput executionInput = ExecutionInput.newExecutionInput()
25+
.query("{ hello }")
26+
.profileExecution(true) // Enable profiling
27+
.build();
28+
29+
graphql.execute(executionInput);
30+
```
31+
32+
## Accessing the Profiler Results
33+
34+
The profiling results are stored in the `GraphQLContext` associated with your `ExecutionInput`. After the execution is complete, you can retrieve the `ProfilerResult` object from the context.
35+
36+
The result object is stored under the key `ProfilerResult.PROFILER_CONTEXT_KEY`.
37+
38+
```java
39+
import graphql.ExecutionInput;
40+
import graphql.ExecutionResult;
41+
import graphql.GraphQL;
42+
import graphql.ProfilerResult;
43+
44+
// ...
45+
ExecutionInput executionInput = /* ... see above ... */
46+
ExecutionResult executionResult = graphql.execute(executionInput);
47+
48+
ProfilerResult profilerResult = executionInput.getGraphQLContext().get(ProfilerResult.PROFILER_CONTEXT_KEY);
49+
50+
if (profilerResult != null) {
51+
// You now have access to the profiling data
52+
Map<String, Object> summary = profilerResult.shortSummaryMap();
53+
System.out.println(summary);
54+
}
55+
```
56+
57+
## Understanding the Profiler Results
58+
59+
The `ProfilerResult` object contains a wealth of information. A great way to get a quick overview is by using the `shortSummaryMap()` method. It returns a map with key metrics about the execution. Let's break down what each key means.
60+
61+
### The Short Summary Map
62+
63+
Here's a detailed explanation of the fields you'll find in the map returned by `shortSummaryMap()`:
64+
65+
| Key | Type | Description |
66+
| ------------------------------------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
67+
| `executionId` | `String` | The unique ID for this GraphQL execution. |
68+
| `operationName` | `String` | The name of the operation, if one was provided in the query (e.g., `query MyQuery { ... }`). |
69+
| `operationType` | `String` | The type of operation, such as `QUERY`, `MUTATION`, or `SUBSCRIPTION`. |
70+
| `startTimeNs` | `long` | The system monotonic time in nanoseconds when the execution started. |
71+
| `endTimeNs` | `long` | The system monotonic time in nanoseconds when the execution finished. |
72+
| `totalRunTimeNs` | `long` | The total wall-clock time of the execution (`endTimeNs - startTimeNs`). This includes time spent waiting for asynchronous operations like database calls or external API requests within your data fetchers. |
73+
| `engineTotalRunningTimeNs` | `long` | The total time the GraphQL engine spent actively running on a thread. This is like the "CPU time" of the execution and excludes time spent waiting for `CompletableFuture`s to complete. Comparing this with `totalRunTimeNs` can give you a good idea of how much time is spent on I/O. |
74+
| `totalDataFetcherInvocations` | `int` | The total number of times any data fetcher was invoked. |
75+
| `totalCustomDataFetcherInvocations` | `int` | The number of invocations for data fetchers you've written yourself (i.e., not the built-in `PropertyDataFetcher`). This is often the most interesting data fetcher metric. |
76+
| `totalTrivialDataFetcherInvocations` | `int` | The number of invocations for the built-in `PropertyDataFetcher`, which simply retrieves a property from a POJO. |
77+
| `totalWrappedTrivialDataFetcherInvocations` | `int` | The number of invocations for data fetchers that wrap a `PropertyDataFetcher`. This usually happens when you use instrumentation to wrap data fetchers. |
78+
| `fieldsFetchedCount` | `int` | The number of unique fields fetched during the execution. |
79+
| `dataLoaderChainingEnabled` | `boolean` | `true` if the experimental data loader chaining feature was enabled for this execution. |
80+
| `dataLoaderLoadInvocations` | `Map` | A map where keys are data loader names and values are the number of times `load()` was called on them. Note that this counts all `load()` calls, including those that hit the data loader cache. |
81+
| `oldStrategyDispatchingAll` | `Set` | An advanced metric related to an older data loader dispatching strategy. It contains the execution levels where all data loaders were dispatched at once. |
82+
| `dispatchEvents` | `List<Map>` | A list of events, one for each time a data loader was dispatched. See below for details. |
83+
| `instrumentationClasses` | `List<String>` | A list of the fully qualified class names of the `Instrumentation` implementations used during this execution. |
84+
| `dataFetcherResultTypes` | `Map` | A summary of the types of values returned by your custom data fetchers. See below for details. |
85+
86+
#### `dispatchEvents`
87+
88+
This is a list of maps, each detailing a `DataLoader` dispatch event.
89+
90+
| Key | Type | Description |
91+
| -------------- | -------- | -------------------------------------------------------------------- |
92+
| `type` | `String` | The type of dispatch. Can be `LEVEL_STRATEGY_DISPATCH`, `CHAINED_STRATEGY_DISPATCH`, `DELAYED_DISPATCH`, `CHAINED_DELAYED_DISPATCH`, or `MANUAL_DISPATCH`. |
93+
| `dataLoader` | `String` | The name of the `DataLoader` that was dispatched. |
94+
| `level` | `int` | The execution strategy level at which the dispatch occurred. |
95+
| `keyCount` | `int` | The number of keys that were dispatched in this batch. |
96+
97+
#### `dataFetcherResultTypes`
98+
99+
This map gives you insight into the nature of your data fetchers' return values. This is especially useful for understanding the asynchronous behavior of your schema.
100+
101+
The keys are `COMPLETABLE_FUTURE_COMPLETED`, `COMPLETABLE_FUTURE_NOT_COMPLETED`, and `MATERIALIZED`.
102+
Each key maps to another map with two keys:
103+
* `count`: The number of unique fields with data fetchers that returned this result type.
104+
* `invocations`: The total number of invocations across all fields that returned this result type.
105+
106+
Here's what the result types mean:
107+
108+
| Result Type | Meaning |
109+
| ---------------------------------- | --------------------------------------------------------------------------------------------- |
110+
| `COMPLETABLE_FUTURE_COMPLETED` | The data fetcher returned a `CompletableFuture` that was already completed when it was returned. |
111+
| `COMPLETABLE_FUTURE_NOT_COMPLETED` | The data fetcher returned an incomplete `CompletableFuture`, indicating asynchronous work. |
112+
| `MATERIALIZED` | The data fetcher returned a simple value (i.e., not a `CompletableFuture`). |

0 commit comments

Comments
 (0)