forked from graphql-java/graphql-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterBenchmark.java
More file actions
161 lines (142 loc) · 4.79 KB
/
Copy pathTwitterBenchmark.java
File metadata and controls
161 lines (142 loc) · 4.79 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
package benchmark;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import com.github.javafaker.Code;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.execution.ExecutionStepInfo;
import graphql.execution.instrumentation.tracing.TracingInstrumentation;
import graphql.execution.preparsed.PreparsedDocumentEntry;
import graphql.execution.preparsed.PreparsedDocumentProvider;
import graphql.execution.preparsed.persisted.InMemoryPersistedQueryCache;
import graphql.execution.preparsed.persisted.PersistedQueryCache;
import graphql.execution.preparsed.persisted.PersistedQuerySupport;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetcherFactory;
import graphql.schema.DataFetcherFactoryEnvironment;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.FieldCoordinates;
import graphql.schema.GraphQLCodeRegistry;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLTypeReference;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaGeneratorHelper;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import static graphql.Scalars.GraphQLString;
import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring;
@Warmup(iterations = 8, time = 10)
@Measurement(iterations = 25, time = 10)
@Fork(1)
@Threads(1)
public class TwitterBenchmark {
private static final int BREADTH = 150;
private static final int DEPTH = 150;
static String query = mkQuery();
static Object queryId = "QUERY_ID";
static GraphQL graphQL = buildGraphQL();
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public void execute(Blackhole bh) {
bh.consume(execute());
}
private static ExecutionResult execute() {
return graphQL.execute(query);
}
public static String mkQuery() {
StringBuilder sb = new StringBuilder();
sb.append("{");
for (int d=1; d <= DEPTH; d++) {
for (int b=1; b <= BREADTH; b++) {
sb.append("leaf_");
sb.append(b);
sb.append(" ");
}
if (d < DEPTH) {
sb.append("branch { ");
}
}
for (int d=1; d <= DEPTH; d++) {
sb.append("}");
}
return sb.toString();
}
private static GraphQL buildGraphQL() {
List<GraphQLFieldDefinition> leafFields = new ArrayList<>(BREADTH);
for (int i = 1; i <= BREADTH; i++) {
leafFields.add(
GraphQLFieldDefinition.newFieldDefinition()
.name("leaf_" + i)
.type(GraphQLString)
.build()
);
}
GraphQLObjectType branchType = GraphQLObjectType.newObject()
.name("Branch")
.fields(leafFields)
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("branch")
.type(GraphQLTypeReference.typeRef("Branch")))
.build();
DataFetcher<Object> simpleFetcher = env -> env.getField().getName();
GraphQLCodeRegistry codeReg = GraphQLCodeRegistry.newCodeRegistry()
.defaultDataFetcher(
new DataFetcherFactory<Object>() {
@Override
public DataFetcher<Object> get(DataFetcherFactoryEnvironment environment) {
return simpleFetcher;
}
}
)
.build();
GraphQLSchema graphQLSchema = GraphQLSchema.newSchema()
.query(branchType)
.codeRegistry(codeReg)
.build();
return GraphQL
.newGraphQL(graphQLSchema)
.preparsedDocumentProvider(
new PersistedQuery(
InMemoryPersistedQueryCache
.newInMemoryPersistedQueryCache()
.addQuery(queryId, query)
.build()
)
)
.build();
}
static class PersistedQuery extends PersistedQuerySupport {
public PersistedQuery(PersistedQueryCache persistedQueryCache) {
super(persistedQueryCache);
}
@Override
protected Optional<Object> getPersistedQueryId(ExecutionInput executionInput) {
return Optional.of(queryId);
}
}
public static void main(String[] args) {
ExecutionResult result = execute();
int i = 0;
}
}