LatencyStats { public ActiveInterval startNewInterval() { synchronized (monitor) { pushEndOfPeriodToNow(); ActiveIntervalInfo activeIntervalInfo = new ActiveIntervalInfo(endOfPeriodNanos); activeIntervals.add(activeIntervalInfo); maxLoad = max(maxLoad, activeIntervals.size()) ; ++numIntervalsStarted; return () -> endInterval(activeIntervalInfo); } } LatencyStats();   LatencyStats(LongSupplier nanoTimeSupplier); ActiveInterval startNewInterval(); LatencyMetrics getLatencyMetrics(); LatencyMetrics getLatencyMetricsAndStartNewPeriod();  }
@Test public void manyIntervals() { nanoTimeSupplier.addSeconds(1); ActiveInterval activeInterval1 = stats.startNewInterval(); nanoTimeSupplier.addSeconds(1); ActiveInterval activeInterval2 = stats.startNewInterval(); assertLatencyMetrics( 0.0, 0.0, 1.0, 1.0, 0.0, 0.5, 2, 2); nanoTimeSupplier.addSeconds(1); ActiveInterval activeInterval3 = stats.startNewInterval(); nanoTimeSupplier.addSeconds(1); activeInterval1.close(); activeInterval3.close(); assertLatencyMetrics( 2.0, 3.0, 3.0, 0.75, 0.5, 1.5, 3, 1); }