/** * {@inheritDoc} */ @Override public void increment() { increment(1L); }
/** * {@inheritDoc} */ @Override public void increment(long amount) { long now = clock.now() / 1000L; if (now != currentSecond.get()) { currentCount.set(0); currentSecond.set(now); } long count = currentCount.addAndGet(amount); updatePeak(count); } }
@Test public void testIncrement() throws Exception { PeakRateCounter c = newInstance("foo"); assertEquals(c.getValue().longValue(), 0L); for (int i = 0; i < 5; i++) { clock.set(i * 1000L); c.increment(); } assertEquals(c.getValue().longValue(), 1L, "Delta of 5 in 5 seconds, e.g. peak rate = average, 1 per second"); for (int i = 0; i < 5; i++) { clock.set((5 + i) * 1000L); c.increment(3); } assertEquals(c.getValue().longValue(), 3L, "Delta of 15 in 5 seconds, e.g. peak rate = average, 3 per second"); clock.set(10 * 1000L); c.increment(10); for (int i = 0; i < 3; i++) { clock.set((11 + i) * 1000L); c.increment(3); } c.increment(); assertEquals(c.getValue().longValue(), 10L, "Delta of 15 in 5 seconds, e.g. peak rate = 10, average = 3, min = 1 per second"); clock.set(19 * 1000L); assertEquals(c.getValue().longValue(), 10L, "Delta of 0 in 5 seconds, e.g. peak rate = previous max, 10 per second"); }
@Test public void testPeakRateCounter() { PeakRateCounter c = new PeakRateCounter(CONFIG); DefaultMonitorRegistry.getInstance().register(c); c.increment(); PolledMeter.update(registry); assertEquals(1.0, registry.gauge(ID).value()); }
/** * {@inheritDoc} */ public String toString() { return "PeakRateCounter{config=" + config + ", max rate per second=" + getValue() + '}'; }
@Test public void testHasRightType() throws Exception { Tag type = newInstance("foo").getConfig().getTags().getTag(DataSourceType.KEY); assertEquals(type.getValue(), "GAUGE"); }
private void updatePeak(long v) { for (int i = 0; i < Pollers.NUM_POLLERS; ++i) { updatePeakPoller(i, v); } }
@Override public PeakRateCounter newInstance(String name) { return new PeakRateCounter(MonitorConfig.builder(name).build(), clock); }
@Test public void testPeakRateCounterRemove() { PeakRateCounter c = new PeakRateCounter(CONFIG); DefaultMonitorRegistry.getInstance().register(c); DefaultMonitorRegistry.getInstance().unregister(c); c.increment(); PolledMeter.update(registry); assertEquals(0, registry.stream().count()); }
/** * {@inheritDoc} */ public String toString() { return "PeakRateCounter{config=" + config + ", max rate per second=" + getValue() + '}'; }
private void updatePeak(long v) { for (int i = 0; i < Pollers.NUM_POLLERS; ++i) { updatePeakPoller(i, v); } }
/** * {@inheritDoc} */ @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj == null || !(obj instanceof PeakRateCounter)) { return false; } final PeakRateCounter c = (PeakRateCounter) obj; final double v = getValue().doubleValue(); final double otherV = c.getValue().doubleValue(); return config.equals(c.getConfig()) && Double.compare(v, otherV) == 0; }
/** * {@inheritDoc} */ @Override public void increment() { increment(1L); }
/** * {@inheritDoc} */ @Override public void increment(long amount) { long now = clock.now() / 1000L; if (now != currentSecond.get()) { currentCount.set(0); currentSecond.set(now); } long count = currentCount.addAndGet(amount); updatePeak(count); } }
/** * {@inheritDoc} */ @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj == null || !(obj instanceof PeakRateCounter)) { return false; } final PeakRateCounter c = (PeakRateCounter) obj; final double v = getValue().doubleValue(); final double otherV = c.getValue().doubleValue(); return config.equals(c.getConfig()) && Double.compare(v, otherV) == 0; }