You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/11/14 14:27:11 UTC
[commons-lang] branch master updated: Add StopWatch.getStopTime().
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new c972213 Add StopWatch.getStopTime().
c972213 is described below
commit c9722131d50de2a78aaf79a222fa5135431b9a73
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Nov 14 09:27:04 2020 -0500
Add StopWatch.getStopTime().
Also:
- Rename private instance variable to reflect scale.
- More precise Javadocs.
- Throw IllegalStateException instead of RuntimeException.
- Refactor common code in test for sleeping the current thread.
---
src/changes/changes.xml | 1 +
.../org/apache/commons/lang3/time/StopWatch.java | 75 +++++++++----
.../apache/commons/lang3/time/StopWatchTest.java | 117 ++++++++-------------
3 files changed, 98 insertions(+), 95 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 316f355..59dd93d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -56,6 +56,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.booleanValues().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.primitiveValues().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add StringUtils.containsAnyIgnoreCase(CharSequence, CharSequence...).</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getStopTime().</action>
<action type="add" dev="ggregory" due-to="Edgar Asatryan">More test coverage for CharSequenceUtils. #631.</action>
<!-- UPDATES -->
<action type="update" dev="chtompki">Bump junit-jupiter from 5.6.2 to 5.7.0.</action>
diff --git a/src/main/java/org/apache/commons/lang3/time/StopWatch.java b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
index 3be7545..d3b9398 100644
--- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
+++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
@@ -205,21 +205,28 @@ public class StopWatch {
private SplitState splitState = SplitState.UNSPLIT;
/**
- * The start time.
+ * The start time in nanoseconds.
*/
- private long startTime;
+ private long startTimeNanos;
/**
- * The start time in Millis - nanoTime is only for elapsed time so we
+ * The start time in milliseconds - nanoTime is only for elapsed time so we
* need to also store the currentTimeMillis to maintain the old
* getStartTime API.
*/
private long startTimeMillis;
/**
- * The stop time.
+ * The end time in milliseconds - nanoTime is only for elapsed time so we
+ * need to also store the currentTimeMillis to maintain the old
+ * getStartTime API.
+ */
+ private long stopTimeMillis;
+
+ /**
+ * The stop time in nanoseconds.
*/
- private long stopTime;
+ private long stopTimeNanos;
/**
* <p>
@@ -273,7 +280,7 @@ public class StopWatch {
/**
* <p>
- * Gets the time on the stopwatch in nanoseconds.
+ * Gets the <em>elapsed</em> time in nanoseconds.
* </p>
*
* <p>
@@ -281,23 +288,24 @@ public class StopWatch {
* start and stop.
* </p>
*
- * @return the time in nanoseconds
+ * @return the <em>elapsed</em> time in nanoseconds.
+ * @see System#nanoTime()
* @since 3.0
*/
public long getNanoTime() {
if (this.runningState == State.STOPPED || this.runningState == State.SUSPENDED) {
- return this.stopTime - this.startTime;
+ return this.stopTimeNanos - this.startTimeNanos;
} else if (this.runningState == State.UNSTARTED) {
return 0;
} else if (this.runningState == State.RUNNING) {
- return System.nanoTime() - this.startTime;
+ return System.nanoTime() - this.startTimeNanos;
}
- throw new RuntimeException("Illegal running state has occurred.");
+ throw new IllegalStateException("Illegal running state has occurred.");
}
/**
* <p>
- * Gets the split time on the stopwatch in nanoseconds.
+ * Gets the split time in nanoseconds.
* </p>
*
* <p>
@@ -312,9 +320,9 @@ public class StopWatch {
*/
public long getSplitNanoTime() {
if (this.splitState != SplitState.SPLIT) {
- throw new IllegalStateException("Stopwatch must be split to get the split time. ");
+ throw new IllegalStateException("Stopwatch must be split to get the split time.");
}
- return this.stopTime - this.startTime;
+ return this.stopTimeNanos - this.startTimeNanos;
}
/**
@@ -337,11 +345,12 @@ public class StopWatch {
}
/**
- * Gets the time this stopwatch was started.
+ * Gets the time this stopwatch was started in milliseconds, between the current time and midnight, January 1, 1970
+ * UTC.
*
- * @return the time this stopwatch was started
- * @throws IllegalStateException
- * if this StopWatch has not been started
+ * @return the time this stopwatch was started in milliseconds, between the current time and midnight, January 1,
+ * 1970 UTC.
+ * @throws IllegalStateException if this StopWatch has not been started
* @since 2.4
*/
public long getStartTime() {
@@ -353,6 +362,23 @@ public class StopWatch {
}
/**
+ * Gets the time this stopwatch was stopped in milliseconds, between the current time and midnight, January 1, 1970
+ * UTC.
+ *
+ * @return the time this stopwatch was started in milliseconds, between the current time and midnight, January 1,
+ * 1970 UTC.
+ * @throws IllegalStateException if this StopWatch has not been started
+ * @since 3.12
+ */
+ public long getStopTime() {
+ if (this.runningState == State.UNSTARTED) {
+ throw new IllegalStateException("Stopwatch has not been started");
+ }
+ // System.nanoTime is for elapsed time
+ return this.stopTimeMillis;
+ }
+
+ /**
* <p>
* Gets the time on the stopwatch.
* </p>
@@ -370,7 +396,7 @@ public class StopWatch {
/**
* <p>
- * Gets the time on the stopwatch in the specified TimeUnit.
+ * Gets the time in the specified TimeUnit.
* </p>
*
* <p>
@@ -439,6 +465,7 @@ public class StopWatch {
this.runningState = State.UNSTARTED;
this.splitState = SplitState.UNSPLIT;
}
+
/**
* <p>
* Resumes the stopwatch after a suspend.
@@ -456,7 +483,7 @@ public class StopWatch {
if (this.runningState != State.SUSPENDED) {
throw new IllegalStateException("Stopwatch must be suspended to resume. ");
}
- this.startTime += System.nanoTime() - this.stopTime;
+ this.startTimeNanos += System.nanoTime() - this.stopTimeNanos;
this.runningState = State.RUNNING;
}
@@ -477,7 +504,7 @@ public class StopWatch {
if (this.runningState != State.RUNNING) {
throw new IllegalStateException("Stopwatch is not running. ");
}
- this.stopTime = System.nanoTime();
+ this.stopTimeNanos = System.nanoTime();
this.splitState = SplitState.SPLIT;
}
@@ -500,7 +527,7 @@ public class StopWatch {
if (this.runningState != State.UNSTARTED) {
throw new IllegalStateException("Stopwatch already started. ");
}
- this.startTime = System.nanoTime();
+ this.startTimeNanos = System.nanoTime();
this.startTimeMillis = System.currentTimeMillis();
this.runningState = State.RUNNING;
}
@@ -522,7 +549,8 @@ public class StopWatch {
throw new IllegalStateException("Stopwatch is not running. ");
}
if (this.runningState == State.RUNNING) {
- this.stopTime = System.nanoTime();
+ this.stopTimeNanos = System.nanoTime();
+ this.stopTimeMillis = System.currentTimeMillis();
}
this.runningState = State.STOPPED;
}
@@ -544,7 +572,8 @@ public class StopWatch {
if (this.runningState != State.RUNNING) {
throw new IllegalStateException("Stopwatch must be running to suspend. ");
}
- this.stopTime = System.nanoTime();
+ this.stopTimeNanos = System.nanoTime();
+ this.stopTimeMillis = System.currentTimeMillis();
this.runningState = State.SUSPENDED;
}
diff --git a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
index 65e6dde..50448c4 100644
--- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
@@ -61,14 +61,22 @@ public class StopWatchTest {
watch.suspend();
try {
final long currentNanos = System.nanoTime();
- FieldUtils.writeField(watch, "startTime", currentNanos - nanos, true);
- FieldUtils.writeField(watch, "stopTime", currentNanos, true);
+ FieldUtils.writeField(watch, "startTimeNanos", currentNanos - nanos, true);
+ FieldUtils.writeField(watch, "stopTimeNanos", currentNanos, true);
} catch (final IllegalAccessException e) {
return null;
}
return watch;
}
+ private void sleepQuietly(final int millis) {
+ try {
+ Thread.sleep(millis);
+ } catch (final InterruptedException ex) {
+ // ignore
+ }
+ }
+
// test bad states
@Test
public void testBadStates() {
@@ -210,18 +218,10 @@ public class StopWatchTest {
@Test
public void testLang315() {
final StopWatch watch = StopWatch.createStarted();
- try {
- Thread.sleep(200);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(200);
watch.suspend();
final long suspendTime = watch.getTime();
- try {
- Thread.sleep(200);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(200);
watch.stop();
final long totalTime = watch.getTime();
assertEquals(suspendTime, totalTime);
@@ -256,11 +256,7 @@ public class StopWatchTest {
@Test
public void testStopWatchSimple() {
final StopWatch watch = StopWatch.createStarted();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(550);
watch.stop();
final long time = watch.getTime();
assertEquals(time, watch.getTime());
@@ -273,42 +269,40 @@ public class StopWatchTest {
}
@Test
+ public void testStopTimeSimple() {
+ final StopWatch watch = StopWatch.createStarted();
+ final long testStartMillis = System.currentTimeMillis();
+ sleepQuietly(550);
+ watch.stop();
+ final long testEndMillis = System.currentTimeMillis();
+ final long stopTime = watch.getStopTime();
+ assertEquals(stopTime, watch.getStopTime());
+
+ assertTrue(stopTime >= testStartMillis);
+ assertTrue(stopTime <= testEndMillis);
+ }
+
+ @Test
public void testStopWatchSimpleGet() {
final StopWatch watch = new StopWatch();
assertEquals(0, watch.getTime());
assertEquals(ZERO_TIME_ELAPSED, watch.toString());
watch.start();
- try {
- Thread.sleep(500);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(500);
assertTrue(watch.getTime() < 2000);
}
@Test
public void testStopWatchSplit() {
final StopWatch watch = StopWatch.createStarted();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(550);
watch.split();
final long splitTime = watch.getSplitTime();
final String splitStr = watch.toSplitString();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(550);
watch.unsplit();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(550);
watch.stop();
final long totalTime = watch.getTime();
@@ -328,24 +322,19 @@ public class StopWatchTest {
@Test
public void testStopWatchSuspend() {
final StopWatch watch = StopWatch.createStarted();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ final long testStartMillis = System.currentTimeMillis();
+ sleepQuietly(550);
watch.suspend();
+ final long testSuspendMillis = System.currentTimeMillis();
final long suspendTime = watch.getTime();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ final long stopTime = watch.getStopTime();
+
+ assertTrue(testStartMillis <= stopTime);
+ assertTrue(testSuspendMillis <= stopTime);
+
+ sleepQuietly(550);
watch.resume();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(550);
watch.stop();
final long totalTime = watch.getTime();
@@ -358,11 +347,7 @@ public class StopWatchTest {
@Test
public void testToSplitString() {
final StopWatch watch = StopWatch.createStarted();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(550);
watch.split();
final String splitStr = watch.toSplitString();
assertEquals(splitStr.length(), 12, "Formatted split string not the correct length");
@@ -372,11 +357,7 @@ public class StopWatchTest {
public void testToSplitStringWithMessage() {
final StopWatch watch = new StopWatch(MESSAGE);
watch.start();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(550);
watch.split();
final String splitStr = watch.toSplitString();
assertEquals(splitStr.length(), 12 + MESSAGE.length() + 1, "Formatted split string not the correct length");
@@ -386,11 +367,7 @@ public class StopWatchTest {
public void testToString() {
//
final StopWatch watch = StopWatch.createStarted();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(550);
watch.split();
final String splitStr = watch.toString();
assertEquals(splitStr.length(), 12, "Formatted split string not the correct length");
@@ -402,11 +379,7 @@ public class StopWatchTest {
//
final StopWatch watch = new StopWatch(MESSAGE);
watch.start();
- try {
- Thread.sleep(550);
- } catch (final InterruptedException ex) {
- // ignore
- }
+ sleepQuietly(550);
watch.split();
final String splitStr = watch.toString();
assertEquals(splitStr.length(), 12 + MESSAGE.length() + 1, "Formatted split string not the correct length");