use of io.micrometer.core.instrument.MockClock in project micrometer by micrometer-metrics.
the class TimeWindowPercentileHistogramTest method percentilesChangeWithMoreRecentSamples.
@Test
void percentilesChangeWithMoreRecentSamples() {
DistributionStatisticConfig config = DistributionStatisticConfig.builder().percentiles(0.5).build().merge(DistributionStatisticConfig.DEFAULT);
TimeWindowPercentileHistogram histogram = new TimeWindowPercentileHistogram(new MockClock(), config, false);
for (int i = 1; i <= 10; i++) {
histogram.recordLong((long) millisToUnit(i, TimeUnit.NANOSECONDS));
}
// baseline median
assertThat(histogram.takeSnapshot(0, 0, 0).percentileValues()).anyMatch(p -> percentileValueIsApproximately(p, 0.5, 5e6));
for (int i = 11; i <= 20; i++) {
histogram.recordLong((long) millisToUnit(i, TimeUnit.NANOSECONDS));
}
// median should have moved after seeing 10 more samples
assertThat(histogram.takeSnapshot(0, 0, 0).percentileValues()).anyMatch(p -> percentileValueIsApproximately(p, 0.5, 10e6));
}
use of io.micrometer.core.instrument.MockClock in project micrometer by micrometer-metrics.
the class TimeWindowPercentileHistogramTest method recordValuesThatExceedTheDynamicRange.
@Test
void recordValuesThatExceedTheDynamicRange() {
TimeWindowPercentileHistogram histogram = new TimeWindowPercentileHistogram(new MockClock(), DistributionStatisticConfig.builder().sla(Long.MAX_VALUE).build().merge(DistributionStatisticConfig.DEFAULT), false);
// Regardless of the imputed dynamic bound for the underlying histogram, Double.MAX_VALUE is always too large.
histogram.recordDouble(Double.MAX_VALUE);
assertThat(histogram.takeSnapshot(0, 0, 0).histogramCounts()).containsExactly(new CountAtBucket(Long.MAX_VALUE, 0));
}
use of io.micrometer.core.instrument.MockClock in project micrometer by micrometer-metrics.
the class TimeWindowPercentileHistogramTest method sampleValueAboveMaximumExpectedValue.
@Test
void sampleValueAboveMaximumExpectedValue() {
TimeWindowPercentileHistogram histogram = new TimeWindowPercentileHistogram(new MockClock(), DistributionStatisticConfig.builder().sla(3).maximumExpectedValue(2L).build().merge(DistributionStatisticConfig.DEFAULT), false);
histogram.recordDouble(3);
assertThat(histogram.takeSnapshot(0, 0, 0).histogramCounts()).containsExactly(new CountAtBucket(3, 1));
}
use of io.micrometer.core.instrument.MockClock in project micrometer by micrometer-metrics.
the class TimeWindowPercentileHistogramTest method histogramsAreCumulative.
@Test
void histogramsAreCumulative() {
TimeWindowPercentileHistogram histogram = new TimeWindowPercentileHistogram(new MockClock(), DistributionStatisticConfig.builder().sla(3, 6, 7).build().merge(DistributionStatisticConfig.DEFAULT), false);
histogram.recordDouble(3);
assertThat(histogram.takeSnapshot(0, 0, 0).histogramCounts()).contains(new CountAtBucket(3, 1));
histogram.recordDouble(6);
// Proves that the accumulated histogram is truly cumulative, and not just a representation
// of the last snapshot
assertThat(histogram.takeSnapshot(0, 0, 0).histogramCounts()).containsExactly(new CountAtBucket(3, 1), new CountAtBucket(6, 2), new CountAtBucket(7, 2));
}
use of io.micrometer.core.instrument.MockClock in project micrometer by micrometer-metrics.
the class TimeWindowPercentileHistogramTest method timeBasedSlidingWindow.
@Test
void timeBasedSlidingWindow() {
final DistributionStatisticConfig config = DistributionStatisticConfig.builder().percentiles(0.0, 0.5, 0.75, 0.9, 0.99, 0.999, 1.0).expiry(Duration.ofSeconds(4)).bufferLength(4).build().merge(DistributionStatisticConfig.DEFAULT);
MockClock clock = new MockClock();
// Start from 0 for more comprehensive timing calculation.
clock.add(-1, TimeUnit.NANOSECONDS);
assertThat(clock.wallTime()).isZero();
Histogram histogram = new TimeWindowPercentileHistogram(clock, config, false);
histogram.recordLong(10);
histogram.recordLong(20);
assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(9.0, 11.0);
assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(19.0, 21.0);
// 900
clock.add(900, TimeUnit.MILLISECONDS);
histogram.recordLong(30);
histogram.recordLong(40);
assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(9.0, 11.0);
assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(38.0, 42.0);
// 999
clock.add(99, TimeUnit.MILLISECONDS);
histogram.recordLong(9);
histogram.recordLong(60);
assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(8.0, 10.0);
assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(58.0, 62.0);
// 1000
clock.add(1, TimeUnit.MILLISECONDS);
histogram.recordLong(12);
histogram.recordLong(70);
assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(8.0, 10.0);
assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(68.0, 72.0);
// 2001
clock.add(1001, TimeUnit.MILLISECONDS);
histogram.recordLong(13);
histogram.recordLong(80);
assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(8.0, 10.0);
assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(75.0, 85.0);
// 3001
clock.add(1000, TimeUnit.MILLISECONDS);
assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(8.0, 10.0);
assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(75.0, 85.0);
// 4000
clock.add(999, TimeUnit.MILLISECONDS);
assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(11.0, 13.0);
assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(75.0, 85.0);
histogram.recordLong(1);
histogram.recordLong(200);
assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(0.0, 2.0);
assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(190.0, 210.0);
// 14000
clock.add(10000, TimeUnit.MILLISECONDS);
assertThat(percentileValue(histogram, 0.0)).isZero();
assertThat(percentileValue(histogram, 1.0)).isZero();
histogram.recordLong(3);
// 17999
clock.add(3999, TimeUnit.MILLISECONDS);
assertThat(percentileValue(histogram, 0.0)).isStrictlyBetween(2.0, 4.0);
assertThat(percentileValue(histogram, 1.0)).isStrictlyBetween(2.0, 4.0);
// 18000
clock.add(1, TimeUnit.MILLISECONDS);
assertThat(percentileValue(histogram, 0.0)).isZero();
assertThat(percentileValue(histogram, 1.0)).isZero();
}
Aggregations