-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ComplexQueryBenchmark.java
275 lines (227 loc) · 9.45 KB
/
ComplexQueryBenchmark.java
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
package benchmark;
import com.google.common.collect.ImmutableList;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.profile.GCProfiler;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring;
/**
* This benchmark is an attempt to have a more complex query that involves async and sync work together
* along with multiple threads happening.
* <p>
* It can also be run in a forever mode say if you want to connect a profiler to it say
*/
@State(Scope.Benchmark)
@Warmup(iterations = 2, time = 5)
@Measurement(iterations = 2)
@Fork(2)
public class ComplexQueryBenchmark {
@Param({"5", "20", "100"})
int howManyItems = 5;
int howLongToSleep = 5;
int howManyQueries = 50;
int howManyQueryThreads = 10;
int howManyFetcherThreads = 10;
ExecutorService queryExecutorService;
ExecutorService fetchersExecutorService;
GraphQL graphQL;
volatile boolean shutDown;
@Setup(Level.Trial)
public void setUp() {
shutDown = false;
queryExecutorService = Executors.newFixedThreadPool(howManyQueryThreads);
fetchersExecutorService = Executors.newFixedThreadPool(howManyFetcherThreads);
graphQL = buildGraphQL();
}
@TearDown(Level.Trial)
public void tearDown() {
shutDown = true;
queryExecutorService.shutdownNow();
fetchersExecutorService.shutdownNow();
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public Object benchMarkSimpleQueriesThroughput() {
return runManyQueriesToCompletion();
}
public static void main(String[] args) throws Exception {
// just to make sure it's all valid before testing
runAtStartup();
Options opt = new OptionsBuilder()
.include("benchmark.ComplexQueryBenchmark")
.addProfiler(GCProfiler.class)
.build();
new Runner(opt).run();
}
@SuppressWarnings({"ConstantValue", "LoopConditionNotUpdatedInsideLoop"})
private static void runAtStartup() {
ComplexQueryBenchmark complexQueryBenchmark = new ComplexQueryBenchmark();
complexQueryBenchmark.howManyQueries = 5;
complexQueryBenchmark.howManyItems = 10;
BenchmarkUtils.runInToolingForSomeTimeThenExit(
complexQueryBenchmark::setUp,
complexQueryBenchmark::runManyQueriesToCompletion,
complexQueryBenchmark::tearDown
);
}
@SuppressWarnings("UnnecessaryLocalVariable")
private Void runManyQueriesToCompletion() {
CompletableFuture<?>[] cfs = new CompletableFuture[howManyQueries];
for (int i = 0; i < howManyQueries; i++) {
cfs[i] = CompletableFuture.supplyAsync(() -> executeQuery(howManyItems, howLongToSleep), queryExecutorService).thenCompose(cf -> cf);
}
Void result = CompletableFuture.allOf(cfs).join();
return result;
}
public CompletableFuture<ExecutionResult> executeQuery(int howMany, int howLong) {
String fields = "id name f1 f2 f3 f4 f5 f6 f7 f8 f9 f10";
String query = "query q {"
+ String.format("shops(howMany : %d) { %s departments( howMany : %d) { %s products(howMany : %d) { %s }}}\n"
, howMany, fields, 10, fields, 5, fields)
+ String.format("expensiveShops(howMany : %d howLong : %d) { %s expensiveDepartments( howMany : %d howLong : %d) { %s expensiveProducts(howMany : %d howLong : %d) { %s }}}\n"
, howMany, howLong, fields, 10, howLong, fields, 5, howLong, fields)
+ "}";
return graphQL.executeAsync(ExecutionInput.newExecutionInput(query).build());
}
private GraphQL buildGraphQL() {
TypeDefinitionRegistry definitionRegistry = new SchemaParser().parse(BenchmarkUtils.loadResource("storesanddepartments.graphqls"));
DataFetcher<?> shopsDF = env -> mkHowManyThings(env.getArgument("howMany"));
DataFetcher<?> expensiveShopsDF = env -> supplyAsync(() -> sleepAndReturnThings(env));
DataFetcher<?> departmentsDF = env -> mkHowManyThings(env.getArgument("howMany"));
DataFetcher<?> expensiveDepartmentsDF = env -> supplyAsyncListItems(env, () -> sleepAndReturnThings(env));
DataFetcher<?> productsDF = env -> mkHowManyThings(env.getArgument("howMany"));
DataFetcher<?> expensiveProductsDF = env -> supplyAsyncListItems(env, () -> sleepAndReturnThings(env));
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
.dataFetcher("shops", shopsDF)
.dataFetcher("expensiveShops", expensiveShopsDF))
.type(newTypeWiring("Shop")
.dataFetcher("departments", departmentsDF)
.dataFetcher("expensiveDepartments", expensiveDepartmentsDF))
.type(newTypeWiring("Department")
.dataFetcher("products", productsDF)
.dataFetcher("expensiveProducts", expensiveProductsDF))
.build();
GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(definitionRegistry, runtimeWiring);
return GraphQL.newGraphQL(graphQLSchema).build();
}
private <T> CompletableFuture<T> supplyAsyncListItems(DataFetchingEnvironment environment, Supplier<T> codeToRun) {
return supplyAsync(codeToRun);
}
private <T> CompletableFuture<T> supplyAsync(Supplier<T> codeToRun) {
if (!shutDown) {
//logEvery(100, "async fetcher");
return CompletableFuture.supplyAsync(codeToRun, fetchersExecutorService);
} else {
// if we have shutdown - get on with it, so we shut down quicker
return CompletableFuture.completedFuture(codeToRun.get());
}
}
private List<IdAndNamedThing> sleepAndReturnThings(DataFetchingEnvironment env) {
// by sleeping, we hope to cause the objects to stay longer in GC land and hence have a longer lifecycle
// then a simple stack say or young gen gc. I don't know this will work, but I am trying it
// to represent work that takes some tie to complete
sleep(env.getArgument("howLong"));
return mkHowManyThings(env.getArgument("howMany"));
}
private void sleep(Integer howLong) {
if (howLong > 0) {
try {
Thread.sleep(howLong);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
AtomicInteger logCount = new AtomicInteger();
private void logEvery(int every, String s) {
int count = logCount.getAndIncrement();
if (count == 0 || count % every == 0) {
System.out.println("\t" + count + "\t" + s);
}
}
private List<IdAndNamedThing> mkHowManyThings(Integer howMany) {
ImmutableList.Builder<IdAndNamedThing> builder = ImmutableList.builder();
for (int i = 0; i < howMany; i++) {
builder.add(new IdAndNamedThing(i));
}
return builder.build();
}
@SuppressWarnings("unused")
static class IdAndNamedThing {
private final int i;
public IdAndNamedThing(int i) {
this.i = i;
}
public String getId() {
return "id" + i;
}
public String getName() {
return "name" + i;
}
public String getF1() {
return "f1" + i;
}
public String getF2() {
return "f2" + i;
}
public String getF3() {
return "f3" + i;
}
public String getF4() {
return "f4" + i;
}
public String getF5() {
return "f5" + i;
}
public String getF6() {
return "f6" + i;
}
public String getF7() {
return "f7" + i;
}
public String getF8() {
return "f8" + i;
}
public String getF9() {
return "f9" + i;
}
public String getF10() {
return "f10" + i;
}
}
}