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

Comment From: msangel

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

Comment From: ventulus95

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

Comment From: snicoll

@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.

Comment From: jhoeller

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.

Comment From: 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.