-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathProfilerResult.java
More file actions
347 lines (282 loc) · 12.4 KB
/
ProfilerResult.java
File metadata and controls
347 lines (282 loc) · 12.4 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package graphql;
import graphql.execution.ExecutionId;
import graphql.language.OperationDefinition;
import graphql.language.OperationDefinition.Operation;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
@ExperimentalApi
@NullMarked
public class ProfilerResult {
public static final String PROFILER_CONTEXT_KEY = "__GJ_PROFILER";
@Nullable
private volatile ExecutionId executionId;
private long startTime;
private long endTime;
private long engineTotalRunningTime;
private final AtomicInteger totalDataFetcherInvocations = new AtomicInteger();
private final AtomicInteger totalTrivialDataFetcherInvocations = new AtomicInteger();
private final AtomicInteger totalWrappedTrivialDataFetcherInvocations = new AtomicInteger();
// this is the count of how many times a data loader was invoked per data loader name
private final Map<String, Integer> dataLoaderLoadInvocations = new ConcurrentHashMap<>();
@Nullable
private volatile String operationName;
@Nullable
private volatile Operation operationType;
private volatile boolean dataLoaderChainingEnabled;
private final Set<Integer> oldStrategyDispatchingAll = ConcurrentHashMap.newKeySet();
private final List<String> instrumentationClasses = Collections.synchronizedList(new ArrayList<>());
private final List<DispatchEvent> dispatchEvents = Collections.synchronizedList(new ArrayList<>());
/**
* the following fields can contain a lot of data for large requests
*/
// all fields fetched during the execution, key is the field path
private final Set<String> fieldsFetched = ConcurrentHashMap.newKeySet();
// this is the count of how many times a data fetcher was invoked per field
private final Map<String, Integer> dataFetcherInvocationCount = new ConcurrentHashMap<>();
// the type of the data fetcher per field, key is the field path
private final Map<String, DataFetcherType> dataFetcherTypeMap = new ConcurrentHashMap<>();
// the type of the data fetcher result field, key is the field path
// in theory different DataFetcher invocations can return different types, but we only record the first one
private final Map<String, DataFetcherResultType> dataFetcherResultType = new ConcurrentHashMap<>();
public void setInstrumentationClasses(List<String> instrumentationClasses) {
this.instrumentationClasses.addAll(instrumentationClasses);
}
public enum DispatchEventType {
LEVEL_STRATEGY_DISPATCH,
CHAINED_STRATEGY_DISPATCH,
DELAYED_DISPATCH,
CHAINED_DELAYED_DISPATCH,
MANUAL_DISPATCH,
}
public static class DispatchEvent {
final String dataLoaderName;
final Integer level; // is null for delayed dispatching
final int keyCount; // how many
final DispatchEventType type;
public DispatchEvent(String dataLoaderName, Integer level, int keyCount, DispatchEventType type) {
this.dataLoaderName = dataLoaderName;
this.level = level;
this.keyCount = keyCount;
this.type = type;
}
public String getDataLoaderName() {
return dataLoaderName;
}
public Integer getLevel() {
return level;
}
public int getKeyCount() {
return keyCount;
}
public DispatchEventType getType() {
return type;
}
@Override
public String toString() {
return "DispatchEvent{" +
"type=" + type +
", dataLoaderName='" + dataLoaderName + '\'' +
", level=" + level +
", keyCount=" + keyCount +
'}';
}
}
public enum DataFetcherType {
WRAPPED_TRIVIAL_DATA_FETCHER,
TRIVIAL_DATA_FETCHER,
CUSTOM
}
public enum DataFetcherResultType {
COMPLETABLE_FUTURE_COMPLETED,
COMPLETABLE_FUTURE_NOT_COMPLETED,
MATERIALIZED
}
// setters are package private to prevent exposure
void setDataLoaderChainingEnabled(boolean dataLoaderChainingEnabled) {
this.dataLoaderChainingEnabled = dataLoaderChainingEnabled;
}
void setDataFetcherType(String key, DataFetcherType dataFetcherType) {
dataFetcherTypeMap.putIfAbsent(key, dataFetcherType);
totalDataFetcherInvocations.incrementAndGet();
if (dataFetcherType == DataFetcherType.TRIVIAL_DATA_FETCHER) {
totalTrivialDataFetcherInvocations.incrementAndGet();
} else if (dataFetcherType == DataFetcherType.WRAPPED_TRIVIAL_DATA_FETCHER) {
totalWrappedTrivialDataFetcherInvocations.incrementAndGet();
}
}
void setDataFetcherResultType(String key, DataFetcherResultType fetchedType) {
dataFetcherResultType.putIfAbsent(key, fetchedType);
}
void incrementDataFetcherInvocationCount(String key) {
dataFetcherInvocationCount.compute(key, (k, v) -> v == null ? 1 : v + 1);
}
void addFieldFetched(String fieldPath) {
fieldsFetched.add(fieldPath);
}
void setExecutionId(ExecutionId executionId) {
this.executionId = executionId;
}
void setTimes(long startTime, long endTime, long engineTotalRunningTime) {
this.startTime = startTime;
this.endTime = endTime;
this.engineTotalRunningTime = engineTotalRunningTime;
}
void setOperation(OperationDefinition operationDefinition) {
this.operationName = operationDefinition.getName();
this.operationType = operationDefinition.getOperation();
}
void addDataLoaderUsed(String dataLoaderName) {
dataLoaderLoadInvocations.compute(dataLoaderName, (k, v) -> v == null ? 1 : v + 1);
}
void oldStrategyDispatchingAll(int level) {
oldStrategyDispatchingAll.add(level);
}
void addDispatchEvent(String dataLoaderName, Integer level, int count, DispatchEventType type) {
dispatchEvents.add(new DispatchEvent(dataLoaderName, level, count, type));
}
// public getters
public @Nullable String getOperationName() {
return operationName;
}
public Operation getOperationType() {
return Assert.assertNotNull(operationType);
}
public Set<String> getFieldsFetched() {
return fieldsFetched;
}
public Set<String> getCustomDataFetcherFields() {
Set<String> result = new LinkedHashSet<>(fieldsFetched);
for (String field : fieldsFetched) {
if (dataFetcherTypeMap.get(field) == DataFetcherType.CUSTOM) {
result.add(field);
}
}
return result;
}
public Set<String> getTrivialDataFetcherFields() {
Set<String> result = new LinkedHashSet<>(fieldsFetched);
for (String field : fieldsFetched) {
if (dataFetcherTypeMap.get(field) == DataFetcherType.TRIVIAL_DATA_FETCHER) {
result.add(field);
}
}
return result;
}
public int getTotalDataFetcherInvocations() {
return totalDataFetcherInvocations.get();
}
public int getTotalTrivialDataFetcherInvocations() {
return totalTrivialDataFetcherInvocations.get();
}
public int getTotalCustomDataFetcherInvocations() {
return totalDataFetcherInvocations.get() - totalTrivialDataFetcherInvocations.get() - totalWrappedTrivialDataFetcherInvocations.get();
}
public long getStartTime() {
return startTime;
}
public long getEndTime() {
return endTime;
}
public long getEngineTotalRunningTime() {
return engineTotalRunningTime;
}
public long getTotalExecutionTime() {
return endTime - startTime;
}
public Map<String, DataFetcherResultType> getDataFetcherResultType() {
return dataFetcherResultType;
}
public Map<String, Integer> getDataLoaderLoadInvocations() {
return dataLoaderLoadInvocations;
}
public Set<Integer> getOldStrategyDispatchingAll() {
return oldStrategyDispatchingAll;
}
public boolean isDataLoaderChainingEnabled() {
return dataLoaderChainingEnabled;
}
public List<DispatchEvent> getDispatchEvents() {
return dispatchEvents;
}
public List<String> getInstrumentationClasses() {
return instrumentationClasses;
}
public Map<String, Object> shortSummaryMap() {
Map<String, Object> result = new LinkedHashMap<>();
result.put("executionId", Assert.assertNotNull(executionId).toString());
result.put("operationName", operationName);
result.put("operationType", Assert.assertNotNull(operationType).toString());
result.put("startTimeNs", startTime);
result.put("endTimeNs", endTime);
result.put("totalRunTimeNs", endTime - startTime);
result.put("engineTotalRunningTimeNs", engineTotalRunningTime);
result.put("totalDataFetcherInvocations", totalDataFetcherInvocations);
result.put("totalCustomDataFetcherInvocations", getTotalCustomDataFetcherInvocations());
result.put("totalTrivialDataFetcherInvocations", totalTrivialDataFetcherInvocations);
result.put("totalWrappedTrivialDataFetcherInvocations", totalWrappedTrivialDataFetcherInvocations);
result.put("fieldsFetchedCount", fieldsFetched.size());
result.put("dataLoaderChainingEnabled", dataLoaderChainingEnabled);
result.put("dataLoaderLoadInvocations", dataLoaderLoadInvocations);
result.put("oldStrategyDispatchingAll", oldStrategyDispatchingAll);
result.put("dispatchEvents", getDispatchEventsAsMap());
result.put("instrumentationClasses", instrumentationClasses);
int completedCount = 0;
int completedInvokeCount = 0;
int notCompletedCount = 0;
int notCompletedInvokeCount = 0;
int materializedCount = 0;
int materializedInvokeCount = 0;
for (String field : dataFetcherResultType.keySet()) {
DataFetcherResultType dFRT = dataFetcherResultType.get(field);
if (dFRT == DataFetcherResultType.COMPLETABLE_FUTURE_COMPLETED) {
completedInvokeCount += Assert.assertNotNull(dataFetcherInvocationCount.get(field));
completedCount++;
} else if (dFRT == DataFetcherResultType.COMPLETABLE_FUTURE_NOT_COMPLETED) {
notCompletedInvokeCount += Assert.assertNotNull(dataFetcherInvocationCount.get(field));
notCompletedCount++;
} else if (dFRT == DataFetcherResultType.MATERIALIZED) {
materializedInvokeCount += Assert.assertNotNull(dataFetcherInvocationCount.get(field));
materializedCount++;
} else {
Assert.assertShouldNeverHappen();
}
}
LinkedHashMap<String, Object> dFRTinfo = new LinkedHashMap<>(3);
dFRTinfo.put(DataFetcherResultType.COMPLETABLE_FUTURE_COMPLETED.name(), createCountMap(completedCount, completedInvokeCount));
dFRTinfo.put(DataFetcherResultType.COMPLETABLE_FUTURE_NOT_COMPLETED.name(), createCountMap(notCompletedCount, notCompletedInvokeCount));
dFRTinfo.put(DataFetcherResultType.MATERIALIZED.name(), createCountMap(materializedCount, materializedInvokeCount));
result.put("dataFetcherResultTypes", dFRTinfo);
return result;
}
private LinkedHashMap<String, Integer> createCountMap(int count, int invocations) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>(2);
map.put("count", count);
map.put("invocations", invocations);
return map;
}
public List<Map<String, Object>> getDispatchEventsAsMap() {
List<Map<String, Object>> result = new ArrayList<>();
for (DispatchEvent event : dispatchEvents) {
Map<String, Object> eventMap = new LinkedHashMap<>();
eventMap.put("type", event.getType().name());
eventMap.put("dataLoader", event.getDataLoaderName());
eventMap.put("level", event.getLevel());
eventMap.put("keyCount", event.getKeyCount());
result.add(eventMap);
}
return result;
}
@Override
public String toString() {
return "ProfilerResult" + shortSummaryMap();
}
}