Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom TimeUnits in StopWatch #25803

Closed
ImSejin opened this issue Sep 23, 2020 · 4 comments
Closed

Support custom TimeUnits in StopWatch #25803

ImSejin opened this issue Sep 23, 2020 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@ImSejin
Copy link

ImSejin commented Sep 23, 2020

I use spring-core:5.2.9.RELEASE.

When I invoke StopWatch#prettyPrint,

StopWatch '': running time = 9624841500 ns
---------------------------------------------
ns         %     Task name
---------------------------------------------
1202450400  012%  createRandoms
8422391100  088%  write

I want StopWatch to provide setTimeUnit(java.util.concurrent.TimeUnit), getLastTime(TimeUnit) and getTotalTime(TimeUnit); the return type of these methods is double.

For example,

StopWatch stopWatch = new StopWatch();
stopWatch.setTimeUnit(TimeUnit.MINUTES); // If not set, StopWatch is applied default TimeUnit.
stopWatch.start();

/* working... */

stopWatch.stop();
getTotalTime(TimeUnit.SECONDS); // 9.6248415
getTotalTime(); // 0.160414025 / Without param, it depends on StopWatch#timeUnit
System.out.println(stopWatch.prettyPrint());

is like ...

StopWatch '': running time = 0.160414025 min
---------------------------------------------
min         %     Task name
---------------------------------------------
0.02004084   012%  createRandoms
0.140373185  088%  write

Thanks!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 23, 2020
@sbrannen sbrannen changed the title StopWatch supports TimeUnit Support custom TimeUnits in StopWatch Sep 23, 2020
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 23, 2020
@sbrannen sbrannen added this to the General Backlog milestone Sep 23, 2020
@sbrannen
Copy link
Member

Thanks for creating this issue.

I can see that this might be useful in some use cases, but this enhancement does not currently have high priority for us. Thus, I have assigned it to the General Backlog to allow this issue to gain interest from the community.

@teddylear
Copy link

@sbrannen It alright if I work on this issue?

@sbrannen sbrannen linked a pull request Sep 25, 2020 that will close this issue
@sbrannen
Copy link
Member

@sbrannen It alright if I work on this issue?

Thanks for the offer, but @ak98neon already submitted PR #25813.

@sbrannen sbrannen removed this from the General Backlog milestone Sep 25, 2020
@sbrannen sbrannen added the status: superseded An issue that has been superseded by another label Sep 25, 2020
ak98neon added a commit to ak98neon/spring-framework that referenced this issue Oct 20, 2022
# Conflicts:
#	spring-core/src/test/java/org/springframework/util/StopWatchTests.java
@jhoeller jhoeller removed the status: superseded An issue that has been superseded by another label Aug 7, 2023
@jhoeller jhoeller self-assigned this Aug 7, 2023
@jhoeller jhoeller added this to the 6.1.0-M4 milestone Aug 7, 2023
@jhoeller jhoeller reopened this Aug 7, 2023
@jhoeller
Copy link
Contributor

jhoeller commented Aug 7, 2023

I ended up switching the default time unit for String renderings to seconds with decimal points in nanosecond precision since the scale is easier to see then, while retaining the same precision level as in the 5.2 revision.

Custom renderings with specific time units can be requested through prettyPrint(TimeUnit), and custom time values can be obtained via getTotalTime(TimeUnit) and TaskInfo.getTime(TimeUnit) with double precision. The prettyPrint table automatically adapts to the scale of the total time reported, rendering the specific task times at the same scale.

The getLaskTaskX methods have been deprecated in favor of nested lastTaskInfo().getX calls. A seconds accessor was missing there, so this was inconsistent anyway, and the new TimeUnit-based accessor is only available on TaskInfo now. Note that there is no instance-level TimeUnit setting, just a TimeUnit method parameter for specific accessors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants