How to implement maintainable performance tests using Docker and Java.
Let's start by first defining what a functional test actually is. There seem to be multiple definitions, here is mine:
A functional test is a test which will test the exposed application features (e.g.: internal or REST API's ) with fewer or no mocks compared to unit tests. Functional tests may also interact with databases or other external services.
For Gentics Mesh we already have a lot of functional tests. Performance tests help to tell whether a change or even a dependency update actually improves or degrades the service performance. The functional tests for Gentics Mesh are written using JUnit. Please keep in mind that a JUnit test must not necessarily be a unit test. In the case of Mesh we have a lot of functional tests which start up an embedded Mesh instance and invoke regular REST requests via HTTP.
So let's see what kind of performance tests are common:
These tests usually test the system load performance. JMeter and Gatling are tools which are commonly employed for these tests. Test situations must be created or recorded up-front. Gatling and JMeter for example are mainly used for testing HTTP APIs. There are various Jenkins CI plugins with allow you to plot neat graphs that visualize your JMeter test results. I think it is even possible to define test thresholds.
Typical stopwatch tests are tests in which you just measure the duration it takes to execute a specific task. This is a very easy method to write performance tests since you can reuse your regular functional tests.
A typical Gentics Mesh stopwatch performance test in a simplified form could look like this:
There are a few things that may be noteworthy:
Stopwatch tests can also be used to assert the performance of specific internal calls like graph database traversals or cache access speed.
While researching options for executing stopwatch performance tests I was wondering whether it is possible to execute a JUnit test remotely and start it from within my Eclipse IDE. Executing the test remotely would allow me to run the test on a dedicated server and thus the performance would always be the same since the hardware is the same. My CI would also utilize this server for executing the test. I could even setup two identical servers in order to be able to write performance tests while also execute CI tests using the other server.
It is possible to create a docker container which contains all needed test classes. Instead of executing the regular $JAVA_HOME/bin/java executable, a bash script will be invoked which creates a test image and executes the JVM within that image.
I just copied my JDK 1.8 folder and replaced the regular java binary with the bash script below. In Eclipse I only had to add this modified JDK folder and invoke the test using that JDK. This way the bash script below was executed by my Eclipse IDE. Next I needed to prepare the base image. The base image needs to contain the JDK. In my case I used the the registry.office/docker-jenkins-slave:latest image which is an internal Jenkins slave docker image which we use to execute our Jenkins jobs.
The bash script will split up the classpath and copy all needed files and folders to the BUILDDIR folder. After that a docker build will be invoked which create a docker image that contains all those files and folders. Then docker run will be used to execute the JVM within the image. It is important to note that your system hostname needs to be specified. The JVM process within the docker image will connect to your IDE in order to visualize the JUnit test results. Finally the build docker image is no longer needed and can be purged from the docker host.
Please keep in mind that the bash script and process will most likely not work on Windows, but it should be possible to transform the Windows classpath to a classpath which Linux understands.
Of course there are also some drawbacks to this solution:
Thanks for reading our blog! Try our API-first CMS Gentics Mesh for free!