Skip to content

Commit

Permalink
Use monotonic time for calculating the elapsed time
Browse files Browse the repository at this point in the history
Resolves #110
  • Loading branch information
ttddyy committed Dec 13, 2023
1 parent 388a61b commit 4101c49
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 11 additions & 0 deletions src/main/asciidoc/changelog-1.11.x.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[changelog-1.11]]
=== 1.11

==== New Features

==== Improvements

* Use monotonic time to measure the elapsed time (https://github.com/jdbc-observations/datasource-proxy/issues/110[Issue-110]).


==== Bug Fixes
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package net.ttddyy.dsproxy.proxy;

import java.util.concurrent.TimeUnit;

/**
* Factory to create {@link SystemStopwatch} which uses {@code System.currentTimeMillis()}.
*
* The unit of time is milliseconds.
* Factory to create {@link SystemStopwatch} which uses monotonic time.
* <p> The unit of time is milliseconds.
*
* @author Tadaya Tsuyukubo
* @since 1.5.1
Expand All @@ -16,17 +17,16 @@ public Stopwatch create() {
}

/**
* Uses {@code System.currentTimeMillis()} to calculate elapsed time.
*
* The unit of time is milliseconds
* Uses monotonic time to calculate elapsed time.
* <p>The unit of time is milliseconds.
*/
public static class SystemStopwatch implements Stopwatch {

private long startTime;

@Override
public Stopwatch start() {
this.startTime = System.currentTimeMillis();
this.startTime = System.nanoTime();
return this;
}

Expand All @@ -37,7 +37,7 @@ public Stopwatch start() {
*/
@Override
public long getElapsedTime() {
return System.currentTimeMillis() - this.startTime;
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - this.startTime);
}

}
Expand Down

0 comments on commit 4101c49

Please sign in to comment.