From 4101c493cd1392ed65b6aa3225d075984b9039d4 Mon Sep 17 00:00:00 2001 From: Tadaya Tsuyukubo Date: Tue, 12 Dec 2023 19:40:39 -0800 Subject: [PATCH] Use monotonic time for calculating the elapsed time Resolves #110 --- src/main/asciidoc/changelog-1.11.x.adoc | 11 +++++++++++ .../dsproxy/proxy/SystemStopwatchFactory.java | 16 ++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 src/main/asciidoc/changelog-1.11.x.adoc diff --git a/src/main/asciidoc/changelog-1.11.x.adoc b/src/main/asciidoc/changelog-1.11.x.adoc new file mode 100644 index 000000000..58c9d71b5 --- /dev/null +++ b/src/main/asciidoc/changelog-1.11.x.adoc @@ -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 diff --git a/src/main/java/net/ttddyy/dsproxy/proxy/SystemStopwatchFactory.java b/src/main/java/net/ttddyy/dsproxy/proxy/SystemStopwatchFactory.java index 421c95da5..0ece21f5c 100644 --- a/src/main/java/net/ttddyy/dsproxy/proxy/SystemStopwatchFactory.java +++ b/src/main/java/net/ttddyy/dsproxy/proxy/SystemStopwatchFactory.java @@ -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. + *

The unit of time is milliseconds. * * @author Tadaya Tsuyukubo * @since 1.5.1 @@ -16,9 +17,8 @@ public Stopwatch create() { } /** - * Uses {@code System.currentTimeMillis()} to calculate elapsed time. - * - * The unit of time is milliseconds + * Uses monotonic time to calculate elapsed time. + *

The unit of time is milliseconds. */ public static class SystemStopwatch implements Stopwatch { @@ -26,7 +26,7 @@ public static class SystemStopwatch implements Stopwatch { @Override public Stopwatch start() { - this.startTime = System.currentTimeMillis(); + this.startTime = System.nanoTime(); return this; } @@ -37,7 +37,7 @@ public Stopwatch start() { */ @Override public long getElapsedTime() { - return System.currentTimeMillis() - this.startTime; + return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - this.startTime); } }