@Test public void testRollingHistogram() { final AtomicInteger currentTime = new AtomicInteger(0); Clock clock = mock(Clock.class); when(clock.getCurrentTime()).then(invocationOnMock -> currentTime.get()); Assert.assertEquals(clock.getCurrentTime(), 0); double[] percentiles = new double[]{0.0, 0.5, 0.75, 0.9, 0.98, 0.99, 0.999, 1.0}; RollingHistogram histogram = new RollingHistogram(clock, StatisticConfig.builder() .percentiles(percentiles) .expiry(Duration.ofSeconds(4)).buckets(4).build()); histogram.record(10); histogram.record(20); Snapshot snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 10.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 20.0, 1.0); Assert.assertEquals(snapshot.getMin(), 10.0, 1.0); Assert.assertEquals(snapshot.getMax(), 20.0, 1.0); currentTime.addAndGet(900); Assert.assertEquals(clock.getCurrentTime(), 900); histogram.record(30); histogram.record(40); snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 10.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 40.0, 1.0); Assert.assertEquals(snapshot.getMin(), 10.0, 1.0); Assert.assertEquals(snapshot.getMax(), 40.0, 1.0); currentTime.addAndGet(99); Assert.assertEquals(clock.getCurrentTime(), 999); histogram.record(9); histogram.record(60); snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 9.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 60.0, 1.0); Assert.assertEquals(snapshot.getMin(), 9.0, 1.0); Assert.assertEquals(snapshot.getMax(), 60.0, 1.0); currentTime.addAndGet(1); Assert.assertEquals(clock.getCurrentTime(), 1000); histogram.record(12); histogram.record(70); snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 9.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 70.0, 1.0); Assert.assertEquals(snapshot.getMin(), 9.0, 1.0); Assert.assertEquals(snapshot.getMax(), 70.0, 1.0); currentTime.addAndGet(1001); Assert.assertEquals(clock.getCurrentTime(), 2001); histogram.record(13); histogram.record(80); snapshot = histogram.getSnapshot(); Assert.assertEquals(snapshot.getMin(), 9.0, 1.0); Assert.assertEquals(snapshot.getMax(), 80.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 0.0), 9.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 80.0, 1.0); currentTime.addAndGet(1000); Assert.assertEquals(clock.getCurrentTime(), 3001); snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 9.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 80.0, 1.0); Assert.assertEquals(snapshot.getMin(), 9.0, 1.0); Assert.assertEquals(snapshot.getMax(), 80.0, 1.0); currentTime.addAndGet(999); Assert.assertEquals(clock.getCurrentTime(), 4000); snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 12.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 80.0, 1.0); Assert.assertEquals(snapshot.getMin(), 12.0, 1.0); Assert.assertEquals(snapshot.getMax(), 80.0, 1.0); histogram.record(1); histogram.record(200); snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 1.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 200.0, 1.0); Assert.assertEquals(snapshot.getMin(), 1.0, 1.0); Assert.assertEquals(snapshot.getMax(), 200.0, 1.0); currentTime.addAndGet(10000); Assert.assertEquals(clock.getCurrentTime(), 14000); snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 0.0, 0.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 0.0, 0.0); Assert.assertEquals(snapshot.getMin(), 0.0, 1.0); Assert.assertEquals(snapshot.getMax(), 0.0, 1.0); histogram.record(3); currentTime.addAndGet(3999); Assert.assertEquals(clock.getCurrentTime(), 17999); snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 3.0, 1.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 3.0, 1.0); Assert.assertEquals(snapshot.getMin(), 3.0, 1.0); Assert.assertEquals(snapshot.getMax(), 3.0, 1.0); currentTime.addAndGet(1); Assert.assertEquals(clock.getCurrentTime(), 18000); snapshot = histogram.getSnapshot(); Assert.assertEquals(percentileValue(snapshot, 0.0), 0.0, 0.0); Assert.assertEquals(percentileValue(snapshot, 1.0), 0.0, 0.0); Assert.assertEquals(snapshot.getMin(), 0.0, 1.0); Assert.assertEquals(snapshot.getMax(), 0.0, 1.0); }
public RollingHistogram(Clock clock, StatisticConfig statisticConfig) { this.clock = clock; this.statisticConfig = statisticConfig; int ageBuckets = (int) statisticConfig.getBuckets(); ringBuffer = new DoubleRecorder[ageBuckets]; for (int i = 0; i < ageBuckets; i++) { ringBuffer[i] = new DoubleRecorder(statisticConfig.getPercentilePrecision()); } this.currentBucket = 0; this.lastRotateTimestampMillis = clock.getCurrentTime(); this.durationBetweenRotatesMillis = statisticConfig.getTimeWindow().toMillis() / ageBuckets; intervalHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); accumulatedHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); }
RollingHistogram { public RollingHistogram(Clock clock, StatisticConfig statisticConfig) { this.clock = clock; this.statisticConfig = statisticConfig; int ageBuckets = (int) statisticConfig.getBuckets(); ringBuffer = new DoubleRecorder[ageBuckets]; for (int i = 0; i < ageBuckets; i++) { ringBuffer[i] = new DoubleRecorder(statisticConfig.getPercentilePrecision()); } this.currentBucket = 0; this.lastRotateTimestampMillis = clock.getCurrentTime(); this.durationBetweenRotatesMillis = statisticConfig.getTimeWindow().toMillis() / ageBuckets; intervalHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); accumulatedHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); } }
RollingHistogram { public RollingHistogram(Clock clock, StatisticConfig statisticConfig) { this.clock = clock; this.statisticConfig = statisticConfig; int ageBuckets = (int) statisticConfig.getBuckets(); ringBuffer = new DoubleRecorder[ageBuckets]; for (int i = 0; i < ageBuckets; i++) { ringBuffer[i] = new DoubleRecorder(statisticConfig.getPercentilePrecision()); } this.currentBucket = 0; this.lastRotateTimestampMillis = clock.getCurrentTime(); this.durationBetweenRotatesMillis = statisticConfig.getTimeWindow().toMillis() / ageBuckets; intervalHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); accumulatedHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); } RollingHistogram(Clock clock, StatisticConfig statisticConfig); }
RollingHistogram { public RollingHistogram(Clock clock, StatisticConfig statisticConfig) { this.clock = clock; this.statisticConfig = statisticConfig; int ageBuckets = (int) statisticConfig.getBuckets(); ringBuffer = new DoubleRecorder[ageBuckets]; for (int i = 0; i < ageBuckets; i++) { ringBuffer[i] = new DoubleRecorder(statisticConfig.getPercentilePrecision()); } this.currentBucket = 0; this.lastRotateTimestampMillis = clock.getCurrentTime(); this.durationBetweenRotatesMillis = statisticConfig.getTimeWindow().toMillis() / ageBuckets; intervalHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); accumulatedHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); } RollingHistogram(Clock clock, StatisticConfig statisticConfig); void record(double value); StatisticConfig getStatisticConfig(); Snapshot getSnapshot(); }
RollingHistogram { public RollingHistogram(Clock clock, StatisticConfig statisticConfig) { this.clock = clock; this.statisticConfig = statisticConfig; int ageBuckets = (int) statisticConfig.getBuckets(); ringBuffer = new DoubleRecorder[ageBuckets]; for (int i = 0; i < ageBuckets; i++) { ringBuffer[i] = new DoubleRecorder(statisticConfig.getPercentilePrecision()); } this.currentBucket = 0; this.lastRotateTimestampMillis = clock.getCurrentTime(); this.durationBetweenRotatesMillis = statisticConfig.getTimeWindow().toMillis() / ageBuckets; intervalHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); accumulatedHistogram = new DoubleHistogram(statisticConfig.getPercentilePrecision()); } RollingHistogram(Clock clock, StatisticConfig statisticConfig); void record(double value); StatisticConfig getStatisticConfig(); Snapshot getSnapshot();  }