-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathProfilerImpl.java
More file actions
172 lines (152 loc) · 8.19 KB
/
ProfilerImpl.java
File metadata and controls
172 lines (152 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package graphql;
import graphql.execution.EngineRunningObserver;
import graphql.execution.ExecutionId;
import graphql.execution.ResultPath;
import graphql.execution.instrumentation.ChainedInstrumentation;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.dataloader.DataLoaderDispatchingContextKeys;
import graphql.introspection.Introspection;
import graphql.language.OperationDefinition;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLTypeUtil;
import graphql.schema.PropertyDataFetcher;
import graphql.schema.SingletonPropertyDataFetcher;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicLong;
@Internal
@NullMarked
public class ProfilerImpl implements Profiler {
private volatile long startTime;
private volatile long endTime;
private volatile long lastStartTime;
private final AtomicLong engineTotalRunningTime = new AtomicLong();
final ProfilerResult profilerResult = new ProfilerResult();
public ProfilerImpl(GraphQLContext graphQLContext) {
// No real work can happen here, since the engine didn't "officially" start yet.
graphQLContext.put(ProfilerResult.PROFILER_CONTEXT_KEY, profilerResult);
}
@Override
public void setExecutionInputAndInstrumentation(ExecutionInput executionInput, Instrumentation instrumentation) {
profilerResult.setExecutionId(executionInput.getExecutionIdNonNull());
boolean dataLoaderChainingEnabled = executionInput.getGraphQLContext().getBoolean(DataLoaderDispatchingContextKeys.ENABLE_DATA_LOADER_CHAINING, false);
profilerResult.setDataLoaderChainingEnabled(dataLoaderChainingEnabled);
List<String> instrumentationClasses = new ArrayList<>();
collectInstrumentationClasses(instrumentationClasses, instrumentation);
profilerResult.setInstrumentationClasses(instrumentationClasses);
}
private void collectInstrumentationClasses(List<String> result, Instrumentation instrumentation) {
if (instrumentation instanceof ChainedInstrumentation) {
ChainedInstrumentation chainedInstrumentation = (ChainedInstrumentation) instrumentation;
for (Instrumentation child : chainedInstrumentation.getInstrumentations()) {
collectInstrumentationClasses(result, child);
}
} else {
result.add(instrumentation.getClass().getName());
}
}
@Override
public void fieldFetched(Object fetchedObject, DataFetcher<?> originalDataFetcher, DataFetcher<?> dataFetcher, ResultPath path, GraphQLFieldDefinition fieldDef, GraphQLOutputType parentType) {
String key = "/" + String.join("/", path.getKeysOnly());
if (Introspection.isIntrospectionTypes(GraphQLTypeUtil.unwrapAll(fieldDef.getType()))
|| Introspection.isIntrospectionTypes(GraphQLTypeUtil.unwrapAll(parentType))
|| fieldDef.getName().equals(Introspection.SchemaMetaFieldDef.getName())
|| fieldDef.getName().equals(Introspection.TypeMetaFieldDef.getName())
|| fieldDef.getName().equals(Introspection.TypeNameMetaFieldDef.getName())) {
return;
}
profilerResult.addFieldFetched(key);
profilerResult.incrementDataFetcherInvocationCount(key);
ProfilerResult.DataFetcherType dataFetcherType;
if (dataFetcher instanceof PropertyDataFetcher || dataFetcher instanceof SingletonPropertyDataFetcher) {
dataFetcherType = ProfilerResult.DataFetcherType.TRIVIAL_DATA_FETCHER;
} else if (originalDataFetcher instanceof PropertyDataFetcher || originalDataFetcher instanceof SingletonPropertyDataFetcher) {
dataFetcherType = ProfilerResult.DataFetcherType.WRAPPED_TRIVIAL_DATA_FETCHER;
} else {
dataFetcherType = ProfilerResult.DataFetcherType.CUSTOM;
// we only record the type of the result if it is not a PropertyDataFetcher
ProfilerResult.DataFetcherResultType dataFetcherResultType;
if (fetchedObject instanceof CompletableFuture) {
CompletableFuture<?> completableFuture = (CompletableFuture<?>) fetchedObject;
if (completableFuture.isDone()) {
dataFetcherResultType = ProfilerResult.DataFetcherResultType.COMPLETABLE_FUTURE_COMPLETED;
} else {
dataFetcherResultType = ProfilerResult.DataFetcherResultType.COMPLETABLE_FUTURE_NOT_COMPLETED;
}
} else {
dataFetcherResultType = ProfilerResult.DataFetcherResultType.MATERIALIZED;
}
profilerResult.setDataFetcherResultType(key, dataFetcherResultType);
}
profilerResult.setDataFetcherType(key, dataFetcherType);
}
@Override
public EngineRunningObserver wrapEngineRunningObserver(@Nullable EngineRunningObserver engineRunningObserver) {
// nothing to wrap here
return new EngineRunningObserver() {
@Override
public void runningStateChanged(@Nullable ExecutionId executionId, GraphQLContext graphQLContext, RunningState runningState) {
runningStateChangedImpl(executionId, graphQLContext, runningState);
if (engineRunningObserver != null) {
engineRunningObserver.runningStateChanged(executionId, graphQLContext, runningState);
}
}
};
}
private void runningStateChangedImpl(@Nullable ExecutionId executionId, GraphQLContext graphQLContext, EngineRunningObserver.RunningState runningState) {
long now = System.nanoTime();
if (runningState == EngineRunningObserver.RunningState.RUNNING_START) {
startTime = now;
lastStartTime = startTime;
} else if (runningState == EngineRunningObserver.RunningState.NOT_RUNNING_FINISH) {
endTime = now;
engineTotalRunningTime.set(engineTotalRunningTime.get() + (endTime - lastStartTime));
profilerResult.setTimes(startTime, endTime, engineTotalRunningTime.get());
} else if (runningState == EngineRunningObserver.RunningState.RUNNING) {
lastStartTime = now;
} else if (runningState == EngineRunningObserver.RunningState.NOT_RUNNING) {
engineTotalRunningTime.set(engineTotalRunningTime.get() + (now - lastStartTime));
} else {
Assert.assertShouldNeverHappen();
}
}
@Override
public void operationDefinition(OperationDefinition operationDefinition) {
profilerResult.setOperation(operationDefinition);
}
@Override
public void dataLoaderUsed(String dataLoaderName) {
profilerResult.addDataLoaderUsed(dataLoaderName);
}
@Override
public void oldStrategyDispatchingAll(int level) {
profilerResult.oldStrategyDispatchingAll(level);
}
@Override
public void batchLoadedOldStrategy(String name, int level, int count) {
profilerResult.addDispatchEvent(name, level, count, ProfilerResult.DispatchEventType.LEVEL_STRATEGY_DISPATCH);
}
@Override
public void batchLoadedNewStrategy(String dataLoaderName, Integer level, int count, boolean delayed, boolean chained) {
ProfilerResult.DispatchEventType dispatchEventType = null;
if (delayed && !chained) {
dispatchEventType = ProfilerResult.DispatchEventType.DELAYED_DISPATCH;
} else if (delayed) {
dispatchEventType = ProfilerResult.DispatchEventType.CHAINED_DELAYED_DISPATCH;
} else if (!chained) {
dispatchEventType = ProfilerResult.DispatchEventType.LEVEL_STRATEGY_DISPATCH;
} else {
dispatchEventType = ProfilerResult.DispatchEventType.CHAINED_STRATEGY_DISPATCH;
}
profilerResult.addDispatchEvent(dataLoaderName, level, count, dispatchEventType);
}
@Override
public <V> void manualDispatch(String dataLoaderName, int level, int count) {
profilerResult.addDispatchEvent(dataLoaderName, level, count, ProfilerResult.DispatchEventType.MANUAL_DISPATCH);
}
}