|
| 1 | +package benchmark; |
| 2 | + |
| 3 | +import graphql.schema.idl.SchemaParser; |
| 4 | +import graphql.schema.idl.TypeDefinitionRegistry; |
| 5 | +import org.jetbrains.annotations.NotNull; |
| 6 | +import org.openjdk.jmh.annotations.Benchmark; |
| 7 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 8 | +import org.openjdk.jmh.annotations.Measurement; |
| 9 | +import org.openjdk.jmh.annotations.Mode; |
| 10 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 11 | +import org.openjdk.jmh.annotations.Warmup; |
| 12 | +import org.openjdk.jmh.infra.Blackhole; |
| 13 | + |
| 14 | +import java.io.ByteArrayInputStream; |
| 15 | +import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.ObjectInputStream; |
| 17 | +import java.io.ObjectOutputStream; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +import static benchmark.BenchmarkUtils.asRTE; |
| 21 | + |
| 22 | +/** |
| 23 | + * This benchmarks {@link graphql.schema.idl.TypeDefinitionRegistry} parsing and serialisation |
| 24 | + * <p> |
| 25 | + * See https://github.com/openjdk/jmh/tree/master/jmh-samples/src/main/java/org/openjdk/jmh/samples/ for more samples |
| 26 | + * on what you can do with JMH |
| 27 | + * <p> |
| 28 | + * You MUST have the JMH plugin for IDEA in place for this to work : https://github.com/artyushov/idea-jmh-plugin |
| 29 | + * <p> |
| 30 | + * Install it and then just hit "Run" on a certain benchmark method |
| 31 | + */ |
| 32 | +@Warmup(iterations = 2, time = 5, batchSize = 3) |
| 33 | +@Measurement(iterations = 3, time = 10, batchSize = 4) |
| 34 | +public class TypeDefinitionParserVersusSerializeBenchMark { |
| 35 | + |
| 36 | + static SchemaParser schemaParser = new SchemaParser(); |
| 37 | + static String SDL = BenchmarkUtils.loadResource("large-schema-2.graphqls"); |
| 38 | + static TypeDefinitionRegistry registryOut = schemaParser.parse(SDL); |
| 39 | + static ByteArrayOutputStream baOS = serialisedRegistryStream(registryOut); |
| 40 | + |
| 41 | + @Benchmark |
| 42 | + @BenchmarkMode(Mode.Throughput) |
| 43 | + @OutputTimeUnit(TimeUnit.SECONDS) |
| 44 | + public void benchMarkParsing(Blackhole blackhole) { |
| 45 | + blackhole.consume(schemaParser.parse(SDL)); |
| 46 | + } |
| 47 | + |
| 48 | + @Benchmark |
| 49 | + @BenchmarkMode(Mode.Throughput) |
| 50 | + @OutputTimeUnit(TimeUnit.SECONDS) |
| 51 | + public void benchMarkSerializing(Blackhole blackhole) { |
| 52 | + blackhole.consume(serialise()); |
| 53 | + } |
| 54 | + |
| 55 | + static TypeDefinitionRegistry serialise() { |
| 56 | + return asRTE(() -> { |
| 57 | + |
| 58 | + ByteArrayInputStream baIS = new ByteArrayInputStream(baOS.toByteArray()); |
| 59 | + ObjectInputStream ois = new ObjectInputStream(baIS); |
| 60 | + |
| 61 | + return (TypeDefinitionRegistry) ois.readObject(); |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + @NotNull |
| 66 | + private static ByteArrayOutputStream serialisedRegistryStream(TypeDefinitionRegistry registryOut) { |
| 67 | + return asRTE(() -> { |
| 68 | + ByteArrayOutputStream baOS = new ByteArrayOutputStream(); |
| 69 | + ObjectOutputStream oos = new ObjectOutputStream(baOS); |
| 70 | + |
| 71 | + oos.writeObject(registryOut); |
| 72 | + return baOS; |
| 73 | + }); |
| 74 | + } |
| 75 | +} |
0 commit comments