diff --git a/performance-checks/.gitignore b/performance-checks/.gitignore new file mode 100644 index 0000000..9243c63 --- /dev/null +++ b/performance-checks/.gitignore @@ -0,0 +1,26 @@ +.gradle +/build/ +!gradle/wrapper/gradle-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ \ No newline at end of file diff --git a/performance-checks/README.md b/performance-checks/README.md new file mode 100644 index 0000000..686df65 --- /dev/null +++ b/performance-checks/README.md @@ -0,0 +1,122 @@ +# Performance checks + +This example has a few mechanisms to prevent your GraphQL server from dealing with expensive queries sent by abusive clients +(or maybe legitimate clients that running expensive queries unaware of the negative impacts they might cause). +Also, it has a couple of timeout strategies that, although won't help ease the burden on the server (since the expensive +operations are already under way), will provide a better experience to the consumer that won't have to wait forever for +their requests to return. + +Here we introduce 4 mechanisms to help with that task. 3 of them are based on GraphQL Java instrumentation capabilities, +and the forth one is a bit out GraphQL Java jurisdiction and more related to web servers. + +1. MaxQueryDepthInstrumentation: limit the depth of queries to 5 +2. MaxQueryComplexityInstrumentation: set complexity values to fields and limit query complexity to 5 +3. A custom Instrumentation that sets a timeout period of 3 seconds for DataFetchers +4. A hard request timeout of 10 seconds, specified in the web server level (Spring) + +The first 2 items will actually prevent server overload, since they act before the request reach the DataFetchers, which +perform the expensive operations. Number 3 and 4 are timeouts that force long running executions to return early to customers. + +# The schema +The schema we're using is quite simple: + +```graphql +type Query { + instrumentedField(sleep: Int): String + characters: [Character] +} + +type Character { + name: String! + friends: [Character] + energy: Float +} +``` + +There are a few interesting facts related to this example. + +* instrumentedField: returns a fixed string ("value"). It receives a parameter "sleep" that forces the DataFetcher to + take that amount of seconds to return. Set that value to anything above 3 seconds and a timeout error will be thrown. + This simulates a long running DataFetcher that would be forcefully stopped. + +```graphql +{ + instrumentedField(sleep: 4) # will result in an error +} +``` + +* friends: will return a list of characters, that can themselves have friends, and so on... It's quite clear that queries + might overuse this field and end up having a large number of nested friends and characters. Add 5 or more levels of + friends and an error will be thrown. + +```graphql +{ + characters { + name + friends { + name + friends { + name + friends { + name + friends { + name # an error will be thrown, since the depth is higher than the limit + } + } + } + } + } +} +``` + +* energy: getting this field involves some expensive calculations, so we've established that it has a complexity value + of 3 (all the other fields have complexity 0). We've also defined that a query can have a maximum complexity of 5. So, + if "energy" is present 2 times or more in a given query, an error will be thrown. + +```graphql +{ + characters { + name + energy + friends { + name + energy # an error will be thrown, since we've asked for "energy" 2 times + } + } +} +``` + +# Request timeout +Although this is not really GraphQL Java business, it might be useful to set a hard request timeout on the web server +level. +To achieve this using Spring, the following property can be used: + +``` +spring.mvc.async.request-timeout=10000 +``` + + +# Running the code + + To build the example code in this repository type: + +``` +./gradlew build + ``` + +To run the example code type: + +``` +./gradlew bootRun +``` + +To access the example application, point your browser at: + http://localhost:8080/ + +## Note about introspection and max query depth +A bad side effect of specifying a maximum depth for queries is that this will prevent introspection queries to properly +execute. This affects GraphiQL's documentation and autocomplete features, that will simply not work. +This is a tricky problem to fix and [has been discussed in the past](https://github.com/graphql-java/graphql-java/issues/1055). + +You can still use GraphiQL to execute queries and inspect results. If you want documentation and autocomplete back in +GraphiQL, just temporarily disable the max depth instrumentation. diff --git a/performance-checks/build.gradle b/performance-checks/build.gradle new file mode 100644 index 0000000..91aa379 --- /dev/null +++ b/performance-checks/build.gradle @@ -0,0 +1,33 @@ +buildscript { + ext { + springBootVersion = '2.0.5.RELEASE' + } + repositories { + mavenCentral() + } + dependencies { + classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") + } +} + +apply plugin: 'java' +apply plugin: 'eclipse' +apply plugin: 'org.springframework.boot' +apply plugin: 'io.spring.dependency-management' + +group = 'com.graphql-java.examples' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + + +dependencies { + compile "io.reactivex.rxjava2:rxjava:2.1.5" + implementation('org.springframework.boot:spring-boot-starter-web') + implementation('com.graphql-java:graphql-java:10.0') + implementation('com.google.guava:guava:26.0-jre') + testImplementation('org.springframework.boot:spring-boot-starter-test') +} diff --git a/performance-checks/gradle/wrapper/gradle-wrapper.jar b/performance-checks/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..1ce6e58 Binary files /dev/null and b/performance-checks/gradle/wrapper/gradle-wrapper.jar differ diff --git a/performance-checks/gradle/wrapper/gradle-wrapper.properties b/performance-checks/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..3953aff --- /dev/null +++ b/performance-checks/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Oct 07 10:24:43 AEDT 2018 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.8.1-all.zip diff --git a/performance-checks/gradlew b/performance-checks/gradlew new file mode 100755 index 0000000..4453cce --- /dev/null +++ b/performance-checks/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save ( ) { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/performance-checks/gradlew.bat b/performance-checks/gradlew.bat new file mode 100644 index 0000000..f955316 --- /dev/null +++ b/performance-checks/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/performance-checks/settings.gradle b/performance-checks/settings.gradle new file mode 100644 index 0000000..4febc54 --- /dev/null +++ b/performance-checks/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'performance-checks' diff --git a/spring-boot-integration/src/main/java/graphql/examples/springboot/Application.java b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/Application.java similarity index 83% rename from spring-boot-integration/src/main/java/graphql/examples/springboot/Application.java rename to performance-checks/src/main/java/com/graphql/java/examples/performance/checks/Application.java index ba45440..91ffe44 100644 --- a/spring-boot-integration/src/main/java/graphql/examples/springboot/Application.java +++ b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/Application.java @@ -1,4 +1,4 @@ -package graphql.examples.springboot; +package com.graphql.java.examples.performance.checks; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/GraphQLController.java b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/GraphQLController.java new file mode 100644 index 0000000..8693d6b --- /dev/null +++ b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/GraphQLController.java @@ -0,0 +1,82 @@ +package com.graphql.java.examples.performance.checks; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import graphql.ExecutionInput; +import graphql.GraphQL; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.Callable; + +/** + * The controller that exposes the GET and POST /graphql endpoints + * + * What is special about this controller is that the request methods return a {@link Callable} wrapping the actual + * value maps. This is to activate the global timeout property, defined by `spring.mvc.async.request-timeout` in the + * `application.properties` file. + */ +@RestController +public class GraphQLController { + private final GraphQL graphql; + private final ObjectMapper objectMapper; + + @Autowired + public GraphQLController(GraphQL graphql, ObjectMapper objectMapper) { + this.graphql = graphql; + this.objectMapper = objectMapper; + } + + @RequestMapping(value = "/graphql", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + @CrossOrigin + public Callable> graphqlGET(@RequestParam("query") String query, + @RequestParam(value = "operationName", required = false) String operationName, + @RequestParam("variables") String variablesJson + ) throws IOException { + final Map variables = new LinkedHashMap<>(); + + if (variablesJson != null) { + variables.putAll(objectMapper.readValue(variablesJson, new TypeReference>() {})); + } + + return () -> executeGraphqlQuery(query, operationName, variables); + } + + @SuppressWarnings("unchecked") + @RequestMapping(value = "/graphql", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) + @CrossOrigin + public Callable> graphql(@RequestBody Map body) { + + final String query = (String) body.get("query"); + + String operationName = (String) body.get("operationName"); + + final Map variables = new LinkedHashMap<>(); + + if (body.get("variables") != null) { + variables.putAll((Map) body.get("variables")); + } + + return () -> executeGraphqlQuery(query, operationName, variables); + } + + private Map executeGraphqlQuery(String query, String operationName, Map variables) { + ExecutionInput executionInput = ExecutionInput.newExecutionInput() + .query(query) + .operationName(operationName) + .variables(variables) + .build(); + return this.graphql.execute(executionInput).toSpecification(); + } + + +} diff --git a/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/GraphQLDataFetchers.java b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/GraphQLDataFetchers.java new file mode 100644 index 0000000..1c45dba --- /dev/null +++ b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/GraphQLDataFetchers.java @@ -0,0 +1,57 @@ +package com.graphql.java.examples.performance.checks; + +import com.graphql.java.examples.performance.checks.data.FilmCharacter; +import com.graphql.java.examples.performance.checks.data.StarWarsData; +import graphql.schema.DataFetcher; +import org.springframework.stereotype.Component; + +import javax.naming.Context; +import java.util.List; +import java.util.Random; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +import static java.util.stream.Collectors.toList; + +@Component +public class GraphQLDataFetchers { + public DataFetcher getInstrumentedFieldDataFetcher() { + return environment -> { + final Integer sleepTime = environment.getArgument("sleep"); + + sleep(sleepTime); + + return "value"; + }; + } + + public DataFetcher getCharactersDataFetcher() { + return environment -> StarWarsData.getAllCharacters(); + } + + public DataFetcher getFriendsDataFetcher() { + return environment -> { + FilmCharacter character = environment.getSource(); + List friendIds = character.getFriends(); + + return friendIds.stream().map(StarWarsData::getCharacter).collect(toList()); + }; + } + + public DataFetcher getEnergyDataFetcher() { + return environment -> Math.random() * 1000; + } + + static void sleep(Integer seconds) { + if(seconds == null) { + return; + } + + try { + TimeUnit.SECONDS.sleep(seconds); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + +} diff --git a/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/GraphQLProvider.java b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/GraphQLProvider.java new file mode 100644 index 0000000..f735a28 --- /dev/null +++ b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/GraphQLProvider.java @@ -0,0 +1,82 @@ +package com.graphql.java.examples.performance.checks; + +import com.google.common.base.Charsets; +import com.google.common.collect.ImmutableMap; +import com.google.common.io.Resources; +import com.graphql.java.examples.performance.checks.instrumentation.InstrumentationFactory; +import graphql.GraphQL; +import graphql.execution.instrumentation.ChainedInstrumentation; +import graphql.execution.instrumentation.Instrumentation; +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 org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.IOException; +import java.net.URL; +import java.util.Arrays; + +import static graphql.schema.idl.TypeRuntimeWiring.newTypeWiring; + +@Component +public class GraphQLProvider { + @Autowired + private GraphQLDataFetchers graphQLDataFetchers; + + private GraphQL graphQL; + + @PostConstruct + public void init() throws IOException { + URL url = Resources.getResource("schema.graphql"); + String sdl = Resources.toString(url, Charsets.UTF_8); + GraphQLSchema graphQLSchema = buildSchema(sdl); + + Instrumentation chainedInstrumentation = new ChainedInstrumentation(Arrays.asList( + InstrumentationFactory.maxDepthInstrumentation(5), + InstrumentationFactory.timeoutInstrumentation(3, "instrumentedField"), + InstrumentationFactory.maxComplexityInstrumentation( + 5, + ImmutableMap.builder() + .put("energy", 3) + .build() + ) + )); + + this.graphQL = GraphQL + .newGraphQL(graphQLSchema) + .instrumentation(chainedInstrumentation) + .build(); + } + + private GraphQLSchema buildSchema(String sdl) { + TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(sdl); + RuntimeWiring runtimeWiring = buildWiring(); + SchemaGenerator schemaGenerator = new SchemaGenerator(); + return schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring); + } + + private RuntimeWiring buildWiring() { + return RuntimeWiring.newRuntimeWiring() + .type(newTypeWiring("Query") + .dataFetcher("instrumentedField", graphQLDataFetchers.getInstrumentedFieldDataFetcher()) + .dataFetcher("characters", graphQLDataFetchers.getCharactersDataFetcher()) + .build()) + .type(newTypeWiring("Character") + .dataFetcher("friends", graphQLDataFetchers.getFriendsDataFetcher()) + .dataFetcher("energy", graphQLDataFetchers.getEnergyDataFetcher()) + ) + .build(); + + } + + @Bean + public GraphQL graphQL() { + return graphQL; + } + +} diff --git a/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/data/FilmCharacter.java b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/data/FilmCharacter.java new file mode 100644 index 0000000..27e2495 --- /dev/null +++ b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/data/FilmCharacter.java @@ -0,0 +1,30 @@ +package com.graphql.java.examples.performance.checks.data; + +import java.util.List; + +/** + * A character from the movie Star Wars + */ +public class FilmCharacter { + final String id; + final String name; + final List friends; + + public FilmCharacter(String id, String name, List friends) { + this.id = id; + this.name = name; + this.friends = friends; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public List getFriends() { + return friends; + } +} diff --git a/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/data/StarWarsData.java b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/data/StarWarsData.java new file mode 100644 index 0000000..50891a0 --- /dev/null +++ b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/data/StarWarsData.java @@ -0,0 +1,70 @@ +package com.graphql.java.examples.performance.checks.data; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; + +import static java.util.Arrays.asList; + +/** + * A fake, static, "database" containing data about a few Star Wars characters + */ +@SuppressWarnings("unused") +public class StarWarsData { + static FilmCharacter luke = new FilmCharacter( + "1000", + "Luke Skywalker", + asList("1002", "1003") + ); + + static FilmCharacter vader = new FilmCharacter( + "1001", + "Darth Vader", + asList("1004") + ); + + static FilmCharacter han = new FilmCharacter( + "1002", + "Han Solo", + asList("1000", "1003") + ); + + static FilmCharacter leia = new FilmCharacter( + "1003", + "Leia Organa", + asList("1000", "1002") + ); + + static FilmCharacter tarkin = new FilmCharacter( + "1004", + "Wilhuff Tarkin", + asList("1001") + ); + + static Map characterData = new LinkedHashMap<>(); + + static { + characterData.put("1000", luke); + characterData.put("1001", vader); + characterData.put("1002", han); + characterData.put("1003", leia); + characterData.put("1004", tarkin); + } + + public static boolean isFilmCharacter(String id) { + return characterData.get(id) != null; + } + + public static Collection getAllCharacters() { + return characterData.values(); + } + + public static FilmCharacter getCharacter(String id) { + if (characterData.get(id) != null) { + return characterData.get(id); + } else if (characterData.get(id) != null) { + return characterData.get(id); + } + return null; + } +} diff --git a/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/instrumentation/InstrumentationFactory.java b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/instrumentation/InstrumentationFactory.java new file mode 100644 index 0000000..d50309d --- /dev/null +++ b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/instrumentation/InstrumentationFactory.java @@ -0,0 +1,70 @@ +package com.graphql.java.examples.performance.checks.instrumentation; + +import graphql.analysis.MaxQueryComplexityInstrumentation; +import graphql.analysis.MaxQueryDepthInstrumentation; +import graphql.execution.instrumentation.Instrumentation; + +import java.util.Map; + +/** + * Contains methods to create instances of the 3 {@link Instrumentation} used in this example + */ +public class InstrumentationFactory { + + /** + * Creates an instance of our custom {@link TimeoutInstrumentation} + * + * @param timeoutSeconds the timeout period in seconds + * @param fields which fields should have their {@link graphql.schema.DataFetcher} instrumented + * @return an instance of {@link TimeoutInstrumentation} + * @see TimeoutInstrumentation + */ + public static Instrumentation timeoutInstrumentation(int timeoutSeconds, String... fields) { + return new TimeoutInstrumentation(timeoutSeconds, fields); + } + + /** + * Creates an instance of the {@link MaxQueryDepthInstrumentation}, defined in the GraphQL Java library + *

