DEV Community

Cover image for Groovy script startup time from 2.1s to 0.013s with GraalVM πŸš€
Szymon Stepniak
Szymon Stepniak

Posted on

Groovy script startup time from 2.1s to 0.013s with GraalVM πŸš€

If you're like me, you use Groovy for scripting automated tasks. I appreciate that I can implement a powerful script executed on JVM in a single file, and I can do it in just several code lines. The only downside is a long startup time - Groovy needs to start the JVM and compile the code before it gets executed. It may not be a problem in most cases. However, if your script does its job in, let's say, 100 milliseconds, spending additional 1.5-2 seconds sounds like a waste of time.

In this video, I explain how to compile a Groovy script (compatible with Groovy 2.x) to the native executable file using Groovy 3 and GraalVM 20.2 (OpenJDK 11)

Source code

gttp - simple HTTP server in Groovy

Inspired by http://melix.github.io/blog/2019/03/simple-http-server-graal.html

Install Groovy

$ sdk install groovy 3.0.5

Install GraalVM 20.2 (JDK 11)

$ sdk install java 20.2.0.r11-grl

$ sdk use java 20.2.0.r11-grl

$ gu install native-image

Compile gttp

$ groovyc --compile-static gttp.groovy

Run gttp with native-image-agent

$ java -agentlib:native-image-agent=config-output-dir=conf/ -cp ".:$GROOVY_HOME/lib/groovy-3.0.5.jar" gttp 

Build native image

$ native-image --allow-incomplete-classpath \
  --report-unsupported-elements-at-runtime \
  --initialize-at-build-time \
  --initialize-at-run-time=org.codehaus.groovy.control.XStreamUtils \
  --no-fallback \
  --no-server \
  -H:ConfigurationFileDirectories=conf/ \
  -cp ".:$GROOVY_HOME/lib/groovy-3.0.5.jar" \ 
  gttp

Run gttp as a standalone executable file

$ ./gttp
Listening at http://localhost:8080/

Change default port

$ ./gttp 9000
Listening at http://localhost:9000/

Change default base directory to /tmp

$ ./gttp 9000 /tmp
Listening at http://localhost:9000/

Alternative: setup environment & build in a docker container

You can also use attached Dockerfile to install all required components, compile Groovy script to Java bytecode, and generate standalone executable file using native-image tool.

$ sh ./build-with-docker.sh



Shownotes

Step 0: Install GraalVM and Groovy using SDKMAN!

  • Install SDKMAN - https://sdkman.io/install
  • List available Java versions - sdk list java
  • Install the latest GraalVM - sdk install java 20.2.0.r11-grl (NOTE: version 20.2.0.r11-grl is the latest at the time of writing this article.)
  • Switch to GraalVM (if not set as default) - sdk use java 20.2.0.r11-grl
  • Install native-image tool - gu install native-image
  • Install Groovy 3.0.5 (or other latest) - sdk install groovy 3.0.5
  • Use installed Groovy (if not set as default) - sdk use groovy 3.0.5

Step 1: Clone gttp repository

git clone https://github.com/wololock/gttp.git

cd gttp
Enter fullscreen mode Exit fullscreen mode

Step 2: Run program as a Groovy script

groovy gttp.groovy
Enter fullscreen mode Exit fullscreen mode

Step 3: Compile script using groovyc

groovyc --compile-static gttp.groovy
Enter fullscreen mode Exit fullscreen mode

Step 4: Run Groovy script as a Java program

export CP=".:$GROOVY_HOME/lib/groovy-3.0.5.jar"

java -cp $CP gttp
Enter fullscreen mode Exit fullscreen mode

Step 5: Generate GraalVM native-image metadata

java -agentlib:native-image-agent=config-output-dir=conf/ -cp "$CP" gttp
Enter fullscreen mode Exit fullscreen mode

Step 6: Compile Groovy script to the native image

native-image --allow-incomplete-classpath \
--report-unsupported-elements-at-runtime \
--initialize-at-build-time \
--initialize-at-run-time=org.codehaus.groovy.control.XStreamUtils \
--no-fallback \
--no-server \
-H:ConfigurationFileDirectories=conf/ \
-cp $CP gttp
Enter fullscreen mode Exit fullscreen mode

Step 7: Run the program

./gttp
Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)