Wednesday 1 June 2016

Testing a Java method's execution time and memory consumption via JUnit




I want to profile how a method's execution time and memory consumption scale depending on the method's parameters. So far, I have come up with testing the method via JUnit and running several tests for different sets of parameters (e.g. using JUnit Theories). In order to abstract from overhead due to setting up tests etc. the time is calculated by a difference in System.currentTimeMillis right before and after the step I am interested in. This seems somewhat hacky, is there a more natural way to do this?




I would furthermore like to see how memory consumption behaves with different parameters. JVisualVM comes to mind, but is there a more elegant way to automatically get what I want?


Answer



I suggest you using JMH java benchmark framework.




JMH is a Java harness for building, running, and analysing
nano/micro/milli/macro benchmarks written in Java and other languages
targetting the JVM.



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...