|
| 1 | +package graphql; |
| 2 | + |
| 3 | +import graphql.execution.EngineRunningObserver; |
| 4 | +import graphql.execution.ExecutionId; |
| 5 | +import graphql.execution.ResultPath; |
| 6 | +import graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys; |
| 7 | +import graphql.language.OperationDefinition; |
| 8 | +import graphql.schema.DataFetcher; |
| 9 | +import graphql.schema.PropertyDataFetcher; |
| 10 | +import graphql.schema.SingletonPropertyDataFetcher; |
| 11 | +import org.jspecify.annotations.NullMarked; |
| 12 | + |
| 13 | +import java.util.concurrent.CompletableFuture; |
| 14 | +import java.util.concurrent.atomic.AtomicLong; |
| 15 | + |
| 16 | +@Internal |
| 17 | +@NullMarked |
| 18 | +public class ProfilerImpl implements Profiler { |
| 19 | + |
| 20 | + private volatile long startTime; |
| 21 | + private volatile long endTime; |
| 22 | + private volatile long lastStartTime; |
| 23 | + private final AtomicLong engineTotalRunningTime = new AtomicLong(); |
| 24 | + |
| 25 | + |
| 26 | + final ProfilerResult profilerResult = new ProfilerResult(); |
| 27 | + |
| 28 | + public ProfilerImpl(GraphQLContext graphQLContext) { |
| 29 | + graphQLContext.put(ProfilerResult.PROFILER_CONTEXT_KEY, profilerResult); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void executionInput(ExecutionInput executionInput) { |
| 34 | + profilerResult.setExecutionId(executionInput.getExecutionId()); |
| 35 | + boolean dataLoaderChainingEnabled = executionInput.getGraphQLContext().getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING, false); |
| 36 | + profilerResult.setDataLoaderChainingEnabled(dataLoaderChainingEnabled); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void fieldFetched(Object fetchedObject, DataFetcher<?> dataFetcher, ResultPath path) { |
| 41 | + String key = "/" + String.join("/", path.getKeysOnly()); |
| 42 | + profilerResult.addFieldFetched(key); |
| 43 | + profilerResult.incrementDataFetcherInvocationCount(key); |
| 44 | + ProfilerResult.DataFetcherType dataFetcherType; |
| 45 | + if (dataFetcher instanceof PropertyDataFetcher || dataFetcher instanceof SingletonPropertyDataFetcher) { |
| 46 | + dataFetcherType = ProfilerResult.DataFetcherType.PROPERTY_DATA_FETCHER; |
| 47 | + } else { |
| 48 | + dataFetcherType = ProfilerResult.DataFetcherType.CUSTOM; |
| 49 | + // we only record the type of the result if it is not a PropertyDataFetcher |
| 50 | + ProfilerResult.DataFetcherResultType dataFetcherResultType; |
| 51 | + if (fetchedObject instanceof CompletableFuture) { |
| 52 | + CompletableFuture<?> completableFuture = (CompletableFuture<?>) fetchedObject; |
| 53 | + if (completableFuture.isDone()) { |
| 54 | + dataFetcherResultType = ProfilerResult.DataFetcherResultType.COMPLETABLE_FUTURE_COMPLETED; |
| 55 | + } else { |
| 56 | + dataFetcherResultType = ProfilerResult.DataFetcherResultType.COMPLETABLE_FUTURE_NOT_COMPLETED; |
| 57 | + } |
| 58 | + } else { |
| 59 | + dataFetcherResultType = ProfilerResult.DataFetcherResultType.MATERIALIZED; |
| 60 | + } |
| 61 | + profilerResult.setDataFetcherResultType(key, dataFetcherResultType); |
| 62 | + } |
| 63 | + |
| 64 | + profilerResult.setDataFetcherType(key, dataFetcherType); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public EngineRunningObserver wrapEngineRunningObserver(EngineRunningObserver engineRunningObserver) { |
| 69 | + // nothing to wrap here |
| 70 | + return new EngineRunningObserver() { |
| 71 | + @Override |
| 72 | + public void runningStateChanged(ExecutionId executionId, GraphQLContext graphQLContext, RunningState runningState) { |
| 73 | + runningStateChangedImpl(executionId, graphQLContext, runningState); |
| 74 | + if (engineRunningObserver != null) { |
| 75 | + engineRunningObserver.runningStateChanged(executionId, graphQLContext, runningState); |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + private void runningStateChangedImpl(ExecutionId executionId, GraphQLContext graphQLContext, EngineRunningObserver.RunningState runningState) { |
| 82 | + long now = System.nanoTime(); |
| 83 | + if (runningState == EngineRunningObserver.RunningState.RUNNING_START) { |
| 84 | + startTime = now; |
| 85 | + lastStartTime = startTime; |
| 86 | + } else if (runningState == EngineRunningObserver.RunningState.NOT_RUNNING_FINISH) { |
| 87 | + endTime = now; |
| 88 | + engineTotalRunningTime.set(engineTotalRunningTime.get() + (endTime - lastStartTime)); |
| 89 | + profilerResult.setTimes(startTime, endTime, engineTotalRunningTime.get()); |
| 90 | + } else if (runningState == EngineRunningObserver.RunningState.RUNNING) { |
| 91 | + lastStartTime = now; |
| 92 | + } else if (runningState == EngineRunningObserver.RunningState.NOT_RUNNING) { |
| 93 | + engineTotalRunningTime.set(engineTotalRunningTime.get() + (now - lastStartTime)); |
| 94 | + } else { |
| 95 | + Assert.assertShouldNeverHappen(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void operationDefinition(OperationDefinition operationDefinition) { |
| 101 | + profilerResult.setOperation(operationDefinition); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void dataLoaderUsed(String dataLoaderName) { |
| 106 | + profilerResult.addDataLoaderUsed(dataLoaderName); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void chainedStrategyDispatching(int level) { |
| 111 | + profilerResult.chainedStrategyDispatching(level); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void oldStrategyDispatchingAll(int level) { |
| 116 | + profilerResult.oldStrategyDispatchingAll(level); |
| 117 | + } |
| 118 | +} |
0 commit comments