This demo illustrates how GraalWasm can be used to embed Photon, a WebAssembly image processing library written in Rust, in a Spring Boot application. The demo also uses GraalJS to access the Photon module through the WebAssembly JavaScript API.
Install GraalVM for JDK 23 and set the value of JAVA_HOME
accordingly.
We recommend using SDKMAN!. (For other download options, see GraalVM Downloads.)
sdk install java 23-graal
To start the demo, simply run:
./mvnw package spring-boot:run
When the demo runs, open the following URLs in a browser:
- http://localhost:8080/photo/default
- http://localhost:8080/photo/grayscale
- http://localhost:8080/photo/colorize
- http://localhost:8080/photo/fliph
- http://localhost:8080/photo/flipv
To compile the application with GraalVM Native Image, run:
./mvnw -Pnative native:compile
./target/demo
To use Profile-Guided Optimization, run the following commands:
# Compile and run instrumented image
./mvnw -Pnative,pgo-instrument native:compile
./target/demo-g1-pgo-instrument
# Produce some load, for example using https://github.com/rakyll/hey
hey -c 8 -z 2m http://localhost:8080/photo/flipv
# Quitting the demo-g1-pgo-instrument process will generate a profile file (default.iprof)
# Compile and run optimized image
./mvnw -Pnative,pgo native:compile
./target/demo-g1-pgo
The DemoController
uses a PhotonService
to implement the /photo/{effectName}
endpoint.
This service accesses Photon
objects that are pooled in a PhotonPool
to check whether an effect for a given effectName exists before applying the effect to a sample image.
PhotonPool
creates a Photon
object for each available processor, which in turn holds a reference to the corresponding photonModule
and an imageContent
object.
Both of these objects are from JavaScript and backed by the same Context
.
Note that the Context
objects share the same Engine
, to improve warmup and memory footprint.
Also note that the Photon JavaScript and WebAssembly modules as well as the sample images are downloaded when the demo is built with the wagon-maven-plugin
Maven plugin (see pom.xml).
The DemoApplicationTests
tests that applying the same effect on two copies of the same image yields the same result, regardless of the effect used.
Run it with ./mvnw test
.