Skip to content

Commit 8394220

Browse files
committed
add NQ benchmarks
1 parent 5ec03a8 commit 8394220

6 files changed

Lines changed: 4967 additions & 0 deletions

File tree

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package benchmark;
2+
3+
import com.google.common.base.Charsets;
4+
import com.google.common.io.Resources;
5+
import graphql.language.Document;
6+
import graphql.normalized.NormalizedQueryTree;
7+
import graphql.normalized.NormalizedQueryTreeFactory;
8+
import graphql.parser.Parser;
9+
import graphql.schema.GraphQLSchema;
10+
import graphql.schema.idl.SchemaGenerator;
11+
import org.openjdk.jmh.annotations.Benchmark;
12+
import org.openjdk.jmh.annotations.BenchmarkMode;
13+
import org.openjdk.jmh.annotations.Fork;
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.Scope;
18+
import org.openjdk.jmh.annotations.Setup;
19+
import org.openjdk.jmh.annotations.State;
20+
import org.openjdk.jmh.annotations.Threads;
21+
import org.openjdk.jmh.annotations.Warmup;
22+
23+
import java.io.IOException;
24+
import java.net.URL;
25+
import java.util.Collections;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.TimeUnit;
28+
29+
import static com.google.common.io.Resources.getResource;
30+
31+
@State(Scope.Benchmark)
32+
@BenchmarkMode(Mode.Throughput)
33+
@Warmup(iterations = 2)
34+
@Measurement(iterations = 2, timeUnit = TimeUnit.NANOSECONDS)
35+
public class NQBenchmark {
36+
37+
@org.openjdk.jmh.annotations.State(Scope.Benchmark)
38+
public static class MyState {
39+
40+
GraphQLSchema schema;
41+
Document document;
42+
43+
@Setup
44+
public void setup() {
45+
try {
46+
String schemaString = readFromClasspath("large-schema-1.graphqls");
47+
schema = SchemaGenerator.createdMockedSchema(schemaString);
48+
49+
String query = readFromClasspath("large-schema-1-query.graphql");
50+
document = Parser.parse(query);
51+
} catch (Exception e) {
52+
System.out.println(e);
53+
throw new RuntimeException(e);
54+
}
55+
}
56+
57+
private String readFromClasspath(String file) throws IOException {
58+
URL url = getResource(file);
59+
return Resources.toString(url, Charsets.UTF_8);
60+
}
61+
}
62+
63+
@Benchmark
64+
@Warmup(iterations = 2)
65+
@Measurement(iterations = 100, time = 10)
66+
@Threads(1)
67+
@Fork(3)
68+
@BenchmarkMode(Mode.AverageTime)
69+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
70+
public NormalizedQueryTree benchMarkAvgTime(MyState myState) throws ExecutionException, InterruptedException {
71+
NormalizedQueryTree normalizedQuery = NormalizedQueryTreeFactory.createNormalizedQuery(myState.schema, myState.document, null, Collections.emptyMap());
72+
// System.out.println("fields size:" + normalizedQuery.getFieldToNormalizedField().size());
73+
return normalizedQuery;
74+
}
75+
76+
77+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package benchmark;
2+
3+
import com.google.common.base.Charsets;
4+
import com.google.common.io.Resources;
5+
import graphql.language.Document;
6+
import graphql.normalized.NormalizedQueryTree;
7+
import graphql.normalized.NormalizedQueryTreeFactory;
8+
import graphql.parser.Parser;
9+
import graphql.schema.GraphQLSchema;
10+
import graphql.schema.idl.SchemaGenerator;
11+
import org.openjdk.jmh.annotations.Benchmark;
12+
import org.openjdk.jmh.annotations.BenchmarkMode;
13+
import org.openjdk.jmh.annotations.Fork;
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.Scope;
18+
import org.openjdk.jmh.annotations.Setup;
19+
import org.openjdk.jmh.annotations.State;
20+
import org.openjdk.jmh.annotations.Threads;
21+
import org.openjdk.jmh.annotations.Warmup;
22+
23+
import java.io.IOException;
24+
import java.net.URL;
25+
import java.util.Collections;
26+
import java.util.concurrent.ExecutionException;
27+
import java.util.concurrent.TimeUnit;
28+
29+
import static com.google.common.io.Resources.getResource;
30+
31+
@State(Scope.Benchmark)
32+
@BenchmarkMode(Mode.Throughput)
33+
@Warmup(iterations = 2)
34+
@Measurement(iterations = 2, timeUnit = TimeUnit.NANOSECONDS)
35+
public class NQBenchmark2 {
36+
37+
@State(Scope.Benchmark)
38+
public static class MyState {
39+
40+
GraphQLSchema schema;
41+
Document document;
42+
43+
@Setup
44+
public void setup() {
45+
try {
46+
String schemaString = readFromClasspath("large-schema-2.graphqls");
47+
schema = SchemaGenerator.createdMockedSchema(schemaString);
48+
49+
String query = readFromClasspath("large-schema-2-query.graphql");
50+
document = Parser.parse(query);
51+
} catch (Exception e) {
52+
System.out.println(e);
53+
throw new RuntimeException(e);
54+
}
55+
}
56+
57+
private String readFromClasspath(String file) throws IOException {
58+
URL url = getResource(file);
59+
return Resources.toString(url, Charsets.UTF_8);
60+
}
61+
}
62+
63+
@Benchmark
64+
@Warmup(iterations = 2)
65+
@Measurement(iterations = 100, time = 10)
66+
@Threads(1)
67+
@Fork(3)
68+
@BenchmarkMode(Mode.AverageTime)
69+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
70+
public NormalizedQueryTree benchMarkAvgTime(MyState myState) throws ExecutionException, InterruptedException {
71+
NormalizedQueryTree normalizedQuery = NormalizedQueryTreeFactory.createNormalizedQuery(myState.schema, myState.document, null, Collections.emptyMap());
72+
System.out.println("fields size:" + normalizedQuery.getFieldToNormalizedField().size());
73+
return normalizedQuery;
74+
}
75+
76+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
query operation {...Fragment1 ...Fragment2 ...Fragment3} fragment Fragment3 on Object27 {field91 {field29 field30 field27 field1} field57 {field58 field59 field1}} fragment Fragment2 on Object27 {field91 {field29 field30 field27 field1} field57 {field58 field59 field1}} fragment Fragment1 on Object27 {field91 {field27 field1} field57 {field58 field59 field1}}

0 commit comments

Comments
 (0)