|
| 1 | +package benchmark; |
| 2 | + |
| 3 | +import graphql.language.Document; |
| 4 | +import graphql.parser.Parser; |
| 5 | +import graphql.schema.GraphQLSchema; |
| 6 | +import graphql.schema.idl.SchemaGenerator; |
| 7 | +import graphql.validation.ValidationError; |
| 8 | +import graphql.validation.Validator; |
| 9 | +import org.openjdk.jmh.annotations.Benchmark; |
| 10 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 11 | +import org.openjdk.jmh.annotations.Fork; |
| 12 | +import org.openjdk.jmh.annotations.Measurement; |
| 13 | +import org.openjdk.jmh.annotations.Mode; |
| 14 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 15 | +import org.openjdk.jmh.annotations.Scope; |
| 16 | +import org.openjdk.jmh.annotations.Setup; |
| 17 | +import org.openjdk.jmh.annotations.State; |
| 18 | +import org.openjdk.jmh.annotations.Warmup; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +import static graphql.Assert.assertTrue; |
| 25 | + |
| 26 | +@State(Scope.Benchmark) |
| 27 | +@BenchmarkMode(Mode.AverageTime) |
| 28 | +@Warmup(iterations = 2, time = 5) |
| 29 | +@Measurement(iterations = 3) |
| 30 | +@Fork(2) |
| 31 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 32 | +public class SubscriptionValidationBenchmark { |
| 33 | + |
| 34 | + private GraphQLSchema schema; |
| 35 | + private Document complexSubscriptionDocument; |
| 36 | + |
| 37 | + @Setup |
| 38 | + public void setup() { |
| 39 | + schema = SchemaGenerator.createdMockedSchema(schemaSdl()); |
| 40 | + complexSubscriptionDocument = Parser.parse(complexSubscriptionQuery()); |
| 41 | + assertTrue(validate(complexSubscriptionDocument).isEmpty()); |
| 42 | + } |
| 43 | + |
| 44 | + @Benchmark |
| 45 | + public List<ValidationError> complexSubscription() { |
| 46 | + return validate(complexSubscriptionDocument); |
| 47 | + } |
| 48 | + |
| 49 | + private List<ValidationError> validate(Document document) { |
| 50 | + Validator validator = new Validator(); |
| 51 | + return validator.validateDocument(schema, document, Locale.ENGLISH); |
| 52 | + } |
| 53 | + |
| 54 | + private static String schemaSdl() { |
| 55 | + return String.join("\n", |
| 56 | + "schema {", |
| 57 | + " query: Query", |
| 58 | + " subscription: Subscription", |
| 59 | + "}", |
| 60 | + "", |
| 61 | + "type Query {", |
| 62 | + " event: Event", |
| 63 | + "}", |
| 64 | + "", |
| 65 | + "type Subscription {", |
| 66 | + " event: Event", |
| 67 | + "}", |
| 68 | + "", |
| 69 | + "interface Node {", |
| 70 | + " id: ID", |
| 71 | + "}", |
| 72 | + "", |
| 73 | + "interface Named {", |
| 74 | + " name: String", |
| 75 | + "}", |
| 76 | + "", |
| 77 | + "type Event implements Node & Named {", |
| 78 | + " id: ID", |
| 79 | + " name: String", |
| 80 | + " timestamp: String", |
| 81 | + " payload: Payload", |
| 82 | + " related: Event", |
| 83 | + "}", |
| 84 | + "", |
| 85 | + "type Payload {", |
| 86 | + " id: ID", |
| 87 | + " value: String", |
| 88 | + " nested: Payload", |
| 89 | + " metrics: Metrics", |
| 90 | + "}", |
| 91 | + "", |
| 92 | + "type Metrics {", |
| 93 | + " count: Int", |
| 94 | + " label: String", |
| 95 | + "}"); |
| 96 | + } |
| 97 | + |
| 98 | + private static String complexSubscriptionQuery() { |
| 99 | + StringBuilder query = new StringBuilder(); |
| 100 | + query.append("subscription ComplexSubscription {\n"); |
| 101 | + appendRootFragmentSpreads(query); |
| 102 | + query.append("}\n"); |
| 103 | + appendRootFragments(query); |
| 104 | + appendEventFragments(query); |
| 105 | + appendSharedEventFragment(query); |
| 106 | + return query.toString(); |
| 107 | + } |
| 108 | + |
| 109 | + private static void appendRootFragmentSpreads(StringBuilder query) { |
| 110 | + for (int i = 0; i < 50; i++) { |
| 111 | + query.append(" ...RootFragment").append(i).append("\n"); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static void appendRootFragments(StringBuilder query) { |
| 116 | + for (int i = 0; i < 50; i++) { |
| 117 | + query.append("fragment RootFragment").append(i).append(" on Subscription {\n"); |
| 118 | + query.append(" event {\n"); |
| 119 | + query.append(" ...EventFragment").append(i % 20).append("\n"); |
| 120 | + query.append(" ...SharedEventFields\n"); |
| 121 | + query.append(" related {\n"); |
| 122 | + query.append(" ...SharedEventFields\n"); |
| 123 | + query.append(" }\n"); |
| 124 | + query.append(" }\n"); |
| 125 | + query.append("}\n"); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static void appendEventFragments(StringBuilder query) { |
| 130 | + for (int i = 0; i < 20; i++) { |
| 131 | + query.append("fragment EventFragment").append(i).append(" on Event {\n"); |
| 132 | + query.append(" id\n"); |
| 133 | + query.append(" name\n"); |
| 134 | + query.append(" payload {\n"); |
| 135 | + query.append(" value\n"); |
| 136 | + query.append(" metrics {\n"); |
| 137 | + query.append(" count\n"); |
| 138 | + query.append(" label\n"); |
| 139 | + query.append(" }\n"); |
| 140 | + query.append(" }\n"); |
| 141 | + query.append("}\n"); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static void appendSharedEventFragment(StringBuilder query) { |
| 146 | + query.append("fragment SharedEventFields on Event {\n"); |
| 147 | + query.append(" id\n"); |
| 148 | + query.append(" name\n"); |
| 149 | + query.append(" timestamp\n"); |
| 150 | + query.append(" payload {\n"); |
| 151 | + query.append(" nested {\n"); |
| 152 | + query.append(" value\n"); |
| 153 | + query.append(" metrics {\n"); |
| 154 | + query.append(" count\n"); |
| 155 | + query.append(" }\n"); |
| 156 | + query.append(" }\n"); |
| 157 | + query.append(" }\n"); |
| 158 | + query.append("}\n"); |
| 159 | + } |
| 160 | +} |
0 commit comments