Skip to main content
Version: master

Profiling GraphQL requests

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.

The Profiler is available in all versions after v25.0.beta-5. It will also be included in the forthcoming official v25.0 release.

Enabling the Profiler

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.

import graphql.ExecutionInput;
import graphql.GraphQL;

// ...
GraphQL graphql = GraphQL.newGraphQL(schema).build();

ExecutionInput executionInput = ExecutionInput.newExecutionInput()
.query("query Hello { world }")
.profileExecution(true) // Enable profiling
.build();

graphql.execute(executionInput);

Accessing the Profiler Results

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.

The result object is stored under the key ProfilerResult.PROFILER_CONTEXT_KEY.

import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.ProfilerResult;

// ...
ExecutionInput executionInput = /* ... see above ... */
ExecutionResult executionResult = graphql.execute(executionInput);

ProfilerResult profilerResult = executionInput.getGraphQLContext().get(ProfilerResult.PROFILER_CONTEXT_KEY);

if (profilerResult != null) {
Map<String, Object> summary = profilerResult.shortSummaryMap();
System.out.println(summary); // or log as you prefer
}

Understanding the Profiler Results

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.

The ProfilerResult Short Summary Map

Here's a detailed explanation of the fields you'll find in the map returned by shortSummaryMap():

KeyTypeDescription
executionIdStringThe unique ID for this GraphQL execution.
operationNameStringThe name of the operation, if one was provided in the query (e.g., query MyQuery { ... }).
operationTypeStringThe type of operation, such as QUERY, MUTATION, or SUBSCRIPTION.
startTimeNslongThe system time in nanoseconds when the execution started.
endTimeNslongThe system time in nanoseconds when the execution finished.
totalRunTimeNslongThe 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.
engineTotalRunningTimeNslongThe 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 CompletableFutures to complete. Comparing this with totalRunTimeNs can give you a good idea of how much time is spent on I/O.
totalDataFetcherInvocationsintThe total number of times any DataFetcher was invoked.
totalCustomDataFetcherInvocationsintThe number of invocations for DataFetchers you've written yourself (i.e., not the built-in PropertyDataFetcher).
totalTrivialDataFetcherInvocationsintThe number of invocations for the built-in PropertyDataFetcher.
totalWrappedTrivialDataFetcherInvocationsintThe number of invocations for DataFetchers that wrap a PropertyDataFetcher.
fieldsFetchedCountintThe number of unique fields fetched during the execution.
dataLoaderChainingEnabledbooleantrue if the experimental Dataloader chaining feature was enabled for this execution.
dataLoaderLoadInvocationsMapA 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.
oldStrategyDispatchingAllSetIf Chained DataLoaders are not used, the older dispatching strategy is used instead. This key lists the levels where DataLoaders were dispatched.
dispatchEventsList<Map>A list of events, one for each time a Dataloader was dispatched. See below for details.
instrumentationClassesList<String>A list of the class names of the Instrumentations used during this execution.
dataFetcherResultTypesMapA summary of the types of values returned by your custom DataFetchers. See below for details.

dispatchEvents

This is a list of maps, each detailing a DataLoader dispatch event.

KeyTypeDescription
typeStringThe type of dispatch. Can be LEVEL_STRATEGY_DISPATCH, CHAINED_STRATEGY_DISPATCH, DELAYED_DISPATCH, CHAINED_DELAYED_DISPATCH, or MANUAL_DISPATCH.
dataLoaderStringThe name of the DataLoader that was dispatched.
levelintThe execution strategy level at which the dispatch occurred.
keyCountintThe number of keys that were dispatched in this batch.

dataFetcherResultTypes

This map gives you more information into the type of your DataFetchers' return values.

The keys are COMPLETABLE_FUTURE_COMPLETED, COMPLETABLE_FUTURE_NOT_COMPLETED, and MATERIALIZED. Each key maps to another map with two keys:

  • count: The number of unique fields with DataFetchers that returned this result type.
  • invocations: The total number of invocations across all fields that returned this result type.

Here's what the result types mean:

Result TypeMeaning
COMPLETABLE_FUTURE_COMPLETEDThe DataFetcher returned a CompletableFuture that was already completed when it was returned.
COMPLETABLE_FUTURE_NOT_COMPLETEDThe DataFetcher returned an incomplete CompletableFuture.
MATERIALIZEDThe DataFetcher returned a simple value (i.e., not a CompletableFuture).

A note on engine timing statistics logged from an Instrumentation

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 Instrumentations have run, so it is not available from within an Instrumentation.

Apart from engine timing information, all other ProfilerResult information is still valid if accessed from within an Instrumentation.