Skip to content

Commit 535eb8e

Browse files
committed
counting wrapped trivial data fetchers
1 parent ca618b4 commit 535eb8e

File tree

6 files changed

+31
-26
lines changed

6 files changed

+31
-26
lines changed

src/main/java/graphql/Profiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ default void dataLoaderUsed(String dataLoaderName) {
2626

2727
}
2828

29-
default void fieldFetched(Object fetchedObject, DataFetcher<?> dataFetcher, ResultPath path) {
29+
default void fieldFetched(Object fetchedObject, DataFetcher<?> originalDataFetcher, DataFetcher<?> dataFetcher, ResultPath path) {
3030

3131
}
3232

src/main/java/graphql/ProfilerImpl.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ private void collectInstrumentationClasses(List<String> result, Instrumentation
5656
}
5757
}
5858

59+
5960
@Override
60-
public void fieldFetched(Object fetchedObject, DataFetcher<?> dataFetcher, ResultPath path) {
61+
public void fieldFetched(Object fetchedObject, DataFetcher<?> originalDataFetcher, DataFetcher<?> dataFetcher, ResultPath path) {
6162
String key = "/" + String.join("/", path.getKeysOnly());
6263
profilerResult.addFieldFetched(key);
6364
profilerResult.incrementDataFetcherInvocationCount(key);
6465
ProfilerResult.DataFetcherType dataFetcherType;
6566
if (dataFetcher instanceof PropertyDataFetcher || dataFetcher instanceof SingletonPropertyDataFetcher) {
66-
dataFetcherType = ProfilerResult.DataFetcherType.PROPERTY_DATA_FETCHER;
67+
dataFetcherType = ProfilerResult.DataFetcherType.TRIVIAL_DATA_FETCHER;
68+
} else if (originalDataFetcher instanceof PropertyDataFetcher || originalDataFetcher instanceof SingletonPropertyDataFetcher) {
69+
dataFetcherType = ProfilerResult.DataFetcherType.WRAPPED_TRIVIAL_DATA_FETCHER;
6770
} else {
6871
dataFetcherType = ProfilerResult.DataFetcherType.CUSTOM;
6972
// we only record the type of the result if it is not a PropertyDataFetcher

src/main/java/graphql/ProfilerResult.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public class ProfilerResult {
2828
private long endTime;
2929
private long engineTotalRunningTime;
3030
private final AtomicInteger totalDataFetcherInvocations = new AtomicInteger();
31-
private final AtomicInteger totalPropertyDataFetcherInvocations = new AtomicInteger();
31+
private final AtomicInteger totalTrivialDataFetcherInvocations = new AtomicInteger();
32+
private final AtomicInteger totalWrappedTrivialDataFetcherInvocations = new AtomicInteger();
3233

3334
// this is the count of how many times a data loader was invoked per data loader name
3435
private final Map<String, Integer> dataLoaderLoadInvocations = new ConcurrentHashMap<>();
@@ -108,7 +109,8 @@ public String toString() {
108109
}
109110

110111
public enum DataFetcherType {
111-
PROPERTY_DATA_FETCHER,
112+
WRAPPED_TRIVIAL_DATA_FETCHER,
113+
TRIVIAL_DATA_FETCHER,
112114
CUSTOM
113115
}
114116

@@ -130,8 +132,10 @@ void setDataLoaderChainingEnabled(boolean dataLoaderChainingEnabled) {
130132
void setDataFetcherType(String key, DataFetcherType dataFetcherType) {
131133
dataFetcherTypeMap.putIfAbsent(key, dataFetcherType);
132134
totalDataFetcherInvocations.incrementAndGet();
133-
if (dataFetcherType == DataFetcherType.PROPERTY_DATA_FETCHER) {
134-
totalPropertyDataFetcherInvocations.incrementAndGet();
135+
if (dataFetcherType == DataFetcherType.TRIVIAL_DATA_FETCHER) {
136+
totalTrivialDataFetcherInvocations.incrementAndGet();
137+
} else if (dataFetcherType == DataFetcherType.WRAPPED_TRIVIAL_DATA_FETCHER) {
138+
totalWrappedTrivialDataFetcherInvocations.incrementAndGet();
135139
}
136140
}
137141

@@ -199,10 +203,10 @@ public Set<String> getCustomDataFetcherFields() {
199203
return result;
200204
}
201205

202-
public Set<String> getPropertyDataFetcherFields() {
206+
public Set<String> getTrivialDataFetcherFields() {
203207
Set<String> result = new LinkedHashSet<>(fieldsFetched);
204208
for (String field : fieldsFetched) {
205-
if (dataFetcherTypeMap.get(field) == DataFetcherType.PROPERTY_DATA_FETCHER) {
209+
if (dataFetcherTypeMap.get(field) == DataFetcherType.TRIVIAL_DATA_FETCHER) {
206210
result.add(field);
207211
}
208212
}
@@ -214,12 +218,12 @@ public int getTotalDataFetcherInvocations() {
214218
return totalDataFetcherInvocations.get();
215219
}
216220

217-
public int getTotalPropertyDataFetcherInvocations() {
218-
return totalPropertyDataFetcherInvocations.get();
221+
public int getTotalTrivialDataFetcherInvocations() {
222+
return totalTrivialDataFetcherInvocations.get();
219223
}
220224

221225
public int getTotalCustomDataFetcherInvocations() {
222-
return totalDataFetcherInvocations.get() - totalPropertyDataFetcherInvocations.get();
226+
return totalDataFetcherInvocations.get() - totalTrivialDataFetcherInvocations.get() - totalWrappedTrivialDataFetcherInvocations.get();
223227
}
224228

225229
public long getStartTime() {
@@ -272,7 +276,8 @@ public Map<String, Object> shortSummaryMap() {
272276
result.put("totalRunTime", (endTime - startTime) + "(" + (endTime - startTime) / 1_000_000 + "ms)");
273277
result.put("engineTotalRunningTime", engineTotalRunningTime + "(" + engineTotalRunningTime / 1_000_000 + "ms)");
274278
result.put("totalDataFetcherInvocations", totalDataFetcherInvocations);
275-
result.put("totalPropertyDataFetcherInvocations", totalPropertyDataFetcherInvocations);
279+
result.put("totalTrivialDataFetcherInvocations", totalTrivialDataFetcherInvocations);
280+
result.put("totalWrappedTrivialDataFetcherInvocations", totalWrappedTrivialDataFetcherInvocations);
276281
result.put("fieldsFetchedCount", fieldsFetched.size());
277282
result.put("dataLoaderChainingEnabled", dataLoaderChainingEnabled);
278283
result.put("dataLoaderLoadInvocations", dataLoaderLoadInvocations);

src/main/java/graphql/execution/DataLoaderDispatchStrategy.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ default void fieldFetched(ExecutionContext executionContext,
5757
}
5858

5959

60-
default DataFetcher<?> modifyDataFetcher(DataFetcher<?> dataFetcher) {
61-
return dataFetcher;
62-
}
6360

6461
default void newSubscriptionExecution(FieldValueInfo fieldValueInfo, AlternativeCallContext alternativeCallContext) {
6562

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,18 +447,17 @@ private Object fetchField(GraphQLFieldDefinition fieldDef, ExecutionContext exec
447447
});
448448

449449
GraphQLCodeRegistry codeRegistry = executionContext.getGraphQLSchema().getCodeRegistry();
450-
DataFetcher<?> dataFetcher = codeRegistry.getDataFetcher(parentType, fieldDef);
450+
DataFetcher<?> originalDataFetcher = codeRegistry.getDataFetcher(parentType, fieldDef);
451451

452452
Instrumentation instrumentation = executionContext.getInstrumentation();
453453

454-
InstrumentationFieldFetchParameters instrumentationFieldFetchParams = new InstrumentationFieldFetchParameters(executionContext, dataFetchingEnvironment, parameters, dataFetcher instanceof TrivialDataFetcher);
454+
InstrumentationFieldFetchParameters instrumentationFieldFetchParams = new InstrumentationFieldFetchParameters(executionContext, dataFetchingEnvironment, parameters, originalDataFetcher instanceof TrivialDataFetcher);
455455
FieldFetchingInstrumentationContext fetchCtx = FieldFetchingInstrumentationContext.nonNullCtx(instrumentation.beginFieldFetching(instrumentationFieldFetchParams,
456456
executionContext.getInstrumentationState())
457457
);
458458

459-
dataFetcher = instrumentation.instrumentDataFetcher(dataFetcher, instrumentationFieldFetchParams, executionContext.getInstrumentationState());
460-
dataFetcher = executionContext.getDataLoaderDispatcherStrategy().modifyDataFetcher(dataFetcher);
461-
Object fetchedObject = invokeDataFetcher(executionContext, parameters, fieldDef, dataFetchingEnvironment, dataFetcher);
459+
DataFetcher<?> dataFetcher = instrumentation.instrumentDataFetcher(originalDataFetcher, instrumentationFieldFetchParams, executionContext.getInstrumentationState());
460+
Object fetchedObject = invokeDataFetcher(executionContext, parameters, fieldDef, dataFetchingEnvironment, originalDataFetcher, dataFetcher);
462461
executionContext.getDataLoaderDispatcherStrategy().fieldFetched(executionContext, parameters, dataFetcher, fetchedObject, dataFetchingEnvironment);
463462
fetchCtx.onDispatched();
464463
fetchCtx.onFetchedValue(fetchedObject);
@@ -497,7 +496,7 @@ private Object fetchField(GraphQLFieldDefinition fieldDef, ExecutionContext exec
497496
* ExecutionContext is not used in the method, but the java agent uses it, so it needs to be present
498497
*/
499498
@SuppressWarnings("unused")
500-
private Object invokeDataFetcher(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLFieldDefinition fieldDef, Supplier<DataFetchingEnvironment> dataFetchingEnvironment, DataFetcher<?> dataFetcher) {
499+
private Object invokeDataFetcher(ExecutionContext executionContext, ExecutionStrategyParameters parameters, GraphQLFieldDefinition fieldDef, Supplier<DataFetchingEnvironment> dataFetchingEnvironment, DataFetcher<?> originalDataFetcher, DataFetcher<?> dataFetcher) {
501500
Object fetchedValue;
502501
try {
503502
Object fetchedValueRaw;
@@ -506,7 +505,7 @@ private Object invokeDataFetcher(ExecutionContext executionContext, ExecutionStr
506505
} else {
507506
fetchedValueRaw = dataFetcher.get(dataFetchingEnvironment.get());
508507
}
509-
executionContext.getProfiler().fieldFetched(fetchedValueRaw, dataFetcher, parameters.getPath());
508+
executionContext.getProfiler().fieldFetched(fetchedValueRaw, originalDataFetcher, dataFetcher, parameters.getPath());
510509
fetchedValue = Async.toCompletableFutureOrMaterializedObject(fetchedValueRaw);
511510
} catch (Exception e) {
512511
fetchedValue = Async.exceptionallyCompletedFuture(e);

src/test/groovy/graphql/ProfilerTest.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ class ProfilerTest extends Specification {
106106

107107
then:
108108
profilerResult.getTotalDataFetcherInvocations() == 3
109-
profilerResult.getTotalPropertyDataFetcherInvocations() == 1
110-
profilerResult.getTotalCustomDataFetcherInvocations() == 2
109+
profilerResult.getTotalTrivialDataFetcherInvocations() == 1
110+
profilerResult.getTotalTrivialDataFetcherInvocations() == 1
111+
profilerResult.getTotalCustomDataFetcherInvocations() == 1
111112
}
112113

113114

@@ -308,7 +309,7 @@ class ProfilerTest extends Specification {
308309
profilerResult.getFieldsFetched() == ["/foo", "/foo/bar", "/foo/id"] as Set
309310
profilerResult.getTotalDataFetcherInvocations() == 7
310311
profilerResult.getTotalCustomDataFetcherInvocations() == 4
311-
profilerResult.getTotalPropertyDataFetcherInvocations() == 3
312+
profilerResult.getTotalTrivialDataFetcherInvocations() == 3
312313
}
313314

314315
def "records timing"() {

0 commit comments

Comments
 (0)