-
Notifications
You must be signed in to change notification settings - Fork 1.2k
DF SelectionSet Benchmark #2893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| package benchmark; | ||
|
|
||
| import com.google.common.base.Charsets; | ||
| import com.google.common.io.Resources; | ||
| import graphql.execution.CoercedVariables; | ||
| import graphql.language.Document; | ||
| import graphql.normalized.ExecutableNormalizedField; | ||
| import graphql.normalized.ExecutableNormalizedOperation; | ||
| import graphql.normalized.ExecutableNormalizedOperationFactory; | ||
| import graphql.parser.Parser; | ||
| import graphql.schema.DataFetchingFieldSelectionSet; | ||
| import graphql.schema.DataFetchingFieldSelectionSetImpl; | ||
| import graphql.schema.GraphQLOutputType; | ||
| import graphql.schema.GraphQLSchema; | ||
| import graphql.schema.SelectedField; | ||
| import graphql.schema.idl.SchemaGenerator; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static com.google.common.io.Resources.getResource; | ||
|
|
||
| @State(Scope.Benchmark) | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @Warmup(iterations = 2) | ||
| @Measurement(iterations = 2, timeUnit = TimeUnit.NANOSECONDS) | ||
| public class DFSelectionSetBenchmark { | ||
|
|
||
| @State(Scope.Benchmark) | ||
| public static class MyState { | ||
|
|
||
| public ExecutableNormalizedField normalisedField; | ||
| public GraphQLOutputType outputFieldType; | ||
| GraphQLSchema schema; | ||
| Document document; | ||
|
|
||
| @Setup | ||
| public void setup() { | ||
| try { | ||
| String schemaString = readFromClasspath("large-schema-2.graphqls"); | ||
| schema = SchemaGenerator.createdMockedSchema(schemaString); | ||
|
|
||
| String query = readFromClasspath("large-schema-2-query.graphql"); | ||
| document = Parser.parse(query); | ||
|
|
||
| ExecutableNormalizedOperation executableNormalizedOperation = ExecutableNormalizedOperationFactory.createExecutableNormalizedOperation(schema, document, null, CoercedVariables.emptyVariables()); | ||
|
|
||
| normalisedField = executableNormalizedOperation.getTopLevelFields().get(0); | ||
|
|
||
| outputFieldType = schema.getObjectType("Object42"); | ||
|
|
||
| } catch (Exception e) { | ||
| System.out.println(e); | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private String readFromClasspath(String file) throws IOException { | ||
| URL url = getResource(file); | ||
| return Resources.toString(url, Charsets.UTF_8); | ||
| } | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Warmup(iterations = 2) | ||
| @Measurement(iterations = 5, time = 10) | ||
| @Threads(1) | ||
| @Fork(3) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| public void benchMarkAvgTime(MyState myState, Blackhole blackhole) { | ||
| List<SelectedField> fields = getSelectedFields(myState); | ||
| blackhole.consume(fields); | ||
| } | ||
|
|
||
| @Benchmark | ||
| @Warmup(iterations = 2) | ||
| @Measurement(iterations = 5, time = 10) | ||
| @Threads(1) | ||
| @Fork(3) | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
| public void benchMarkThroughput(MyState myState, Blackhole blackhole) { | ||
| List<SelectedField> fields = getSelectedFields(myState); | ||
| blackhole.consume(fields); | ||
| } | ||
|
|
||
| private List<SelectedField> getSelectedFields(MyState myState) { | ||
| DataFetchingFieldSelectionSet dataFetchingFieldSelectionSet = DataFetchingFieldSelectionSetImpl.newCollector(myState.schema, myState.outputFieldType, () -> myState.normalisedField); | ||
| return dataFetchingFieldSelectionSet.getFields("wontBeFound"); | ||
| } | ||
|
|
||
| public static void mainX(String[] args) throws InterruptedException { | ||
| MyState myState = new MyState(); | ||
| myState.setup(); | ||
|
|
||
| while (true) { | ||
| List<SelectedField> selectedFields = new DFSelectionSetBenchmark().getSelectedFields(myState); | ||
| Thread.sleep(500); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw I found it's perfectly fine to simply name this
main. In IntelliJ the run button on the class will run this, but the run buttons next to thebenchMarkAvgTimeandbenchMarkThroughputfunctions still work fine.