Skip to content

Commit f306009

Browse files
author
edelgadoh
committed
Adding labels to split StopWatch feature - replacing startNanoTime
1 parent a1f68eb commit f306009

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/main/java/org/apache/commons/lang3/time/StopWatch.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.commons.lang3.time;
1919

20-
import java.time.Clock;
2120
import java.time.Duration;
2221
import java.time.Instant;
2322
import java.util.ArrayList;
@@ -865,8 +864,11 @@ private void processSplits(TimeUnit timeUnit) {
865864
}
866865

867866
for (int i = 0; i < splits.size() - 1; i++) {
868-
final Duration duration = Duration.between(splits.get(i).getStartTime(), splits.get(i + 1).getStartTime());
869-
splits.get(i).setDuration(timeUnit == TimeUnit.NANOSECONDS ? duration.toNanos() : duration.toMillis());
867+
final long durationNanos = splits.get(i + 1).getStartNanoTime() - splits.get(i).getStartNanoTime();
868+
final long duration = (timeUnit == TimeUnit.NANOSECONDS)
869+
? durationNanos
870+
: TimeUnit.MILLISECONDS.convert(durationNanos, TimeUnit.NANOSECONDS);
871+
splits.get(i).setDuration(duration);
870872
}
871873

872874
}
@@ -931,9 +933,9 @@ private String getReport(TimeUnit timeUnit) {
931933
protected static class Split {
932934

933935
/**
934-
* The start time of this split
936+
* The start nano time of this split
935937
*/
936-
private Instant startTime = Clock.systemUTC().instant();
938+
private long startNanoTime = System.nanoTime();
937939

938940
/**
939941
* The duration (time took) on this split
@@ -959,10 +961,10 @@ public Split(String label) {
959961
* Get the timestamp when this split was created
960962
* </p>
961963
*
962-
* @return startTime
964+
* @return startNanoTime
963965
*/
964-
public Instant getStartTime() {
965-
return startTime;
966+
public long getStartNanoTime() {
967+
return startNanoTime;
966968
}
967969

968970
/**

src/test/java/org/apache/commons/lang3/time/StopWatchTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ void testSplitsWithStringLabels() {
531531
assertEquals(splits.get(2).getLabel(), thirdLabel);
532532

533533
// check duration
534-
final int margin = 200;
534+
final int margin = 50;
535535
assertEquals(firstSleepTime, splits.get(0).getDuration(), margin);
536536
assertEquals(secondSleepTime, splits.get(1).getDuration(), margin);
537537
assertEquals(thirdSleepTime, splits.get(2).getDuration(), margin);
@@ -578,7 +578,7 @@ void testSplitsWithIntLabels() {
578578
assertEquals(splits.get(2).getLabel(), String.valueOf(thirdLabel));
579579

580580
// check duration
581-
final int margin = 200;
581+
final int margin = 50;
582582
assertEquals(firstSleepTime, splits.get(0).getDuration(), margin);
583583
assertEquals(secondSleepTime, splits.get(1).getDuration(), margin);
584584
assertEquals(thirdSleepTime, splits.get(2).getDuration(), margin);
@@ -612,7 +612,7 @@ void testNanoSplitsWithLabel() {
612612
assertEquals(2, splits.size());
613613

614614
// check duration
615-
final int margin = 200_000_000;
615+
final int margin = 50_000_000;
616616
assertEquals(200_000_000, splits.get(0).getDuration(), margin);
617617
assertEquals(300_000_000, splits.get(1).getDuration(), margin);
618618

0 commit comments

Comments
 (0)