public ActiveInterval startNewInterval() {
        synchronized (monitor) {
            pushEndOfPeriodToNow();
            ActiveIntervalInfo activeIntervalInfo = new ActiveIntervalInfo(endOfPeriodNanos);
            activeIntervals.add(activeIntervalInfo);
            maxLoad = max(maxLoad, activeIntervals.size()) ;
            ++numIntervalsStarted;
            return () -> endInterval(activeIntervalInfo);
        }
    }
@Test
    public void manyIntervals() {
        nanoTimeSupplier.addSeconds(1);
        ActiveInterval activeInterval1 = stats.startNewInterval();
        nanoTimeSupplier.addSeconds(1);
        ActiveInterval activeInterval2 = stats.startNewInterval();

        assertLatencyMetrics(
                0.0,  // latency: No intervals have ended
                0.0,  // maxLatency: No intervals have ended
                1.0,  // maxActiveLatency:  First interval has lasted 1s
                1.0,  // startHz:  There have been 2 starts in 2 seconds
                0.0,  // endHz:  There have been 0 endings of intervals
                0.5,  // load:  First second had 0 active intervals, second second had 1.
                2,  // maxLoad:  There are now 2 active intervals
                2);  // currentLoad:  There are now 2 active intervals

        nanoTimeSupplier.addSeconds(1);
        ActiveInterval activeInterval3 = stats.startNewInterval();

        nanoTimeSupplier.addSeconds(1);
        activeInterval1.close();
        activeInterval3.close();

        assertLatencyMetrics(
                2.0,  // latency: 2 intervals ended: 3s and 1s
                3.0,
                3.0,  // maxActiveLatency:  both interval 1 and 2 have 3s latency
                0.75,  // startHz: 3 started in 4s
                0.5,
                1.5,  // load: 1s of each of 0, 1, 2, and 3 active intervals.
                3,
                1);
    }