Skip to content

Instantly share code, notes, and snippets.

@bbakerman
Created December 30, 2018 01:14
Show Gist options
  • Save bbakerman/855e9f2b76ce3642efebfd655e29efcd to your computer and use it in GitHub Desktop.
Save bbakerman/855e9f2b76ce3642efebfd655e29efcd to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.execution.instrumentation.tracing.TracingInstrumentation;
import graphql.schema.DataFetcher;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring;
public class TestPropertyDataFetcherPerformance {
private static final int NUMBER_OF_FRIENDS = 40 * 1000;
public static void main(String[] args) throws IOException {
GraphQL graphQL = buildGraphQL();
ObjectMapper objectMapper = new ObjectMapper();
// warm up
String query = "{ hero { name friends { name friends { name } } } }";
ExecutionResult warmUpResult = graphQL.execute(query);
objectMapper.writer().writeValue(new StringWriter(), warmUpResult);
long then = System.currentTimeMillis();
ExecutionResult executionResult = graphQL.execute(query);
long ms = System.currentTimeMillis() - then;
//System.out.println(executionResult.getExtensions());
System.out.printf("\n\nCompleted %d objects in %d ms\n", NUMBER_OF_FRIENDS, ms);
then = System.currentTimeMillis();
objectMapper.writer().writeValue(new StringWriter(), executionResult);
ms = System.currentTimeMillis() - then;
System.out.printf("\n\nJackson write %d objects in %d ms\n", NUMBER_OF_FRIENDS, ms);
}
private static GraphQL buildGraphQL() {
InputStream sdl = TestPropertyDataFetcherPerformance.class.getClassLoader().getResourceAsStream("starWarsSchema.graphqls");
TypeDefinitionRegistry definitionRegistry = new SchemaParser().parse(new InputStreamReader(sdl));
DataFetcher heroDataFetcher = environment -> CharacterDTO.mkCharacter("r2d2", NUMBER_OF_FRIENDS);
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
.type(
newTypeWiring("QueryType").dataFetcher("hero", heroDataFetcher))
.type(newTypeWiring("Character").typeResolver(
env -> env.getSchema().getObjectType("Human")
))
.build();
GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(definitionRegistry, runtimeWiring);
return GraphQL.newGraphQL(graphQLSchema)
.instrumentation(new TracingInstrumentation())
.build();
}
static class CharacterDTO {
private final String name;
private final List<CharacterDTO> friends;
CharacterDTO(String name, List<CharacterDTO> friends) {
this.name = name;
this.friends = friends;
}
public String getName() {
return name;
}
public List<CharacterDTO> getFriends() {
return friends;
}
public static CharacterDTO mkCharacter(String name, int friendCount) {
List<CharacterDTO> friends = new ArrayList<>(friendCount);
for (int i = 0; i < friendCount; i++) {
friends.add(mkCharacter("friend" + i, 0));
}
return new CharacterDTO(name, friends);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment