Skip to content

Commit 4101c49

Browse files
committed
Use monotonic time for calculating the elapsed time
Resolves #110
1 parent 388a61b commit 4101c49

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[changelog-1.11]]
2+
=== 1.11
3+
4+
==== New Features
5+
6+
==== Improvements
7+
8+
* Use monotonic time to measure the elapsed time (https://github.com/jdbc-observations/datasource-proxy/issues/110[Issue-110]).
9+
10+
11+
==== Bug Fixes

src/main/java/net/ttddyy/dsproxy/proxy/SystemStopwatchFactory.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package net.ttddyy.dsproxy.proxy;
22

3+
import java.util.concurrent.TimeUnit;
4+
35
/**
4-
* Factory to create {@link SystemStopwatch} which uses {@code System.currentTimeMillis()}.
5-
*
6-
* The unit of time is milliseconds.
6+
* Factory to create {@link SystemStopwatch} which uses monotonic time.
7+
* <p> The unit of time is milliseconds.
78
*
89
* @author Tadaya Tsuyukubo
910
* @since 1.5.1
@@ -16,17 +17,16 @@ public Stopwatch create() {
1617
}
1718

1819
/**
19-
* Uses {@code System.currentTimeMillis()} to calculate elapsed time.
20-
*
21-
* The unit of time is milliseconds
20+
* Uses monotonic time to calculate elapsed time.
21+
* <p>The unit of time is milliseconds.
2222
*/
2323
public static class SystemStopwatch implements Stopwatch {
2424

2525
private long startTime;
2626

2727
@Override
2828
public Stopwatch start() {
29-
this.startTime = System.currentTimeMillis();
29+
this.startTime = System.nanoTime();
3030
return this;
3131
}
3232

@@ -37,7 +37,7 @@ public Stopwatch start() {
3737
*/
3838
@Override
3939
public long getElapsedTime() {
40-
return System.currentTimeMillis() - this.startTime;
40+
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - this.startTime);
4141
}
4242

4343
}

0 commit comments

Comments
 (0)