@Test public void shouldResampleSaneEpochTimestamp() { final OffsetEpochNanoClock clock = new OffsetEpochNanoClock(); clock.sample(); assertSaneEpochTimeStamp(clock); }
public void sample() { long bestInitialCurrentNanoTime = 0, bestInitialNanoTime = 0; long bestNanoTimeWindow = Long.MAX_VALUE; for (int i = 0; i < maxMeasurementRetries; i++) { final long firstNanoTime = System.nanoTime(); final long initialCurrentTimeMillis = System.currentTimeMillis(); final long secondNanoTime = System.nanoTime(); final long nanoTimeWindow = secondNanoTime - firstNanoTime; if (nanoTimeWindow < measurementThresholdNs) { initialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); initialNanoTime = (firstNanoTime + secondNanoTime) >> 1; isWithinThreshold = true; return; } else if (nanoTimeWindow < bestNanoTimeWindow) { bestInitialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); bestInitialNanoTime = (firstNanoTime + secondNanoTime) >> 1; bestNanoTimeWindow = nanoTimeWindow; } } initialCurrentNanoTime = bestInitialCurrentNanoTime; initialNanoTime = bestInitialNanoTime; isWithinThreshold = false; }
OffsetEpochNanoClock implements EpochNanoClock { public void sample() { long bestInitialCurrentNanoTime = 0, bestInitialNanoTime = 0; long bestNanoTimeWindow = Long.MAX_VALUE; for (int i = 0; i < maxMeasurementRetries; i++) { final long firstNanoTime = System.nanoTime(); final long initialCurrentTimeMillis = System.currentTimeMillis(); final long secondNanoTime = System.nanoTime(); final long nanoTimeWindow = secondNanoTime - firstNanoTime; if (nanoTimeWindow < measurementThresholdNs) { initialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); initialNanoTime = (firstNanoTime + secondNanoTime) >> 1; isWithinThreshold = true; return; } else if (nanoTimeWindow < bestNanoTimeWindow) { bestInitialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); bestInitialNanoTime = (firstNanoTime + secondNanoTime) >> 1; bestNanoTimeWindow = nanoTimeWindow; } } initialCurrentNanoTime = bestInitialCurrentNanoTime; initialNanoTime = bestInitialNanoTime; isWithinThreshold = false; } }
OffsetEpochNanoClock implements EpochNanoClock { public void sample() { long bestInitialCurrentNanoTime = 0, bestInitialNanoTime = 0; long bestNanoTimeWindow = Long.MAX_VALUE; for (int i = 0; i < maxMeasurementRetries; i++) { final long firstNanoTime = System.nanoTime(); final long initialCurrentTimeMillis = System.currentTimeMillis(); final long secondNanoTime = System.nanoTime(); final long nanoTimeWindow = secondNanoTime - firstNanoTime; if (nanoTimeWindow < measurementThresholdNs) { initialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); initialNanoTime = (firstNanoTime + secondNanoTime) >> 1; isWithinThreshold = true; return; } else if (nanoTimeWindow < bestNanoTimeWindow) { bestInitialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); bestInitialNanoTime = (firstNanoTime + secondNanoTime) >> 1; bestNanoTimeWindow = nanoTimeWindow; } } initialCurrentNanoTime = bestInitialCurrentNanoTime; initialNanoTime = bestInitialNanoTime; isWithinThreshold = false; } OffsetEpochNanoClock();  OffsetEpochNanoClock(
        final int maxMeasurementRetries, final long measurementThresholdNs, final long resampleIntervalNs); }
OffsetEpochNanoClock implements EpochNanoClock { public void sample() { long bestInitialCurrentNanoTime = 0, bestInitialNanoTime = 0; long bestNanoTimeWindow = Long.MAX_VALUE; for (int i = 0; i < maxMeasurementRetries; i++) { final long firstNanoTime = System.nanoTime(); final long initialCurrentTimeMillis = System.currentTimeMillis(); final long secondNanoTime = System.nanoTime(); final long nanoTimeWindow = secondNanoTime - firstNanoTime; if (nanoTimeWindow < measurementThresholdNs) { initialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); initialNanoTime = (firstNanoTime + secondNanoTime) >> 1; isWithinThreshold = true; return; } else if (nanoTimeWindow < bestNanoTimeWindow) { bestInitialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); bestInitialNanoTime = (firstNanoTime + secondNanoTime) >> 1; bestNanoTimeWindow = nanoTimeWindow; } } initialCurrentNanoTime = bestInitialCurrentNanoTime; initialNanoTime = bestInitialNanoTime; isWithinThreshold = false; } OffsetEpochNanoClock();  OffsetEpochNanoClock(
        final int maxMeasurementRetries, final long measurementThresholdNs, final long resampleIntervalNs); void sample(); long nanoTime(); boolean isWithinThreshold(); }
OffsetEpochNanoClock implements EpochNanoClock { public void sample() { long bestInitialCurrentNanoTime = 0, bestInitialNanoTime = 0; long bestNanoTimeWindow = Long.MAX_VALUE; for (int i = 0; i < maxMeasurementRetries; i++) { final long firstNanoTime = System.nanoTime(); final long initialCurrentTimeMillis = System.currentTimeMillis(); final long secondNanoTime = System.nanoTime(); final long nanoTimeWindow = secondNanoTime - firstNanoTime; if (nanoTimeWindow < measurementThresholdNs) { initialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); initialNanoTime = (firstNanoTime + secondNanoTime) >> 1; isWithinThreshold = true; return; } else if (nanoTimeWindow < bestNanoTimeWindow) { bestInitialCurrentNanoTime = MILLISECONDS.toNanos(initialCurrentTimeMillis); bestInitialNanoTime = (firstNanoTime + secondNanoTime) >> 1; bestNanoTimeWindow = nanoTimeWindow; } } initialCurrentNanoTime = bestInitialCurrentNanoTime; initialNanoTime = bestInitialNanoTime; isWithinThreshold = false; } OffsetEpochNanoClock();  OffsetEpochNanoClock(
        final int maxMeasurementRetries, final long measurementThresholdNs, final long resampleIntervalNs); void sample(); long nanoTime(); boolean isWithinThreshold();  }