Skip to content

Add performance checks example #6

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 2 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions performance-checks/.gitignore
Original file line number Diff line number Diff line change
@@ -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/
122 changes: 122 additions & 0 deletions performance-checks/README.md
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 33 additions & 0 deletions performance-checks/build.gradle
Original file line number Diff line number Diff line change
@@ -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')
}
Binary file not shown.
6 changes: 6 additions & 0 deletions performance-checks/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
172 changes: 172 additions & 0 deletions performance-checks/gradlew
Original file line number Diff line number Diff line change
@@ -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" "$@"
Loading