aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorHåkon Hallingstad <hakon@oath.com>2018-06-22 09:53:11 +0200
committerHåkon Hallingstad <hakon@oath.com>2018-06-22 09:53:11 +0200
commit02f23addf2fe400e3eeadcee30b1ca9aba12953c (patch)
tree3d1e43d21b616bb647c7c7ba1f4d0f0cfd4a1f6b /vespajlib
parentde6c275b321f3dc78a761c8dd045a05c198ab28f (diff)
Remove unused WallClockSource from package to export
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/time/WallClockSource.java118
-rw-r--r--vespajlib/src/test/java/com/yahoo/time/TimeBudgetTest.java (renamed from vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java)2
-rw-r--r--vespajlib/src/test/java/com/yahoo/time/WallClockSourceTestCase.java86
3 files changed, 1 insertions, 205 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/time/WallClockSource.java b/vespajlib/src/main/java/com/yahoo/time/WallClockSource.java
deleted file mode 100644
index 8a77879adf5..00000000000
--- a/vespajlib/src/main/java/com/yahoo/time/WallClockSource.java
+++ /dev/null
@@ -1,118 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.time;
-
-import com.google.common.annotations.Beta;
-
-/**
- * A source for high-resolution timestamps.
- *
- * @author arnej27959
- */
-
-@Beta
-public class WallClockSource {
-
- private volatile long offset;
-
- /**
- * Obtain the current time in nanoseconds.
- * The epoch is January 1, 1970 UTC just as for System.currentTimeMillis(),
- * but with greater resolution. Note that the absolute accuracy may be
- * no better than currentTimeMills().
- * @return nanoseconds since the epoch.
- **/
- public final long currentTimeNanos() {
- return System.nanoTime() + offset;
- }
-
- /**
- * Create a source with 1 millisecond accuracy at start.
- **/
- WallClockSource() {
- long actual = System.currentTimeMillis();
- actual *= 1000000;
- this.offset = actual - System.nanoTime();
- initialAdjust();
- }
-
- /** adjust the clock source from currentTimeMillis()
- * to ensure that it is no more than 1 milliseconds off.
- * @return true if we want adjust called again soon
- **/
- boolean adjust() {
- long nanosB = System.nanoTime();
- long actual = System.currentTimeMillis();
- long nanosA = System.nanoTime();
- if (nanosA - nanosB > 100000) {
- return true; // not a good time to adjust, try again soon
- }
- return adjustOffset(nanosB, actual, nanosA);
- }
-
- private boolean adjustOffset(long before, long actual, long after) {
- actual *= 1000000; // convert millis to nanos
- if (actual > after + offset) {
- // System.out.println("WallClockSource adjust UP "+(actual-after-offset));
- offset = actual - after;
- return true;
- }
- if (actual + 999999 < before + offset) {
- // System.out.println("WallClockSource adjust DOWN "+(before+offset-actual-999999));
- offset = actual + 999999 - before;
- return true;
- }
- return false;
- }
-
- private void initialAdjust() {
- for (int i = 0; i < 100; i++) {
- long nanosB = System.nanoTime();
- long actual = System.currentTimeMillis();
- long nanosA = System.nanoTime();
- adjustOffset(nanosB, actual, nanosA);
- }
- }
-
-
- static private WallClockSource autoAdjustingInstance = new WallClockSource();
-
- /**
- * Get a WallClockSource which auto adjusts to wall clock time.
- **/
- static public WallClockSource get() {
- autoAdjustingInstance.startAdjuster();
- return autoAdjustingInstance;
- }
-
- private Thread adjuster;
-
- private synchronized void startAdjuster() {
- if (adjuster == null) {
- adjuster = new AdjustThread();
- adjuster.setDaemon(true);
- adjuster.start();
- }
- }
-
- private class AdjustThread extends Thread {
- public void run() {
- int millis = 0;
- int nanos = 313373; // random number
- while (true) {
- try {
- sleep(millis, nanos);
- if (++millis > 4321) {
- millis = 1000; // do not sleep too long
- }
- } catch (InterruptedException e) {
- return;
- }
- if (adjust()) {
- // adjust more often in case clock jumped
- millis = 0;
- }
- }
- }
- }
-
-}
diff --git a/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java b/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTest.java
index ddd57c71a0d..a51081067b7 100644
--- a/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/time/TimeBudgetTest.java
@@ -13,7 +13,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
-public class TimeBudgetTestCase {
+public class TimeBudgetTest {
private final Clock clock = mock(Clock.class);
@Test
diff --git a/vespajlib/src/test/java/com/yahoo/time/WallClockSourceTestCase.java b/vespajlib/src/test/java/com/yahoo/time/WallClockSourceTestCase.java
deleted file mode 100644
index 4c181235a38..00000000000
--- a/vespajlib/src/test/java/com/yahoo/time/WallClockSourceTestCase.java
+++ /dev/null
@@ -1,86 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.time;
-
-import org.junit.Test;
-import static org.junit.Assert.assertTrue;
-
-public class WallClockSourceTestCase {
-
- @Test
- public void testSimple() {
- long actualBefore = System.currentTimeMillis();
- WallClockSource clock = new WallClockSource();
- long nanos = clock.currentTimeNanos();
- long micros = nanos / 1000;
- long millis = micros / 1000;
- long actualAfter = System.currentTimeMillis();
-
- assertTrue(actualBefore <= millis);
- assertTrue(millis <= actualAfter);
- }
-
- @Test
- public void testWithAdjust() {
- WallClockSource clock = new WallClockSource();
- long diffB = 0;
- long diffA = 0;
- for (int i = 0; i < 66666; i++) {
- long actualB = System.currentTimeMillis();
- clock.adjust();
- long nanos = clock.currentTimeNanos();
- long actualA = System.currentTimeMillis();
- long micros = nanos / 1000;
- long millis = micros / 1000;
- diffB = Math.max(diffB, actualB - millis);
- diffA = Math.max(diffA, millis - actualA);
- // System.out.println("adj Timing values, before: "+actualB+" <= guess: "+millis+" <= after: "+actualA);
- }
- System.out.println("adjust test: biggest difference (beforeTime - guess): "+diffB);
- System.out.println("adjust test: biggest difference (guess - afterTime): "+diffA);
- assertTrue("actual time before sample must be <= wallclocksource, diff: " + diffB, diffB < 2);
- assertTrue("actual time after sample must be >= wallclocksource, diff: " + diffA, diffA < 2);
- }
-
- @Test
- public void testNoAdjust() {
- WallClockSource clock = new WallClockSource();
- long diffB = 0;
- long diffA = 0;
- for (int i = 0; i < 66666; i++) {
- long actualB = System.currentTimeMillis();
- long nanos = clock.currentTimeNanos();
- long actualA = System.currentTimeMillis();
- long micros = nanos / 1000;
- long millis = micros / 1000;
- diffB = Math.max(diffB, actualB - millis);
- diffA = Math.max(diffA, millis - actualA);
- // System.out.println("noadj Timing values, before: "+actualB+" <= guess: "+millis+" <= after: "+actualA);
- }
- System.out.println("noadjust test: biggest difference (beforeTime - guess): "+diffB);
- System.out.println("noadjust test: biggest difference (guess - afterTime): "+diffA);
- assertTrue("actual time before sample must be <= wallclocksource, diff: " + diffB, diffB < 3);
- assertTrue("actual time after sample must be >= wallclocksource, diff: " + diffA, diffA < 3);
- }
-
- @Test
- public void testAutoAdjust() {
- WallClockSource clock = WallClockSource.get();
- long diffB = 0;
- long diffA = 0;
- for (int i = 0; i < 66666; i++) {
- long actualB = System.currentTimeMillis();
- long nanos = clock.currentTimeNanos();
- long actualA = System.currentTimeMillis();
- long micros = nanos / 1000;
- long millis = micros / 1000;
- diffB = Math.max(diffB, actualB - millis);
- diffA = Math.max(diffA, millis - actualA);
- // System.out.println("noadj Timing values, before: "+actualB+" <= guess: "+millis+" <= after: "+actualA);
- }
- System.out.println("autoadjust test: biggest difference (beforeTime - guess): "+diffB);
- System.out.println("autoadjust test: biggest difference (guess - afterTime): "+diffA);
- assertTrue("actual time before sample must be <= wallclocksource, diff: " + diffB, diffB < 3);
- assertTrue("actual time after sample must be >= wallclocksource, diff: " + diffA, diffA < 3);
- }
-
-}