+ * This Instrumentation will analyze the incoming GraphQL queries and, if they contain more nested fields than + * the limit specified by maxDepth, an error will be thrown before any data fetchers execute. + * + * @param maxDepth the maximum depth allowed in the queries + * @return an instance of {@link MaxQueryDepthInstrumentation} + */ + public static Instrumentation maxDepthInstrumentation(int maxDepth) { + return new MaxQueryDepthInstrumentation(maxDepth); + } + + /** + * Creates an instance of the {@link MaxQueryComplexityInstrumentation}, defined in the GraphQL Java library + *

+ * This Instrumentation will analyze the incoming GraphQL queries and, if the sum of the complexity of the fields + * used in the query is higher than the limit specified by maxComplexity an error will be thrown before any + * data fetchers execute. + *

+ * The default implementation of {@link MaxQueryComplexityInstrumentation} considers that every field has a + * complexity of 1. This behaviour can be extended by providing an implementation of + * {@link graphql.analysis.FieldComplexityCalculator}. + *

+ * The example {@link Instrumentation} created by this method allows the consumer to specify arbitrary + * complexity for each field. + * + * @param maxComplexity the maximum complexity allowed in the query + * @param fieldsComplexity a map containing the complexity of each field. Fields that are not contained in the map + * are considered to have complexity equal to 0. + * @return an instance of {@link MaxQueryComplexityInstrumentation} + */ + public static Instrumentation maxComplexityInstrumentation( + int maxComplexity, Map fieldsComplexity + ) { + return new MaxQueryComplexityInstrumentation(maxComplexity, (environment, childComplexity) -> { + final String fieldName = environment.getField().getName(); + + final int thisComplexity = fieldsComplexity.getOrDefault(fieldName, 0); + + return thisComplexity + childComplexity; + }); + } + +} diff --git a/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/instrumentation/TimeoutInstrumentation.java b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/instrumentation/TimeoutInstrumentation.java new file mode 100644 index 0000000..999876b --- /dev/null +++ b/performance-checks/src/main/java/com/graphql/java/examples/performance/checks/instrumentation/TimeoutInstrumentation.java @@ -0,0 +1,60 @@ +package com.graphql.java.examples.performance.checks.instrumentation; + +import graphql.execution.instrumentation.SimpleInstrumentation; +import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; +import graphql.schema.DataFetcher; +import io.reactivex.Observable; +import io.reactivex.schedulers.Schedulers; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * A custom Instrumentation that will stop a Data Fetcher that takes too long to return a value. + *

