From e31aa760ddcc2b23d0e92a0091038b0c47f2827d Mon Sep 17 00:00:00 2001 From: James Agnew Date: Mon, 5 Mar 2018 19:06:05 -0500 Subject: [PATCH] Fix stopwatch ETA calculations --- .../main/java/ca/uhn/fhir/util/StopWatch.java | 9 +-- .../java/ca/uhn/fhir/util/StopWatchTest.java | 60 ++++++++++++++++--- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/StopWatch.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/StopWatch.java index 4b412dfeeea..8103c31c81f 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/StopWatch.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/StopWatch.java @@ -67,15 +67,8 @@ public class StopWatch { * @return A formatted amount of time */ public String getEstimatedTimeRemaining(double theCompleteToDate, double theTotal) { - double completeSoFar = theCompleteToDate / theTotal; - double remaining; - if (completeSoFar > 0.5) { - remaining = (1.0 - completeSoFar); - } else { - remaining = (1.0 / completeSoFar); - } double millis = getMillis(); - long millisRemaining = (long) (millis * remaining); + long millisRemaining = (long) (((theTotal / theCompleteToDate) * millis) - (millis)); return formatMillis(millisRemaining); } diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/util/StopWatchTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/util/StopWatchTest.java index 60d6f691f46..19a234706f5 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/util/StopWatchTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/util/StopWatchTest.java @@ -28,27 +28,73 @@ public class StopWatchTest { } @Test - public void testEstimatedTimeRemaining() { + public void testEstimatedTimeRemainingOutOfOne() { StopWatch.setNowForUnitTestForUnitTest(777777777L); StopWatch sw = new StopWatch(); + // Less than half StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE); - assertEquals("600ms", sw.getEstimatedTimeRemaining(0.99, 1.0)); + assertEquals("00:09:00", sw.getEstimatedTimeRemaining(0.1, 1.0)); StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE); - assertEquals("00:10:00", sw.getEstimatedTimeRemaining(0.1, 1.0)); + assertEquals("00:09:00", sw.getEstimatedTimeRemaining(1, 10)); StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE + 100); - assertEquals("00:10:01", sw.getEstimatedTimeRemaining(0.1, 1.0)); + assertEquals("00:09:00", sw.getEstimatedTimeRemaining(0.1, 1.0)); StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE); - assertEquals("00:20:00", sw.getEstimatedTimeRemaining(0.05, 1.0)); + assertEquals("00:19:00", sw.getEstimatedTimeRemaining(0.05, 1.0)); StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE); - assertEquals("00:40:00", sw.getEstimatedTimeRemaining(0.025, 1.0)); + assertEquals("00:39:00", sw.getEstimatedTimeRemaining(0.025, 1.0)); + + // More than half + StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE); + assertEquals("00:01:00.000", sw.getEstimatedTimeRemaining(0.5, 1.0)); StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE); - assertEquals("01:40:00", sw.getEstimatedTimeRemaining(0.01, 1.0)); + assertEquals("00:00:59.760", sw.getEstimatedTimeRemaining(0.501, 1.0)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE); + assertEquals("00:00:40.000", sw.getEstimatedTimeRemaining(0.6, 1.0)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE); + assertEquals("6666ms", sw.getEstimatedTimeRemaining(0.9, 1.0)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + DateUtils.MILLIS_PER_MINUTE); + assertEquals("60ms", sw.getEstimatedTimeRemaining(0.999, 1.0)); + + } + + @Test + public void testEstimatedTimeRemainingOutOfOneHundred() { + StopWatch.setNowForUnitTestForUnitTest(777777777L); + StopWatch sw = new StopWatch(); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + (10 * DateUtils.MILLIS_PER_MINUTE)); + assertEquals("01:30:00", sw.getEstimatedTimeRemaining(10, 100)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + (DateUtils.MILLIS_PER_MINUTE)); + assertEquals("00:04:00", sw.getEstimatedTimeRemaining(20, 100)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + (30 * DateUtils.MILLIS_PER_MINUTE)); + assertEquals("01:10:00", sw.getEstimatedTimeRemaining(30, 100)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + (40 * DateUtils.MILLIS_PER_MINUTE)); + assertEquals("01:00:00", sw.getEstimatedTimeRemaining(40, 100)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + (50 * DateUtils.MILLIS_PER_MINUTE)); + assertEquals("00:50:00", sw.getEstimatedTimeRemaining(50, 100)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + (60 * DateUtils.MILLIS_PER_MINUTE)); + assertEquals("00:40:00", sw.getEstimatedTimeRemaining(60, 100)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + (60 * DateUtils.MILLIS_PER_MINUTE)); + assertEquals("00:00:36.363", sw.getEstimatedTimeRemaining(99, 100)); + + StopWatch.setNowForUnitTestForUnitTest(777777777L + (60 * DateUtils.MILLIS_PER_MINUTE)); + assertEquals("360ms", sw.getEstimatedTimeRemaining(99.99, 100)); + } @Test