|
| 1 | +package benchmark; |
| 2 | + |
| 3 | +import graphql.execution.CoercedVariables; |
| 4 | +import graphql.language.Document; |
| 5 | +import graphql.normalized.ExecutableNormalizedOperation; |
| 6 | +import graphql.normalized.ExecutableNormalizedOperationFactory; |
| 7 | +import graphql.parser.Parser; |
| 8 | +import graphql.schema.GraphQLSchema; |
| 9 | +import graphql.schema.idl.SchemaGenerator; |
| 10 | +import org.openjdk.jmh.annotations.Benchmark; |
| 11 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 12 | +import org.openjdk.jmh.annotations.Fork; |
| 13 | +import org.openjdk.jmh.annotations.Level; |
| 14 | +import org.openjdk.jmh.annotations.Measurement; |
| 15 | +import org.openjdk.jmh.annotations.Mode; |
| 16 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 17 | +import org.openjdk.jmh.annotations.Param; |
| 18 | +import org.openjdk.jmh.annotations.Scope; |
| 19 | +import org.openjdk.jmh.annotations.Setup; |
| 20 | +import org.openjdk.jmh.annotations.State; |
| 21 | +import org.openjdk.jmh.annotations.Warmup; |
| 22 | +import org.openjdk.jmh.runner.Runner; |
| 23 | +import org.openjdk.jmh.runner.RunnerException; |
| 24 | +import org.openjdk.jmh.runner.options.Options; |
| 25 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 26 | + |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | + |
| 29 | +import static graphql.normalized.ExecutableNormalizedOperationFactory.*; |
| 30 | + |
| 31 | +@State(Scope.Benchmark) |
| 32 | +@Warmup(iterations = 2, time = 5) |
| 33 | +@Measurement(iterations = 3, time = 5) |
| 34 | +@Fork(2) |
| 35 | +public class ENFBenchmarkDeepIntrospection { |
| 36 | + |
| 37 | + @Param({"2", "10", "20"}) |
| 38 | + int howDeep = 2; |
| 39 | + |
| 40 | + String query = ""; |
| 41 | + |
| 42 | + GraphQLSchema schema; |
| 43 | + Document document; |
| 44 | + |
| 45 | + @Setup(Level.Trial) |
| 46 | + public void setUp() { |
| 47 | + String schemaString = BenchmarkUtils.loadResource("large-schema-2.graphqls"); |
| 48 | + schema = SchemaGenerator.createdMockedSchema(schemaString); |
| 49 | + |
| 50 | + query = createDeepQuery(howDeep); |
| 51 | + document = Parser.parse(query); |
| 52 | + } |
| 53 | + @Benchmark |
| 54 | + @BenchmarkMode(Mode.AverageTime) |
| 55 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 56 | + public ExecutableNormalizedOperation benchMarkAvgTime() { |
| 57 | + ExecutableNormalizedOperationFactory.Options options = ExecutableNormalizedOperationFactory.Options.defaultOptions(); |
| 58 | + ExecutableNormalizedOperation executableNormalizedOperation = createExecutableNormalizedOperation(schema, |
| 59 | + document, |
| 60 | + null, |
| 61 | + CoercedVariables.emptyVariables(), |
| 62 | + options); |
| 63 | + return executableNormalizedOperation; |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String[] args) throws RunnerException { |
| 67 | + runAtStartup(); |
| 68 | + |
| 69 | + Options opt = new OptionsBuilder() |
| 70 | + .include("benchmark.ENFBenchmarkDeepIntrospection") |
| 71 | + .build(); |
| 72 | + |
| 73 | + new Runner(opt).run(); |
| 74 | + } |
| 75 | + |
| 76 | + private static void runAtStartup() { |
| 77 | + |
| 78 | + ENFBenchmarkDeepIntrospection benchmarkIntrospection = new ENFBenchmarkDeepIntrospection(); |
| 79 | + benchmarkIntrospection.howDeep = 2; |
| 80 | + |
| 81 | + BenchmarkUtils.runInToolingForSomeTimeThenExit( |
| 82 | + benchmarkIntrospection::setUp, |
| 83 | + () -> { while (true) { benchmarkIntrospection.benchMarkAvgTime(); }}, |
| 84 | + () ->{} |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + private static String createDeepQuery(int depth) { |
| 91 | + String result = "query test {\n" + |
| 92 | + " __schema {\n" + |
| 93 | + " types {\n" + |
| 94 | + " ...F1\n" + |
| 95 | + " }\n" + |
| 96 | + " }\n" + |
| 97 | + "}\n"; |
| 98 | + |
| 99 | + for (int i = 1; i < depth; i++) { |
| 100 | + result += " fragment F" + i + " on __Type {\n" + |
| 101 | + " fields {\n" + |
| 102 | + " type {\n" + |
| 103 | + " ...F" + (i + 1) +"\n" + |
| 104 | + " }\n" + |
| 105 | + " }\n" + |
| 106 | + "\n" + |
| 107 | + " ofType {\n" + |
| 108 | + " ...F"+ (i + 1) + "\n" + |
| 109 | + " }\n" + |
| 110 | + " }\n"; |
| 111 | + } |
| 112 | + result += " fragment F" + depth + " on __Type {\n" + |
| 113 | + " fields {\n" + |
| 114 | + " type {\n" + |
| 115 | + "name\n" + |
| 116 | + " }\n" + |
| 117 | + " }\n" + |
| 118 | + "}\n"; |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments