Graal “The Holy Grail” VM (Part II)
Part I: https://mohammedzu.medium.com/graal-the-holy-grail-vm-part-i-9b0c074088be
Let’s complete the Graal saga with some hands-on…
I am going to demonstrate with a test project using Quarkus, a simple rest api called ‘yo yo!’ ← Name rings a bell? yes yes, the project which fetched 1M investment in no time, back in the web 2.0 craziness days… phew! and now we are doing web3… deja vu!
The best thing about being in the valley is FOMO, and equally the worst, if you don’t have the nerve to keep up with the craziness, to keep learning and to keep racing then…. run away as far as you can… even Corona and WFH cannot undo the valley cult!!
Ah the demo… it’s simple, follow along.
1. Go to quarkus.io and generate a new project selecting gradle and resteasy
2. fire up IntelliJ (this isn’t a vi vs emacs discussion… if you ain’t using intellij, yet can still blurt out java code, you are a Demi god!)
3. open “main/java/it.graal/GreetingsResource” (don’t forget to comment out the test cases)
4. GreetingsResource thats a boring name, so i refactored it to RestYo
5. The cheapest of code for a ReST resource i can blurt out
@Path("/graal")
public class RestYo {
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/yoyo/{me}")
public Response yo(
@PathParam String me
) {
return Response.ok(String.format("Yo! Yo! %s\n", me)).build();
}
}
6. Resteasy really makes it easy, the code is self explanatory i guess, if not then all it does is defines a root level path /graal, and a GET handler for /yoyo/<path-parameter>, which simply responds with “Yo! Yo! <path-parameter>”, like below:
~ curl http://localhost:8080/graal/yoyo/mz
Yo! Yo! mz
That’s it, that’s our demo la grande! but the real deal is to see the memory and run-time superiority of Graal over JVM, right! moving on…
7. Get the GraalVM 21.3 community edition from here , i used the darwin build for mac here
8. Unzip to some place cool
9. Set the environment and install the native image:
➜ ~ export GRAALVM_HOME=/your/path/to/graalvm-ce-java11–21.3.0/Contents/Home/
➜ ~$GRAALVM_HOME/bin/gu install native-image10. Fire up a Gradle build (you can use maven too but i really like gradle, without the xml quirks), It takes a lot more time to build than conventional jar build (you know the “AOT” thingy from Part I) have some patience, its really worth gold here and when its done, voila! you have the executable (called runner) and the uber-jar both at your disposal in the build folder:
11. Let’s fire it up, first JVM, next GRAAL, standard stuff, run with a time prefix and a top -pid <pid> for checking memory
DONE! Now….. What did your keen eyes notice?
Clue: Zoom in to “Started in…” and “MEM”
- The startup time: JVM is 0.924s, for Graal its 0.023s, beat that!
- Next Memory: JVM (108 freaking megs…. lol) and Graal (10 megs) …. this is a no contest! cold start, what cold start
You know what makes this even better, you guessed it, DORA (docker once run anywhere) but thats for Part III and what makes it a killer? is to dora and fly a kite (you know k8s… sounds like a kite, no? you like “kubernetes” better, sure!) thats for part 4.