1
+ package benchmark ;
2
+
3
+ import graphql .schema .GraphQLSchema ;
4
+ import graphql .schema .idl .RuntimeWiring ;
5
+ import graphql .schema .idl .SchemaGenerator ;
6
+ import graphql .schema .idl .SchemaParser ;
7
+ import graphql .schema .idl .TypeDefinitionRegistry ;
8
+ import org .openjdk .jmh .annotations .Benchmark ;
9
+ import org .openjdk .jmh .annotations .BenchmarkMode ;
10
+ import org .openjdk .jmh .annotations .Fork ;
11
+ import org .openjdk .jmh .annotations .Measurement ;
12
+ import org .openjdk .jmh .annotations .Mode ;
13
+ import org .openjdk .jmh .annotations .OutputTimeUnit ;
14
+ import org .openjdk .jmh .annotations .Warmup ;
15
+ import org .openjdk .jmh .infra .Blackhole ;
16
+ import org .openjdk .jmh .runner .Runner ;
17
+ import org .openjdk .jmh .runner .RunnerException ;
18
+ import org .openjdk .jmh .runner .options .Options ;
19
+ import org .openjdk .jmh .runner .options .OptionsBuilder ;
20
+
21
+ import java .util .concurrent .TimeUnit ;
22
+
23
+ import static benchmark .BenchmarkUtils .runInToolingForSomeTimeThenExit ;
24
+
25
+ /**
26
+ * This JMH
27
+ */
28
+ @ Warmup (iterations = 2 , time = 5 )
29
+ @ Measurement (iterations = 3 )
30
+ @ Fork (3 )
31
+ public class CreateExtendedSchemaBenchmark {
32
+
33
+ private static final String SDL = mkSDL ();
34
+
35
+ @ Benchmark
36
+ @ BenchmarkMode (Mode .Throughput )
37
+ @ OutputTimeUnit (TimeUnit .MINUTES )
38
+ public void benchmarkLargeSchemaCreate (Blackhole blackhole ) {
39
+ blackhole .consume (createSchema (SDL ));
40
+ }
41
+
42
+ @ Benchmark
43
+ @ BenchmarkMode (Mode .AverageTime )
44
+ @ OutputTimeUnit (TimeUnit .MILLISECONDS )
45
+ public void benchmarkLargeSchemaCreateAvgTime (Blackhole blackhole ) {
46
+ blackhole .consume (createSchema (SDL ));
47
+ }
48
+
49
+ private static GraphQLSchema createSchema (String sdl ) {
50
+ TypeDefinitionRegistry registry = new SchemaParser ().parse (sdl );
51
+ return new SchemaGenerator ().makeExecutableSchema (registry , RuntimeWiring .MOCKED_WIRING );
52
+ }
53
+
54
+ /* something like
55
+ type Query { q : String } interface I { f : String }
56
+ interface I1 implements I {
57
+ f : String
58
+ f1 : String
59
+ }
60
+ type O1_1 implements I1 & I {
61
+ f : String
62
+ f1 : String
63
+ }
64
+ type O1_2 implements I1 & I {
65
+ f : String
66
+ f1 : String
67
+ }
68
+ */
69
+ private static String mkSDL () {
70
+ int numTypes = 10000 ;
71
+ int numExtends = 10 ;
72
+
73
+ StringBuilder sb = new StringBuilder ();
74
+ sb .append ("type Query { q : String } interface I { f : String } interface X { x : String }\n " );
75
+ for (int i = 0 ; i < numTypes ; i ++) {
76
+ sb .append ("interface I" ).append (i ).append (" implements I { \n " )
77
+ .append ("\t f : String \n " )
78
+ .append ("\t f" ).append (i ).append (" : String \n " ).append ("}\n " );
79
+
80
+ sb .append ("type O" ).append (i ).append (" implements I" ).append (i ).append (" & I { \n " )
81
+ .append ("\t f : String \n " )
82
+ .append ("\t f" ).append (i ).append (" : String \n " )
83
+ .append ("}\n " );
84
+
85
+ sb .append ("extend type O" ).append (i ).append (" implements X" ).append (" { \n " )
86
+ .append ("\t x : String \n " )
87
+ .append ("}\n " );
88
+
89
+ for (int j = 0 ; j < numExtends ; j ++) {
90
+ sb .append ("extend type O" ).append (i ).append (" { \n " )
91
+ .append ("\t extendedF" ).append (j ).append (" : String \n " )
92
+ .append ("}\n " );
93
+
94
+ }
95
+ }
96
+ return sb .toString ();
97
+ }
98
+
99
+ public static void main (String [] args ) throws RunnerException {
100
+ try {
101
+ runAtStartup ();
102
+ } catch (Throwable e ) {
103
+ throw new RuntimeException (e );
104
+ }
105
+ Options opt = new OptionsBuilder ()
106
+ .include ("benchmark.CreateExtendedSchemaBenchmark" )
107
+ .build ();
108
+
109
+ new Runner (opt ).run ();
110
+ }
111
+
112
+ private static void runAtStartup () {
113
+ runInToolingForSomeTimeThenExit (
114
+ () -> {
115
+ },
116
+ () -> createSchema (SDL ),
117
+ () -> {
118
+ }
119
+ );
120
+ }
121
+ }
0 commit comments