Skip to content

Commit 21ac5c9

Browse files
committed
Added benchmark and bin helper
1 parent 3e82b74 commit 21ac5c9

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

bin/jmh.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
BRANCH=$(git rev-parse --abbrev-ref HEAD)
4+
JAR="build/libs/graphql-java-0.0.0-$BRANCH-SNAPSHOT-jmh.jar"
5+
echo "build and then running jmh for $JAR"
6+
7+
./gradlew clean jmhJar
8+
9+
java -jar "$JAR" "$@"
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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("\tf : String \n")
78+
.append("\tf").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("\tf : String \n")
82+
.append("\tf").append(i).append(" : String \n")
83+
.append("}\n");
84+
85+
sb.append("extend type O").append(i).append(" implements X").append(" { \n")
86+
.append("\tx : 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("\textendedF").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

Comments
 (0)