Micro-benchmarking Java Code With Caliper

Posted on September 5, 2012

Benchmarking Java code can be tricky, as the Java's JIT compiler optimizes the code for high-speed execution. But there are cases where you can test Java code for performance statistics. Caliper is one such tool, which can be used to benchmark java code. More precisely, it is a Micro-Benchmarking tool, used only for testing the performance of small units of Java code.

To test how Caliper works, I worked on a sample Maven based project. The code that I wanted to test is to check if a Java's HashMap contains a key and get it, if the key exists. The usual way of doing this is:

if(someMap.containsKey(key)) {
    someValue = someMap.get(key);
But, the below code performs better:

someValue = someMap.get(key);
if(someValue != null) {}
Although 'containsKey' operation is the same as 'get', the above code performs better, as containsKey operation does some additional work. To micro-benchmark this code, I've worked on a simple project and the sources are listed below: Below is the pom.xml dependency for Caliper:


And below is the code for the Caliper Demo class file:

Below are the results for the above test (displaying both 'linear' and 'logarithmic' run-times):

After running the above test, I ran the same code with different length's, by changing the @Param values to "@Param({"50", "100", "1000", "10000"})" and the results are as below (displaying both 'linear' and 'logarithmic' times):

PS: Premature Optimization can still be the root of all evils. :P

Blog Categories
The views expressed on this blog are my personal views and do not reflect the views of my employer or campaigns I am supporting.

All sample code is provided for illustrative purposes only. These examples have not been thoroughly tested under all conditions. The writer therefore, cannot guarantee or imply reliability, serviceability, or function of these programs.

All programs contained herein are provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.