Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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?

like image 585
PassfishSwordword Avatar asked Sep 25 '22 21:09

PassfishSwordword


1 Answers

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.

like image 65
dds Avatar answered Oct 03 '22 16:10

dds