private static final double DELTA = 1e-6;
@Test
public void testUpdate() throws Exception {
BasicCounter basicCounter = new BasicCounter(MonitorConfig.builder("basicCounter").build());
ManualClock manualClock = new ManualClock(0);
StepCounter stepCounter = new StepCounter(MonitorConfig.builder("stepCounter").build(),
manualClock);
LongGauge gauge = new LongGauge(MonitorConfig.builder("longGauge").build());
ImmutableList<? extends AbstractMonitor<Number>> monitors = ImmutableList.of(basicCounter,
stepCounter, gauge);
MemoryMetricObserver observer = new MemoryMetricObserver("normalization-test", 1);
NormalizationTransform normalizationTransform = new NormalizationTransform(observer, 60,
120, TimeUnit.SECONDS, manualClock);
CounterToRateMetricTransform toRateMetricTransform =
new CounterToRateMetricTransform(normalizationTransform, 60,
120, TimeUnit.SECONDS, manualClock);
double[] rates = {0.5 / 60.0, 2 / 60.0, 3 / 60.0, 4 / 60.0};
double[] expectedNormalized = {
rates[0] * (2.0 / 3.0), // 20000L over stepBoundary
rates[0] * (1.0 / 3.0) + rates[1] * (2.0 / 3.0),
rates[1] * (1.0 / 3.0) + rates[2] * (2.0 / 3.0),
rates[2] * (1.0 / 3.0) + rates[3] * (2.0 / 3.0)};
for (int i = 1; i < 5; ++i) {
long now = 20000L + i * 60000L;
long stepBoundary = i * 60000L;
manualClock.set(now);
basicCounter.increment(i);
stepCounter.increment(i);
gauge.set((long) i);
List<Metric> metrics = getValue(monitors, manualClock);
toRateMetricTransform.update(metrics);
List<Metric> o = observer.getObservations().get(0);
assertEquals(o.size(), 3);
double basicCounterVal = o.get(0).getNumberValue().doubleValue();
double stepCounterVal = o.get(1).getNumberValue().doubleValue();
double gaugeVal = o.get(2).getNumberValue().doubleValue();
assertEquals(gaugeVal, (double) i, DELTA);
// rate per second for the prev interval
assertEquals(stepCounterVal, (i - 1) / 60.0, DELTA);
assertEquals(basicCounterVal, expectedNormalized[i - 1], DELTA);
for (Metric m : o) {
assertEquals(m.getTimestamp(), stepBoundary);
}
}
// no updates to anything, just clock forward
int i = 5;
manualClock.set(i * 60000L + 20000L);
List<Metric> metrics = getValue(monitors, manualClock);
toRateMetricTransform.update(metrics);
List<Metric> o = observer.getObservations().get(0);
assertEquals(o.size(), 3);