|
| 1 | +package benchmark; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableList; |
| 4 | +import graphql.ExecutionInput; |
| 5 | +import graphql.ExecutionResult; |
| 6 | +import graphql.GraphQL; |
| 7 | +import graphql.schema.DataFetcher; |
| 8 | +import graphql.schema.DataFetchingEnvironment; |
| 9 | +import graphql.schema.GraphQLSchema; |
| 10 | +import graphql.schema.idl.RuntimeWiring; |
| 11 | +import graphql.schema.idl.SchemaGenerator; |
| 12 | +import graphql.schema.idl.SchemaParser; |
| 13 | +import graphql.schema.idl.TypeDefinitionRegistry; |
| 14 | +import org.openjdk.jmh.annotations.Benchmark; |
| 15 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 16 | +import org.openjdk.jmh.annotations.Fork; |
| 17 | +import org.openjdk.jmh.annotations.Level; |
| 18 | +import org.openjdk.jmh.annotations.Measurement; |
| 19 | +import org.openjdk.jmh.annotations.Mode; |
| 20 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 21 | +import org.openjdk.jmh.annotations.Param; |
| 22 | +import org.openjdk.jmh.annotations.Scope; |
| 23 | +import org.openjdk.jmh.annotations.Setup; |
| 24 | +import org.openjdk.jmh.annotations.State; |
| 25 | +import org.openjdk.jmh.annotations.TearDown; |
| 26 | +import org.openjdk.jmh.annotations.Warmup; |
| 27 | +import org.openjdk.jmh.profile.GCProfiler; |
| 28 | +import org.openjdk.jmh.runner.Runner; |
| 29 | +import org.openjdk.jmh.runner.options.Options; |
| 30 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 31 | + |
| 32 | +import java.util.List; |
| 33 | +import java.util.concurrent.CompletableFuture; |
| 34 | +import java.util.concurrent.ExecutorService; |
| 35 | +import java.util.concurrent.Executors; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.concurrent.atomic.AtomicInteger; |
| 38 | +import java.util.function.Supplier; |
| 39 | + |
| 40 | +import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring; |
| 41 | + |
| 42 | +/** |
| 43 | + * This benchmark is an attempt to have a more complex query that involves async and sync work together |
| 44 | + * along with multiple threads happening. |
| 45 | + * <p> |
| 46 | + * It can also be run in a forever mode say if you want to connect a profiler to it say |
| 47 | + */ |
| 48 | +@State(Scope.Benchmark) |
| 49 | +@Warmup(iterations = 2, time = 5) |
| 50 | +@Measurement(iterations = 2) |
| 51 | +@Fork(2) |
| 52 | +public class ComplexQueryBenchmark { |
| 53 | + |
| 54 | + @Param({"5", "20", "100"}) |
| 55 | + int howManyItems = 5; |
| 56 | + int howLongToSleep = 5; |
| 57 | + int howManyQueries = 50; |
| 58 | + int howManyQueryThreads = 10; |
| 59 | + int howManyFetcherThreads = 10; |
| 60 | + |
| 61 | + ExecutorService queryExecutorService; |
| 62 | + ExecutorService fetchersExecutorService; |
| 63 | + GraphQL graphQL; |
| 64 | + volatile boolean shutDown; |
| 65 | + |
| 66 | + @Setup(Level.Trial) |
| 67 | + public void setUp() { |
| 68 | + shutDown = false; |
| 69 | + queryExecutorService = Executors.newFixedThreadPool(howManyQueryThreads); |
| 70 | + fetchersExecutorService = Executors.newFixedThreadPool(howManyFetcherThreads); |
| 71 | + graphQL = buildGraphQL(); |
| 72 | + } |
| 73 | + |
| 74 | + @TearDown(Level.Trial) |
| 75 | + public void tearDown() { |
| 76 | + shutDown = true; |
| 77 | + queryExecutorService.shutdownNow(); |
| 78 | + fetchersExecutorService.shutdownNow(); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @Benchmark |
| 83 | + @BenchmarkMode(Mode.Throughput) |
| 84 | + @OutputTimeUnit(TimeUnit.SECONDS) |
| 85 | + public Object benchMarkSimpleQueriesThroughput() { |
| 86 | + return runManyQueriesToCompletion(); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + public static void main(String[] args) throws Exception { |
| 91 | + // just to make sure it's all valid before testing |
| 92 | + runAtStartup(); |
| 93 | + |
| 94 | + Options opt = new OptionsBuilder() |
| 95 | + .include("benchmark.ComplexQueryBenchmark") |
| 96 | + .addProfiler(GCProfiler.class) |
| 97 | + .build(); |
| 98 | + |
| 99 | + new Runner(opt).run(); |
| 100 | + } |
| 101 | + |
| 102 | + @SuppressWarnings({"ConstantValue", "LoopConditionNotUpdatedInsideLoop"}) |
| 103 | + private static void runAtStartup() { |
| 104 | + // set this to true if you want to hook in profiler say to a forever running JVM |
| 105 | + boolean forever = Boolean.getBoolean("forever"); |
| 106 | + |
| 107 | + long then = System.currentTimeMillis(); |
| 108 | + System.out.printf("Running initial code before starting the benchmark in forever=%b mode \n", forever); |
| 109 | + ComplexQueryBenchmark complexQueryBenchmark = new ComplexQueryBenchmark(); |
| 110 | + complexQueryBenchmark.setUp(); |
| 111 | + do { |
| 112 | + System.out.print("Running queries....\n"); |
| 113 | + complexQueryBenchmark.howManyItems = 100; |
| 114 | + complexQueryBenchmark.runManyQueriesToCompletion(); |
| 115 | + } while (forever); |
| 116 | + complexQueryBenchmark.tearDown(); |
| 117 | + |
| 118 | + System.out.printf("This took %d millis\n", System.currentTimeMillis() - then); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + @SuppressWarnings("UnnecessaryLocalVariable") |
| 123 | + private Void runManyQueriesToCompletion() { |
| 124 | + CompletableFuture<?>[] cfs = new CompletableFuture[howManyQueries]; |
| 125 | + for (int i = 0; i < howManyQueries; i++) { |
| 126 | + cfs[i] = CompletableFuture.supplyAsync(() -> executeQuery(howManyItems, howLongToSleep), queryExecutorService).thenCompose(cf -> cf); |
| 127 | + } |
| 128 | + Void result = CompletableFuture.allOf(cfs).join(); |
| 129 | + return result; |
| 130 | + } |
| 131 | + |
| 132 | + public CompletableFuture<ExecutionResult> executeQuery(int howMany, int howLong) { |
| 133 | + String fields = "id name f1 f2 f3 f4 f5 f6 f7 f8 f9 f10"; |
| 134 | + String query = "query q {" |
| 135 | + + String.format("shops(howMany : %d) { %s departments( howMany : %d) { %s products(howMany : %d) { %s }}}\n" |
| 136 | + , howMany, fields, 10, fields, 5, fields) |
| 137 | + + String.format("expensiveShops(howMany : %d howLong : %d) { %s expensiveDepartments( howMany : %d howLong : %d) { %s expensiveProducts(howMany : %d howLong : %d) { %s }}}\n" |
| 138 | + , howMany, howLong, fields, 10, howLong, fields, 5, howLong, fields) |
| 139 | + + "}"; |
| 140 | + return graphQL.executeAsync(ExecutionInput.newExecutionInput(query).build()); |
| 141 | + } |
| 142 | + |
| 143 | + private GraphQL buildGraphQL() { |
| 144 | + TypeDefinitionRegistry definitionRegistry = new SchemaParser().parse(BenchmarkUtils.loadResource("storesanddepartments.graphqls")); |
| 145 | + |
| 146 | + DataFetcher<?> shopsDF = env -> mkHowManyThings(env.getArgument("howMany")); |
| 147 | + DataFetcher<?> expensiveShopsDF = env -> supplyAsync(() -> sleepAndReturnThings(env)); |
| 148 | + DataFetcher<?> departmentsDF = env -> mkHowManyThings(env.getArgument("howMany")); |
| 149 | + DataFetcher<?> expensiveDepartmentsDF = env -> supplyAsyncListItems(env, () -> sleepAndReturnThings(env)); |
| 150 | + DataFetcher<?> productsDF = env -> mkHowManyThings(env.getArgument("howMany")); |
| 151 | + DataFetcher<?> expensiveProductsDF = env -> supplyAsyncListItems(env, () -> sleepAndReturnThings(env)); |
| 152 | + |
| 153 | + RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring() |
| 154 | + .type(newTypeWiring("Query") |
| 155 | + .dataFetcher("shops", shopsDF) |
| 156 | + .dataFetcher("expensiveShops", expensiveShopsDF)) |
| 157 | + .type(newTypeWiring("Shop") |
| 158 | + .dataFetcher("departments", departmentsDF) |
| 159 | + .dataFetcher("expensiveDepartments", expensiveDepartmentsDF)) |
| 160 | + .type(newTypeWiring("Department") |
| 161 | + .dataFetcher("products", productsDF) |
| 162 | + .dataFetcher("expensiveProducts", expensiveProductsDF)) |
| 163 | + .build(); |
| 164 | + |
| 165 | + GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(definitionRegistry, runtimeWiring); |
| 166 | + |
| 167 | + return GraphQL.newGraphQL(graphQLSchema).build(); |
| 168 | + } |
| 169 | + |
| 170 | + private <T> CompletableFuture<T> supplyAsyncListItems(DataFetchingEnvironment environment, Supplier<T> codeToRun) { |
| 171 | + return supplyAsync(codeToRun); |
| 172 | + } |
| 173 | + |
| 174 | + private <T> CompletableFuture<T> supplyAsync(Supplier<T> codeToRun) { |
| 175 | + if (!shutDown) { |
| 176 | + //logEvery(100, "async fetcher"); |
| 177 | + return CompletableFuture.supplyAsync(codeToRun, fetchersExecutorService); |
| 178 | + } else { |
| 179 | + // if we have shutdown - get on with it, so we shut down quicker |
| 180 | + return CompletableFuture.completedFuture(codeToRun.get()); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + private List<IdAndNamedThing> sleepAndReturnThings(DataFetchingEnvironment env) { |
| 185 | + // by sleeping, we hope to cause the objects to stay longer in GC land and hence have a longer lifecycle |
| 186 | + // then a simple stack say or young gen gc. I don't know this will work, but I am trying it |
| 187 | + // to represent work that takes some tie to complete |
| 188 | + sleep(env.getArgument("howLong")); |
| 189 | + return mkHowManyThings(env.getArgument("howMany")); |
| 190 | + } |
| 191 | + |
| 192 | + private void sleep(Integer howLong) { |
| 193 | + if (howLong > 0) { |
| 194 | + try { |
| 195 | + Thread.sleep(howLong); |
| 196 | + } catch (InterruptedException e) { |
| 197 | + throw new RuntimeException(e); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + AtomicInteger logCount = new AtomicInteger(); |
| 203 | + private void logEvery(int every, String s) { |
| 204 | + int count = logCount.getAndIncrement(); |
| 205 | + if (count == 0 || count % every == 0) { |
| 206 | + System.out.println("\t" + count + "\t" + s); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private List<IdAndNamedThing> mkHowManyThings(Integer howMany) { |
| 211 | + ImmutableList.Builder<IdAndNamedThing> builder = ImmutableList.builder(); |
| 212 | + for (int i = 0; i < howMany; i++) { |
| 213 | + builder.add(new IdAndNamedThing(i)); |
| 214 | + } |
| 215 | + return builder.build(); |
| 216 | + } |
| 217 | + |
| 218 | + @SuppressWarnings("unused") |
| 219 | + static class IdAndNamedThing { |
| 220 | + private final int i; |
| 221 | + |
| 222 | + public IdAndNamedThing(int i) { |
| 223 | + this.i = i; |
| 224 | + } |
| 225 | + |
| 226 | + public String getId() { |
| 227 | + return "id" + i; |
| 228 | + } |
| 229 | + |
| 230 | + public String getName() { |
| 231 | + return "name" + i; |
| 232 | + } |
| 233 | + |
| 234 | + public String getF1() { |
| 235 | + return "f1" + i; |
| 236 | + } |
| 237 | + |
| 238 | + public String getF2() { |
| 239 | + return "f2" + i; |
| 240 | + } |
| 241 | + |
| 242 | + public String getF3() { |
| 243 | + return "f3" + i; |
| 244 | + } |
| 245 | + |
| 246 | + public String getF4() { |
| 247 | + return "f4" + i; |
| 248 | + } |
| 249 | + |
| 250 | + public String getF5() { |
| 251 | + return "f5" + i; |
| 252 | + } |
| 253 | + |
| 254 | + public String getF6() { |
| 255 | + return "f6" + i; |
| 256 | + } |
| 257 | + |
| 258 | + public String getF7() { |
| 259 | + return "f7" + i; |
| 260 | + } |
| 261 | + |
| 262 | + public String getF8() { |
| 263 | + return "f8" + i; |
| 264 | + } |
| 265 | + |
| 266 | + public String getF9() { |
| 267 | + return "f9" + i; |
| 268 | + } |
| 269 | + |
| 270 | + public String getF10() { |
| 271 | + return "f10" + i; |
| 272 | + } |
| 273 | + } |
| 274 | +} |
0 commit comments