Use System.nanoTime() in StopWatch
Prior to this commit, StopWatch used System.currentTimeMillis() to track and report running time in milliseconds. This commit updates the internals of StopWatch to use System.nanoTime() instead of System.currentTimeMillis(). Consequently, running time is now tracked and reported in nanoseconds; however, users still have the option to retrieve running time in milliseconds or seconds. Closes gh-23235
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.util.StopWatch.TaskInfo;
|
||||
@@ -39,8 +41,8 @@ public class StopWatchTests {
|
||||
private static final String name2 = "Task 2";
|
||||
|
||||
private static final long duration1 = 200;
|
||||
private static final long duration2 = 50;
|
||||
private static final long fudgeFactor = 20;
|
||||
private static final long duration2 = 100;
|
||||
private static final long fudgeFactor = 50;
|
||||
|
||||
private final StopWatch stopWatch = new StopWatch(ID);
|
||||
|
||||
@@ -70,30 +72,44 @@ public class StopWatchTests {
|
||||
@Test
|
||||
public void validUsage() throws Exception {
|
||||
assertThat(stopWatch.isRunning()).isFalse();
|
||||
|
||||
stopWatch.start(name1);
|
||||
Thread.sleep(duration1);
|
||||
assertThat(stopWatch.isRunning()).isTrue();
|
||||
assertThat(stopWatch.currentTaskName()).isEqualTo(name1);
|
||||
stopWatch.stop();
|
||||
assertThat(stopWatch.isRunning()).isFalse();
|
||||
|
||||
assertThat(stopWatch.getLastTaskTimeNanos())
|
||||
.as("last task time in nanoseconds for task #1")
|
||||
.isGreaterThanOrEqualTo(millisToNanos(duration1))
|
||||
.isLessThanOrEqualTo(millisToNanos(duration1 + fudgeFactor));
|
||||
assertThat(stopWatch.getTotalTimeMillis())
|
||||
.as("Unexpected timing " + stopWatch.getTotalTimeMillis())
|
||||
.isGreaterThanOrEqualTo(duration1);
|
||||
assertThat(stopWatch.getTotalTimeMillis())
|
||||
.as("Unexpected timing " + stopWatch.getTotalTimeMillis())
|
||||
.as("total time in milliseconds for task #1")
|
||||
.isGreaterThanOrEqualTo(duration1)
|
||||
.isLessThanOrEqualTo(duration1 + fudgeFactor);
|
||||
assertThat(stopWatch.getTotalTimeSeconds())
|
||||
.as("total time in seconds for task #1")
|
||||
.isGreaterThanOrEqualTo(duration1 / 1000.0)
|
||||
.isLessThanOrEqualTo((duration1 + fudgeFactor) / 1000.0);
|
||||
|
||||
stopWatch.start(name2);
|
||||
Thread.sleep(duration2);
|
||||
assertThat(stopWatch.isRunning()).isTrue();
|
||||
assertThat(stopWatch.currentTaskName()).isEqualTo(name2);
|
||||
stopWatch.stop();
|
||||
|
||||
assertThat(stopWatch.isRunning()).isFalse();
|
||||
assertThat(stopWatch.getLastTaskTimeNanos())
|
||||
.as("last task time in nanoseconds for task #2")
|
||||
.isGreaterThanOrEqualTo(millisToNanos(duration2))
|
||||
.isLessThanOrEqualTo(millisToNanos(duration2 + fudgeFactor));
|
||||
assertThat(stopWatch.getTotalTimeMillis())
|
||||
.as("Unexpected timing " + stopWatch.getTotalTimeMillis())
|
||||
.isGreaterThanOrEqualTo(duration1 + duration2);
|
||||
assertThat(stopWatch.getTotalTimeMillis())
|
||||
.as("Unexpected timing " + stopWatch.getTotalTimeMillis())
|
||||
.as("total time in milliseconds for tasks #1 and #2")
|
||||
.isGreaterThanOrEqualTo(duration1 + duration2)
|
||||
.isLessThanOrEqualTo(duration1 + duration2 + fudgeFactor);
|
||||
assertThat(stopWatch.getTotalTimeSeconds())
|
||||
.as("total time in seconds for task #2")
|
||||
.isGreaterThanOrEqualTo((duration1 + duration2) / 1000.0)
|
||||
.isLessThanOrEqualTo((duration1 + duration2 + fudgeFactor) / 1000.0);
|
||||
|
||||
assertThat(stopWatch.getTaskCount()).isEqualTo(2);
|
||||
assertThat(stopWatch.prettyPrint()).contains(name1, name2);
|
||||
@@ -106,24 +122,26 @@ public class StopWatchTests {
|
||||
public void validUsageDoesNotKeepTaskList() throws Exception {
|
||||
stopWatch.setKeepTaskList(false);
|
||||
|
||||
assertThat(stopWatch.isRunning()).isFalse();
|
||||
|
||||
stopWatch.start(name1);
|
||||
Thread.sleep(duration1);
|
||||
assertThat(stopWatch.isRunning()).isTrue();
|
||||
assertThat(stopWatch.currentTaskName()).isEqualTo(name1);
|
||||
stopWatch.stop();
|
||||
assertThat(stopWatch.isRunning()).isFalse();
|
||||
|
||||
stopWatch.start(name2);
|
||||
Thread.sleep(duration2);
|
||||
assertThat(stopWatch.isRunning()).isTrue();
|
||||
assertThat(stopWatch.currentTaskName()).isEqualTo(name2);
|
||||
stopWatch.stop();
|
||||
assertThat(stopWatch.isRunning()).isFalse();
|
||||
|
||||
assertThat(stopWatch.getTaskCount()).isEqualTo(2);
|
||||
assertThat(stopWatch.prettyPrint()).contains("No task info kept");
|
||||
assertThat(stopWatch.toString()).doesNotContain(name1, name2);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(stopWatch::getTaskInfo);
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class)
|
||||
.isThrownBy(stopWatch::getTaskInfo)
|
||||
.withMessage("Task info is not being kept!");
|
||||
}
|
||||
|
||||
private static long millisToNanos(long duration) {
|
||||
return TimeUnit.NANOSECONDS.convert(duration, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user