|
| 1 | +--- |
| 2 | +title: "Profiling requests" |
| 3 | +date: 2025-08-05T12:52:46+10:00 |
| 4 | +--- |
| 5 | + |
| 6 | +# Profiling GraphQL requests |
| 7 | + |
| 8 | +We've introduced a new profiling feature to help you understand the performance of your GraphQL executions. It provides detailed insights into DataFetcher calls, Dataloader usage, and execution timing. This guide will show you how to use it and interpret the results. |
| 9 | + |
| 10 | +The Profiler is available in all versions after v25.0.beta-5. It will also be included in the forthcoming official v25.0 release. |
| 11 | + |
| 12 | +## Enabling the Profiler |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +```java |
| 17 | +import graphql.ExecutionInput; |
| 18 | +import graphql.GraphQL; |
| 19 | + |
| 20 | +// ... |
| 21 | +GraphQL graphql = GraphQL.newGraphQL(schema).build(); |
| 22 | + |
| 23 | +ExecutionInput executionInput = ExecutionInput.newExecutionInput() |
| 24 | + .query("query Hello { world }") |
| 25 | + .profileExecution(true) // Enable profiling |
| 26 | + .build(); |
| 27 | + |
| 28 | +graphql.execute(executionInput); |
| 29 | +``` |
| 30 | + |
| 31 | +## Accessing the Profiler Results |
| 32 | + |
| 33 | +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. |
| 34 | + |
| 35 | +The result object is stored under the key `ProfilerResult.PROFILER_CONTEXT_KEY`. |
| 36 | + |
| 37 | +```java |
| 38 | +import graphql.ExecutionInput; |
| 39 | +import graphql.ExecutionResult; |
| 40 | +import graphql.GraphQL; |
| 41 | +import graphql.ProfilerResult; |
| 42 | + |
| 43 | +// ... |
| 44 | +ExecutionInput executionInput = /* ... see above ... */ |
| 45 | +ExecutionResult executionResult = graphql.execute(executionInput); |
| 46 | + |
| 47 | +ProfilerResult profilerResult = executionInput.getGraphQLContext().get(ProfilerResult.PROFILER_CONTEXT_KEY); |
| 48 | + |
| 49 | +if (profilerResult != null) { |
| 50 | + Map<String, Object> summary = profilerResult.shortSummaryMap(); |
| 51 | + System.out.println(summary); // or log as you prefer |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## Understanding the Profiler Results |
| 56 | + |
| 57 | +A great way to get a quick overview about `ProfilerResult` is by using the `shortSummaryMap()` method. It returns a map with key metrics about the execution, which you can use for logging. Let's break down what each key means. |
| 58 | + |
| 59 | +### The ProfilerResult Short Summary Map |
| 60 | + |
| 61 | +Here's a detailed explanation of the fields you'll find in the map returned by `shortSummaryMap()`: |
| 62 | + |
| 63 | +| Key | Type | Description | |
| 64 | +| ------------------------------------- | ----------- |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 65 | +| `executionId` | `String` | The unique ID for this GraphQL execution. | |
| 66 | +| `operationName` | `String` | The name of the operation, if one was provided in the query (e.g., `query MyQuery { ... }`). | |
| 67 | +| `operationType` | `String` | The type of operation, such as `QUERY`, `MUTATION`, or `SUBSCRIPTION`. | |
| 68 | +| `startTimeNs` | `long` | The system time in nanoseconds when the execution started. | |
| 69 | +| `endTimeNs` | `long` | The system time in nanoseconds when the execution finished. | |
| 70 | +| `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 DataFetchers. | |
| 71 | +| `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. | |
| 72 | +| `totalDataFetcherInvocations` | `int` | The total number of times any DataFetcher was invoked. | |
| 73 | +| `totalCustomDataFetcherInvocations` | `int` | The number of invocations for DataFetchers you've written yourself (i.e., not the built-in `PropertyDataFetcher`). | |
| 74 | +| `totalTrivialDataFetcherInvocations` | `int` | The number of invocations for the built-in `PropertyDataFetcher`. | |
| 75 | +| `totalWrappedTrivialDataFetcherInvocations` | `int` | The number of invocations for DataFetchers that wrap a `PropertyDataFetcher`. | |
| 76 | +| `fieldsFetchedCount` | `int` | The number of unique fields fetched during the execution. | |
| 77 | +| `dataLoaderChainingEnabled` | `boolean` | `true` if the experimental Dataloader chaining feature was enabled for this execution. | |
| 78 | +| `dataLoaderLoadInvocations` | `Map` | A map where keys are Dataloader 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 Dataloader cache. | |
| 79 | +| `oldStrategyDispatchingAll` | `Set` | If Chained DataLoaders are not used, the older dispatching strategy is used instead. This key lists the levels where DataLoaders were dispatched. | |
| 80 | +| `dispatchEvents` | `List<Map>` | A list of events, one for each time a Dataloader was dispatched. See below for details. | |
| 81 | +| `instrumentationClasses` | `List<String>` | A list of the class names of the `Instrumentation`s used during this execution. | |
| 82 | +| `dataFetcherResultTypes` | `Map` | A summary of the types of values returned by your custom DataFetchers. See below for details. | |
| 83 | + |
| 84 | +#### `dispatchEvents` |
| 85 | + |
| 86 | +This is a list of maps, each detailing a `DataLoader` dispatch event. |
| 87 | + |
| 88 | +| Key | Type | Description | |
| 89 | +| -------------- | -------- | -------------------------------------------------------------------- | |
| 90 | +| `type` | `String` | The type of dispatch. Can be `LEVEL_STRATEGY_DISPATCH`, `CHAINED_STRATEGY_DISPATCH`, `DELAYED_DISPATCH`, `CHAINED_DELAYED_DISPATCH`, or `MANUAL_DISPATCH`. | |
| 91 | +| `dataLoader` | `String` | The name of the `DataLoader` that was dispatched. | |
| 92 | +| `level` | `int` | The execution strategy level at which the dispatch occurred. | |
| 93 | +| `keyCount` | `int` | The number of keys that were dispatched in this batch. | |
| 94 | + |
| 95 | +#### `dataFetcherResultTypes` |
| 96 | + |
| 97 | +This map gives you more information into the type of your DataFetchers' return values. |
| 98 | + |
| 99 | +The keys are `COMPLETABLE_FUTURE_COMPLETED`, `COMPLETABLE_FUTURE_NOT_COMPLETED`, and `MATERIALIZED`. |
| 100 | +Each key maps to another map with two keys: |
| 101 | +* `count`: The number of unique fields with DataFetchers that returned this result type. |
| 102 | +* `invocations`: The total number of invocations across all fields that returned this result type. |
| 103 | + |
| 104 | +Here's what the result types mean: |
| 105 | + |
| 106 | +| Result Type | Meaning | |
| 107 | +| ---------------------------------- | --------------------------------------------------------------------------------------------- | |
| 108 | +| `COMPLETABLE_FUTURE_COMPLETED` | The DataFetcher returned a `CompletableFuture` that was already completed when it was returned. | |
| 109 | +| `COMPLETABLE_FUTURE_NOT_COMPLETED` | The DataFetcher returned an incomplete `CompletableFuture`. | |
| 110 | +| `MATERIALIZED` | The DataFetcher returned a simple value (i.e., not a `CompletableFuture`). | |
| 111 | + |
| 112 | +### A note on engine timing statistics logged from an `Instrumentation` |
| 113 | + |
| 114 | +If you're logging the `ProfilerResult` from an `Instrumentation`, note that engine timing statistics such as `startTimeNs`, `endTimeNs`, `totalRunTimeNs`, `engineTotalRunningTimeNs` will be set to `0`. This is because the timing is set after all `Instrumentation`s have run, so it is not available from within an `Instrumentation`. |
| 115 | + |
| 116 | +Apart from engine timing information, all other `ProfilerResult` information is still valid if accessed from within an `Instrumentation`. |
0 commit comments