/** * Get or create a double distribution summary with the specified id. * * @param id * Identifier for the metric being registered. * @return * Distribution summary corresponding to the id. */ public static DoubleDistributionSummary get(Id id) { return get(Spectator.globalRegistry(), id); }
/** * Updates the statistics kept by the summary with the specified amount. * * @param amount * Amount for an event being measured. For example, if the size in bytes of responses * from a server. If the amount is less than 0 the value will be dropped. */ public void record(double amount) { if (amount >= 0.0) { add(totalAmount, amount); add(totalOfSquares, amount * amount); max(max, amount); count.incrementAndGet(); lastUpdateTime.set(clock.wallTime()); } }
@Override public Iterable<Measurement> measure() { final long now = clock.wallTime(); final long prev = lastResetTime.get(); final long delta = now - prev; final boolean reset = delta > resetFreq; if (reset) { lastResetTime.set(now); } final List<Measurement> ms = new ArrayList<>(3); if (delta > 1000L) { ms.add(new Measurement(countId, now, toRateLong(count, delta, reset))); ms.add(new Measurement(totalAmountId, now, toRateDouble(totalAmount, delta, reset))); ms.add(new Measurement(totalOfSquaresId, now, toRateDouble(totalOfSquares, delta, reset))); ms.add(new Measurement(maxId, now, toDouble(max, reset))); } return ms; }
/** * Get or create a double distribution summary with the specified id. * * @param registry * Registry to use. * @param id * Identifier for the metric being registered. * @return * Distribution summary corresponding to the id. */ static DoubleDistributionSummary get(Registry registry, Id id) { DoubleDistributionSummary instance = INSTANCES.get(id); if (instance == null) { final Clock c = registry.clock(); DoubleDistributionSummary tmp = new DoubleDistributionSummary(c, id, RESET_FREQ); instance = INSTANCES.putIfAbsent(id, tmp); if (instance == null) { instance = tmp; registry.register(tmp); } } return instance; }
@Test public void testRecord() { DoubleDistributionSummary t = newInstance(); t.record(42.0); Assertions.assertEquals(t.count(), 1L); Assertions.assertEquals(t.totalAmount(), 42.0, 1e-12); }
@Test public void testExpire() { clock.setWallTime(0L); DoubleDistributionSummary t = newInstance(); Assertions.assertFalse(t.hasExpired()); t.record(42.0); clock.setWallTime(15L * 60000L); Assertions.assertFalse(t.hasExpired()); clock.setWallTime(15L * 60000L + 1); Assertions.assertTrue(t.hasExpired()); t.record(42.0); Assertions.assertFalse(t.hasExpired()); }
@Test public void testInit() { DoubleDistributionSummary t = newInstance(); Assertions.assertEquals(t.count(), 0L); Assertions.assertEquals(t.totalAmount(), 0.0, 1e-12); }
/** * Get or create a double distribution summary with the specified id. * * @param registry * Registry to use. * @param id * Identifier for the metric being registered. * @return * Distribution summary corresponding to the id. */ static DoubleDistributionSummary get(Registry registry, Id id) { DoubleDistributionSummary instance = INSTANCES.get(id); if (instance == null) { final Clock c = registry.clock(); DoubleDistributionSummary tmp = new DoubleDistributionSummary(c, id, RESET_FREQ); instance = INSTANCES.putIfAbsent(id, tmp); if (instance == null) { instance = tmp; registry.register(tmp); } } return instance; }
@Override public Iterable<Measurement> measure() { final long now = clock.wallTime(); final long prev = lastResetTime.get(); final long delta = now - prev; final boolean reset = delta > resetFreq; if (reset) { lastResetTime.set(now); } final List<Measurement> ms = new ArrayList<>(3); if (delta > 1000L) { ms.add(new Measurement(countId, now, toRateLong(count, delta, reset))); ms.add(new Measurement(totalAmountId, now, toRateDouble(totalAmount, delta, reset))); ms.add(new Measurement(totalOfSquaresId, now, toRateDouble(totalOfSquares, delta, reset))); ms.add(new Measurement(maxId, now, toDouble(max, reset))); } return ms; }
/** * Updates the statistics kept by the summary with the specified amount. * * @param amount * Amount for an event being measured. For example, if the size in bytes of responses * from a server. If the amount is less than 0 the value will be dropped. */ public void record(double amount) { if (amount >= 0.0) { add(totalAmount, amount); add(totalOfSquares, amount * amount); max(max, amount); count.incrementAndGet(); lastUpdateTime.set(clock.wallTime()); } }
/** * Get or create a double distribution summary with the specified id. * * @param id * Identifier for the metric being registered. * @return * Distribution summary corresponding to the id. */ public static DoubleDistributionSummary get(Id id) { return get(Spectator.globalRegistry(), id); }
private DoubleDistributionSummary newInstance() { clock.setWallTime(0L); return new DoubleDistributionSummary(clock, registry.createId("foo"), 60000); }