Skip to content

A dockerized application exemplifying copying files in Java and serving files in Spring Webflux with and without "Zero Copy" mechanism

Notifications You must be signed in to change notification settings

maxxhuang/zero-copy-in-java-and-webflux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zero-copy-in-java-and-webflux

This application contains code examples to

  1. demonstrate how to copy files using JDK API with and without zero-copy mechanism.
  2. showcase several ways to serve static files in Spring WebFlux and measure the CPU usage.

It comes with no surprise that serving static files with zero-copy outperforms the one without zero-copy.

Building executable jar

Spring Boot provides spring-boot-maven-plugin that packages application classes along with all dependency jars into one single jar. Check out here for more details.

With the spring boot maven plugin specified in pom.xml, creating a Spring Boot executable jar is as simple as

./mvnw clean package

which generates a repackaged jar (executable jar) under target folder (target/zerocopy-0.0.1-SNAPSHOT.jar).

Building docker image

target/zerocopy-0.0.1-SNAPSHOT.jar needs to be available before building docker image. With the jar in place, run

docker build -t 10am/zerocopyapp .

Replace 10am/zerocopy with any tag name you prefer.

Run the file server

For this file server to be able to report metrics, we need to bring up a graphite server. In this example, we'll use a predefined dockerized graphite server by alex-mercer:

docker run \
-p 2003:2003 \
-p 3000:3000 \
-p 8888:80 \
-d \
alexmercer/graphite-grafana

With the graphite server up and running, we can proceed to start the file server:

docker run \
-e MONITOR_INTERVAL=[metric reporting interval in seconds] \
-e GRAPHITE_IP=[graphite server ip] \
-e GRAPHITE_PORT=[graphite server port] \
-p 8080:8080 \
10am/zerocopyapp

Again, 10am/zerocopyapp should be replaced with your own choice.

Among the environmental variables above, GRAPHITE_IP is mandatory while GRAPHITE_PORT (defaults to 2003) and MONITOR_INTERVAL (defaults to 10) are optional and you can safely skip them.

Run the command to keep the file server busy.

zero copy

while true; do curl -v [file server ip]:8080/zerocopy1 > /dev/null; done

non zero copy

while true; do curl -v [file server ip]:8080/nonzerocopy > /dev/null; done

Observing CPU metrics

While the file server is working to serve files, go to http://[graphite server ip]:8888 to observe the metrics in graphite web.

Or if you prefer a fancy dashboard and you happen to have a grafana server brought up just as the one we gave above (alexmercer/graphite-grafana), go to http://[grafana server ip]:3000 to create your own modern metrics dashboard.

About

A dockerized application exemplifying copying files in Java and serving files in Spring Webflux with and without "Zero Copy" mechanism

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published