4

I need to measure performances on a specific part of my application. To calculate these measures, I use the Spring StopWatch class. In a method, there is a loop where I need to measure the execution of each iteration.

What I would like is to have, for the loop part, a counter, and the min / max / average values. Unfortunately, Spring StopWatch class does not provides such feature.

For example, the following code:

StopWatch watch = new StopWatch("foo");
watch.start("First process");
doSomething();
watch.stop();
for (int i = 0; i < 10; i++) {
    watch.start("loop");
    doAnotherThing();
    watch.stop();
}
System.out.println(watch.prettyPrint());

will output:

StopWatch 'foo': running time (millis) = 1469
-----------------------------------------
ms     %     Task name
-----------------------------------------
01000  068%  First process
00078  005%  loop
00000  000%  loop
00016  001%  loop
00047  003%  loop
00015  001%  loop
00047  003%  loop
00094  006%  loop
00000  000%  loop
00109  007%  loop
00063  004%  loop

What I want is to have:

StopWatch 'foo': running time (millis) = 1469
-----------------------------------------
ms     %     Task name
-----------------------------------------
01000  068%  First process
00469  032%  loop | min: 0 ; max: 109 ; average: 47 ; count: 10

Is there a way to achieve that using Spring?

ps: I know that perf4j will allow that, but if I can avoid adding a new library, it would be great...

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273

1 Answers1

0

I'm not aware of any such class in Spring

I'd encapsulate into your own class if you'd prefer not to add Perf4J.

A general Statistics class is a handy thing to have. Just be sure to make it thread safe.

duffymo
  • 305,152
  • 44
  • 369
  • 561