spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Support custom TimeUnits in StopWatch

Open ak98neon opened this issue 4 years ago • 1 comments

Add TimeUnit support in StopWatch util class.

  • Provide StopWatch#getLastTime and StopWatch#getTotalTime
  • Modify the print format for tasks / include default TimeUnit in nanoseconds

See #25803

ak98neon avatar Sep 24 '20 19:09 ak98neon

Precisely what I was looking for. I don't know how many nanoseconds are in one second, so this will simplify my life.

msangel avatar Sep 11 '22 12:09 msangel

The function itself is implemented, but it hasn't passed the CI yet. So won't this PR be merged in the foreseeable future? It is very useful for debugging and I would like to use it in a version, so when will this PR be merged? @ak98neon

ventulus95 avatar May 02 '23 04:05 ventulus95

@ventulus95 it's in the general backlog so there's no commitment on our end. That said, I am not sure we should implement it this way. We could reuse Duration which sounds a more rich concept for what you're trying to do.

snicoll avatar May 02 '23 16:05 snicoll

I don't see the need for a new group of TimeUnit-based accessors and see them as rather confusing next to our existing getLaskTaskTimeX and getTotalTimeX accessors, in particular if the parameterless variant returns the time in a unit based on a StopWatch instance property.

That said, I do see the point with prettyPrint. The pre-5.2 representation with milliseconds was arguably more useful than the present implementation is. This can be addressed through an overloaded prettyPrint(TimeUnit) method which I'm inclined to introduce under #25803.

That aside, there is the question of Duration-returning time accessors... and of ChronoUnit for prettyPrint if java.time alignment is more appropriate than java.util.concurrent alignment. Either way, this is worth a broader revision for 6.1.

jhoeller avatar Aug 07 '23 14:08 jhoeller

See https://github.com/spring-projects/spring-framework/issues/25803#issuecomment-1668705268 for a summary of the 6.1 revision. This ended up preferring TimeUnit based accessors over Duration since the double precision is rather important here. Note that there is no instance-level TimeUnit setting, just a TimeUnit method parameter for specific accessors.

Thanks for the pull request and the specific proposal! This turned into very useful inspiration, I hope the end result is appropriate for your purposes.

jhoeller avatar Aug 07 '23 23:08 jhoeller