Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method's execution time in java

I want to print the execution time for a method in java. Currently am using the following code,

long startTime = System.currentTimeMillis();
// method body
long runTime= System.currentTimeMillis() - startTime;
System.out.println(" XYZ Method execution time: " + runTime);

There are so many methods for which I would like to find the runtime in the project, is there any way to prevent writing this piece of code in every method?

The project uses Spring 2.5, Struts 2.

Edit:I do not want to add the logic to every method, I would like to, say, write the logic once and use some kind of configuration where I can specify the methods for which I need to print the execution time. So whenever the method is executed, automatically the run time should get printed to the console. One way is as said by me is to add the code to prologue and epilogue, but I do not want to edit the method as that would be time consuming and writing the same code wont be a good practice.

like image 625
NEO Avatar asked Jan 14 '23 23:01

NEO


1 Answers

I recommend you take a look at the Metrics package from Codahale. This package collects various metrics (including timing) which is then reported via JMX or other mechanisms.

like image 152
Jordan S. Jones Avatar answered Jan 19 '23 12:01

Jordan S. Jones