+ * This Instrumentation will act only when the query is being executed. Some other Instrumentations + * (like {@link graphql.analysis.MaxQueryDepthInstrumentation} and {@link graphql.analysis.MaxQueryComplexityInstrumentation}) + * have their rules applied when the query is being analyzed. + */ +public class TimeoutInstrumentation extends SimpleInstrumentation { + private final int timeoutSeconds; + private final List fields; + + /** + * @param timeoutSeconds the timeout period in seconds + * @param fields which fields should have their {@link DataFetcher} instrumented + */ + public TimeoutInstrumentation(int timeoutSeconds, String... fields) { + this.timeoutSeconds = timeoutSeconds; + this.fields = Arrays.asList(fields); + } + + /** + * Wraps the {@link DataFetcher} in another instance of {@link DataFetcher} that will throw a Timeout error + * if the original one takes too long to return. + * + * @param dataFetcher the original {@link DataFetcher} + * @param parameters contains data about the environment and parameters + * @return + */ + @Override + public DataFetcher instrumentDataFetcher( + DataFetcher dataFetcher, InstrumentationFieldFetchParameters parameters + ) { + // Only apply instrumentation to specified fields + if (!fields.contains(parameters.getEnvironment().getField().getName())) { + return dataFetcher; + } + + // Times out if the original dataFetcher doesn't return before the specified period. + // This implementation is using RxJava Observables but it could very well be implemented using + // CompletableFutures or Threads + return environment -> + Observable.fromCallable(() -> dataFetcher.get(environment)) + .subscribeOn(Schedulers.computation()) + .timeout(timeoutSeconds, TimeUnit.SECONDS) + .blockingFirst(); + + } +} diff --git a/performance-checks/src/main/resources/application.properties b/performance-checks/src/main/resources/application.properties new file mode 100644 index 0000000..de3799f --- /dev/null +++ b/performance-checks/src/main/resources/application.properties @@ -0,0 +1,4 @@ +# Specifies a timeout for all requests served by this application +# This is kind of a "last resource" timeout. If any of the mechanisms implemented programmatically in the app +# fail to prevent long running requests, they will still be killed by this global timeout. +spring.mvc.async.request-timeout=10000 diff --git a/performance-checks/src/main/resources/schema.graphql b/performance-checks/src/main/resources/schema.graphql new file mode 100644 index 0000000..f30e2a3 --- /dev/null +++ b/performance-checks/src/main/resources/schema.graphql @@ -0,0 +1,16 @@ + +type Query { + instrumentedField(sleep: Int): String + characters: [Character] +} + +type Character { + # "name" is cheap to obtain, so its complexity value is 0. + name: String! + # a list of "friends" can nest another list of friends, and so on. This nesting can go on forever, + # so, in the code, we limit the amount of nested fields to 5. + friends: [Character] + # the "energy" field is very expensive to calculate. So it has a complexity of value of 3. + energy: Float +} + diff --git a/performance-checks/src/main/resources/static/index.html b/performance-checks/src/main/resources/static/index.html new file mode 100644 index 0000000..8efc68c --- /dev/null +++ b/performance-checks/src/main/resources/static/index.html @@ -0,0 +1,144 @@ + + + + + + + + + + + + + + + + +

+

Welcome to spring-boot integration example

+
+
Loading graphiql editor...
+ + \ No newline at end of file diff --git a/spring-boot-integration/src/test/java/com/graphqljava/examples/springboot/ApplicationTests.java b/performance-checks/src/test/java/com/graphql/java/examples/performance/checks/ApplicationTests.java similarity index 85% rename from spring-boot-integration/src/test/java/com/graphqljava/examples/springboot/ApplicationTests.java rename to performance-checks/src/test/java/com/graphql/java/examples/performance/checks/ApplicationTests.java index 7785f46..86f56e4 100644 --- a/spring-boot-integration/src/test/java/com/graphqljava/examples/springboot/ApplicationTests.java +++ b/performance-checks/src/test/java/com/graphql/java/examples/performance/checks/ApplicationTests.java @@ -1,4 +1,4 @@ -package com.graphqljava.examples.springboot; +package com.graphql.java.examples.performance.checks; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/settings.gradle b/settings.gradle index 491bd6e..4547ad1 100644 --- a/settings.gradle +++ b/settings.gradle @@ -4,5 +4,4 @@ include 'http-example' include 'spring-boot-integration' include 'hibernate-example' include 'subscription-example' - - +include 'performance-checks' diff --git a/spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/Application.java b/spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/Application.java new file mode 100644 index 0000000..91ffe44 --- /dev/null +++ b/spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/Application.java @@ -0,0 +1,12 @@ +package com.graphql.java.examples.performance.checks; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot-integration/src/main/java/graphql/examples/springboot/GraphQLController.java b/spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/GraphQLController.java similarity index 98% rename from spring-boot-integration/src/main/java/graphql/examples/springboot/GraphQLController.java rename to spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/GraphQLController.java index 7c5c933..c5d2488 100644 --- a/spring-boot-integration/src/main/java/graphql/examples/springboot/GraphQLController.java +++ b/spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/GraphQLController.java @@ -1,4 +1,4 @@ -package graphql.examples.springboot; +package com.graphql.java.examples.performance.checks; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/spring-boot-integration/src/main/java/graphql/examples/springboot/GraphQLDataFetchers.java b/spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/GraphQLDataFetchers.java similarity index 86% rename from spring-boot-integration/src/main/java/graphql/examples/springboot/GraphQLDataFetchers.java rename to spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/GraphQLDataFetchers.java index 1f0da89..622cb01 100644 --- a/spring-boot-integration/src/main/java/graphql/examples/springboot/GraphQLDataFetchers.java +++ b/spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/GraphQLDataFetchers.java @@ -1,4 +1,4 @@ -package graphql.examples.springboot; +package com.graphql.java.examples.performance.checks; import graphql.schema.DataFetcher; import org.springframework.stereotype.Component; diff --git a/spring-boot-integration/src/main/java/graphql/examples/springboot/GraphQLProvider.java b/spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/GraphQLProvider.java similarity index 97% rename from spring-boot-integration/src/main/java/graphql/examples/springboot/GraphQLProvider.java rename to spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/GraphQLProvider.java index a4bf55f..d052213 100644 --- a/spring-boot-integration/src/main/java/graphql/examples/springboot/GraphQLProvider.java +++ b/spring-boot-integration/src/main/java/com/graphql/java/examples/performance/checks/GraphQLProvider.java @@ -1,4 +1,4 @@ -package graphql.examples.springboot; +package com.graphql.java.examples.performance.checks; import com.google.common.base.Charsets; import com.google.common.io.Resources; diff --git a/spring-boot-integration/src/test/java/com/graphql/java/examples/performance/checks/ApplicationTests.java b/spring-boot-integration/src/test/java/com/graphql/java/examples/performance/checks/ApplicationTests.java new file mode 100644 index 0000000..86f56e4 --- /dev/null +++ b/spring-boot-integration/src/test/java/com/graphql/java/examples/performance/checks/ApplicationTests.java @@ -0,0 +1,16 @@ +package com.graphql.java.examples.performance.checks; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApplicationTests { + + @Test + public void contextLoads() { + } + +}