@Override public Id id() { return get().id(); }
@Override protected Id id() { return counter.id(); }
@Override protected Id id() { return counter.id(); }
@Override public String getName() { return counter.id().name(); }
private void update(Counter counter) { T obj = ref.get(); if (obj != null) { long current = f.applyAsLong(obj); if (current > previous) { final long delta = current - previous; LOGGER.trace("incrementing counter [{}] by {}", counter.id(), delta); counter.increment(delta); } else { LOGGER.trace("no update to counter [{}]: previous = {}, current = {}", counter.id(), previous, current); } previous = current; } } }
@Override public Id id() { return get().id(); }
private void update(Counter counter) { T obj = ref.get(); if (obj != null) { long current = f.applyAsLong(obj); if (current > previous) { final long delta = current - previous; LOGGER.trace("incrementing counter [{}] by {}", counter.id(), delta); counter.increment(delta); } else { LOGGER.trace("no update to counter [{}]: previous = {}, current = {}", counter.id(), previous, current); } previous = current; } } }
@Test public void testGet() { Registry r = newRegistry(5, true); Counter c = r.counter(r.createId("foo")); c.increment(42); Meter m = r.get(c.id()); Assertions.assertEquals(c.measure().iterator().next(), m.measure().iterator().next()); }
@Test public void iteratorDoesNotContainNullMeters() { Registry dflt = new ServoRegistry(); boolean found = false; Counter counter = dflt.counter("servo.testCounter"); for (Meter m : dflt) { found = m.id().equals(counter.id()); } Assertions.assertTrue(found, "id could not be found in iterator"); }
@Test public void endpointUnknownIfNotSet() { Registry registry = new DefaultRegistry(); IpcLogger logger = new IpcLogger(registry, clock, LoggerFactory.getLogger(getClass())); logger.createServerEntry() .withOwner("test") .markStart() .markEnd() .log(); registry.counters().forEach(c -> { Assertions.assertEquals("unknown", Utils.getTagValue(c.id(), "ipc.endpoint")); }); } }
private void doMeasurementTest(Counter c, int expectedValue, long expectedTime) { c.increment(expectedValue); clock.setWallTime(expectedTime); List<Measurement> measurements = Utils.toList(c.measure()); Assertions.assertEquals(1, measurements.size()); Measurement m = measurements.get(0); Assertions.assertEquals(c.id(), m.id()); Assertions.assertEquals(expectedTime, m.timestamp()); Assertions.assertEquals(expectedValue, m.value(), 0.1e-12); }
@Test public void globalIterator() { Registry dflt = new DefaultRegistry(); CompositeRegistry global = Spectator.globalRegistry(); global.removeAll(); global.add(dflt); boolean found = false; Counter counter = dflt.counter("testCounter"); for (Meter m : global) { found |= m.id().equals(counter.id()); } Assertions.assertTrue(found, "id for sub-registry could not be found in global iterator"); }
@Test public void testIncrement() { String[] tagValue = new String[] { "default" }; Counter c = factory.counter(factory.createId("testIncrement", Collections.singleton(new TestTagFactory(tagValue)))); Assertions.assertEquals(0L, c.count()); Assertions.assertEquals("testIncrement:tag=default", c.id().toString()); c.increment(); Assertions.assertEquals(1L, c.count()); c.increment(); c.increment(); Assertions.assertEquals(3L, c.count()); tagValue[0] = "value2"; Assertions.assertEquals("testIncrement:tag=value2", c.id().toString()); c.increment(); Assertions.assertEquals(1L, c.count()); tagValue[0] = "default"; Assertions.assertEquals("testIncrement:tag=default", c.id().toString()); c.increment(); Assertions.assertEquals(4L, c.count()); }
@Test public void globalIteratorWithDifferences() { Registry r1 = new DefaultRegistry(); Registry r2 = new DefaultRegistry(); CompositeRegistry global = Spectator.globalRegistry(); global.removeAll(); global.add(r1); global.add(r2); boolean found = false; Counter counter = r2.counter("testCounter"); for (Meter m : global) { found |= m.id().equals(counter.id()); } Assertions.assertTrue(found, "id for sub-registry could not be found in global iterator"); } }
@Test public void testMeasure() { Counter c = new DefaultCounter(clock, NoopId.INSTANCE); c.increment(42); clock.setWallTime(3712345L); for (Measurement m : c.measure()) { Assertions.assertEquals(m.id(), c.id()); Assertions.assertEquals(m.timestamp(), 3712345L); Assertions.assertEquals(m.value(), 42.0, 0.1e-12); } }
@Test public void measure() { Counter c = newCounter(); c.increment(42); clock.setWallTime(3712345L); for (Measurement m : c.measure()) { Assertions.assertEquals(m.id(), c.id()); Assertions.assertEquals(m.timestamp(), 3712345L); Assertions.assertEquals(m.value(), 42.0, 0.1e-12); } }
@Test public void testRegister() { Registry r = newRegistry(5, true); Counter c = new DefaultCounter(clock, r.createId("foo")); r.register(c); c.increment(); Assertions.assertEquals(c.count(), 1L); r.register(c); PolledMeter.update(r); Meter meter = r.get(c.id()); for (Measurement m : meter.measure()) { Assertions.assertEquals(m.value(), 2.0, 1e-12); } }