Monitoring JVM during a JMeter test
In past days I’m working to improve how we are collecting data about our shift-left tests executed using JMeter. We use to monitor the test running using Prometheus + Graphana or VisualVM, but we don’t have a way to save a report about used resources exactly during the test execution. We want to save data about memory allocation, CPU usage, loaded classes, and running thread exactly from when the test starts to when the test ends.
tl;dr: Jumps to a sample project on Github Gist!
Actually, we would like to have a snapshot view like the monitor tab in VisualVM, but, we wouldn’t do that manually.
Fortunately, we found a beautiful plugin named JMXMon that can read all we need using attributes exposing via JMX. As said before, we decide to collect data and build graphs about 4 values that are exposed by the attributes below:
- Memory: java.lang:type=Memory HeapMemoryUsage used
- Thread: java.lang:type=Threading ThreadCount
- CPU: java.lang:type=OperatingSystem ProcessCpuLoad
- Loaded classes: java.lang:type=ClassLoading LoadedClassCount
In our scenario, we use to execute our tests on an application deployed on Kubernetes, so we need to do a port-forward to connect to JMX:
kubectl port-forward mypod-name-7799bc9d48-cwjdd 9010:9010
Cool! Now we have all we need, but the graph is a little bit confusing. The values scale is very different and the graph is a little bit unclear.
So, we decide to split into 4 different graphs:
Moreover, we also decide to store collected values in a CSV file so we can keep this file to compare with previous versions.
But sometimes may images can be worth a thousand words. So we need to automatically save also the generated graphs. We right-click on each image and saves it manually, but, in fact, this isn’t automatic and in our days we need to automatic everything!
After searching and trying some plugin, we decide to create a simple Groovy script that will use JFreeChart to generate some simple charts starting from CSV reports.
The script must be executed at the end of the test, after the report creation on disk. The reports will be saved only when the test ends, but the tests never end if all steps aren’t completed. Panic! So, in order to do that we will execute the graph generation into another thread started in the script and check if the report exists.
More details and a little JMeter script example can be viewed here: https://gist.github.com/jesty/b478cbd4341231286ff60085f9646407
The images above are the generated graphs for our reports. So we can commit all in our repository in order to monitor the system performance during the time and compare it with a previous version. Cool!