@Test public void mostFrequentUnderLimit() { int n = 27; ManualClock clock = new ManualClock(0L, 0L); Function<String, String> f = CardinalityLimiters.mostFrequent(n, clock); for (int t = 0; t < 1000; ++t) { for (int i = 0; i < n; ++i) { Assertions.assertEquals("" + i, f.apply("" + i)); } clock.setWallTime(t * 1000); } }
@BeforeEach public void before() { clock.setWallTime(0L); clock.setMonotonicTime(0L); }
private void advanceClock(ManualClock clock) { long t = clock.wallTime(); clock.setWallTime(t + 10 * 60000 + 1); }
@Test public void recordRunnable() { dist.record(() -> clock.setMonotonicTime(clock.monotonicTime() + 2)); clock.setWallTime(step + 1); checkValue(1, 2, 4, 2); }
@Test public void rollForward() { gauge.set(42); clock.setWallTime(step + 1); checkValue(42); clock.setWallTime(step + step + 1); checkValue(0); }
@Test public void startTimeOnlyComputedLatency() { long expected = 157; long t = 1234567890L; clock.setMonotonicTime(TimeUnit.MILLISECONDS.toNanos(t)); entry.markStart(); clock.setMonotonicTime(TimeUnit.MILLISECONDS.toNanos(t + expected)); double actual = (double) entry .convert(this::toMap) .get("latency"); Assertions.assertEquals(expected / 1000.0, actual, 1e-12); }
@Test public void testLongTaskTimerHelpers() { ManualClock clock = new ManualClock(); Registry r = new DefaultRegistry(clock); LongTaskTimer c1 = r.longTaskTimer("foo", "bar", "baz", "k", "v"); assertLongTaskTimer(r, c1.id(), 0L, 0, 0L); LongTaskTimer c2 = r.longTaskTimer("foo", ArrayTagSet.create("k", "v").add(new BasicTag("bar", "baz"))); Assertions.assertEquals(c1.id(), c2.id()); long t1 = c1.start(); long t2 = c2.start(); clock.setMonotonicTime(1000L); clock.setWallTime(1L); assertLongTaskTimer(r, c1.id(), 1L, 2, 2.0e-6); c1.stop(t1); assertLongTaskTimer(r, c1.id(), 1L, 1, 1.0e-6); c2.stop(t2); assertLongTaskTimer(r, c1.id(), 1L, 0, 0L); }
@Test public void testGaugeHelpersWithCustomFunction() { AtomicLong al1 = new AtomicLong(1L); Registry r = new DefaultRegistry(new ManualClock(40, 0)); DoubleFunction<AtomicLong> f = new DoubleFunction<AtomicLong>() { @Override public double apply(double v) { return (r.clock().wallTime() - v) / 1000.0; } }; AtomicLong v1 = r.gauge("foo", al1, f); Assertions.assertSame(v1, al1); Id id1 = r.createId("foo"); assertGaugeValue(r, id1, 39.0 / 1000.0); }
@Test public void monotonicTime() { ManualClock mc = new ManualClock(); StepClock sc = new StepClock(mc, 10000); mc.setMonotonicTime(5000); Assertions.assertEquals(5000L, sc.monotonicTime()); mc.setMonotonicTime(10000); Assertions.assertEquals(10000L, sc.monotonicTime()); mc.setMonotonicTime(20212); Assertions.assertEquals(20212L, sc.monotonicTime()); } }
@Test public void rollForward() { counter.increment(42); clock.setWallTime(step + 1); checkValue(42); clock.setWallTime(step + step + 1); checkValue(0); }
@Test public void testRecordRunnable() throws Exception { Timer t = newTimer(); clock.setMonotonicTime(100L); t.record(() -> clock.setMonotonicTime(500L)); assertCountEquals(t, 1L); assertTotalEquals(t, 400L); }
@Test public void recordCallable() throws Exception { String s = dist.record(() -> { clock.setMonotonicTime(clock.monotonicTime() + 2); return "foo"; }); Assertions.assertEquals("foo", s); clock.setWallTime(step + 1); checkValue(1, 2, 4, 2); }
@Test public void testGaugeHelpersWithFunction() { AtomicLong al1 = new AtomicLong(1L); Registry r = new DefaultRegistry(new ManualClock(40, 0)); DoubleFunction<AtomicLong> f = Functions.age(r.clock()); AtomicLong v1 = r.gauge("foo", al1, f); Assertions.assertSame(v1, al1); Id id1 = r.createId("foo"); assertGaugeValue(r, id1, 39.0 / 1000.0); }
@Test public void mostFrequentAllUnique() { // Ensure we have a somewhat stable set and there isn't a memory leak if every value is // unique. For example, if a user tried to use a request id. ManualClock clock = new ManualClock(0L, 0L); Function<String, String> f = CardinalityLimiters.mostFrequent(2, clock); Set<String> values = new TreeSet<>(); for (int i = 0; i < 10000; ++i) { values.add(f.apply("" + i)); clock.setWallTime(i * 1000); } // The values less than equal 9616 should have been cleaned up based on the clock Assertions.assertFalse(f.toString().contains("9616")); Assertions.assertEquals(3, values.size()); }
@Test public void recordSquaresOverflow() { long v = (long) (Math.sqrt(Long.MAX_VALUE) / 1e9) + 1; dist.record(v, TimeUnit.SECONDS); clock.setWallTime(step + 1); double square = 1e18 * v * v; checkValue(1, v * 1000000000L, square, v * 1000000000L); }
@BeforeEach public void before() { clock.setWallTime(0L); clock.setMonotonicTime(0L); }
@Test public void measureTimestamp() { long start = clock.wallTime(); clock.setWallTime(start + step); Assertions.assertEquals(start + step, gauge.measure().iterator().next().timestamp()); clock.setWallTime(start + step * 2); Assertions.assertEquals(start + step * 2, gauge.measure().iterator().next().timestamp()); } }
@Test public void testRecordCallable() throws Exception { Timer t = newTimer(); clock.setMonotonicTime(100L); int v = t.record(() -> { clock.setMonotonicTime(500L); return 42; }); Assertions.assertEquals(v, 42); assertCountEquals(t, 1L); assertTotalEquals(t, 400L); }
@Test public void emptyExpressionList() { ManualClock clock = new ManualClock(); byte[] data = "{\"expressions\":[]}".getBytes(StandardCharsets.UTF_8); SubscriptionManager mgr = newInstance(clock, ok(data)); mgr.refresh(); Assertions.assertTrue(mgr.subscriptions().isEmpty()); }
@Test public void wallTime() { ManualClock mc = new ManualClock(); StepClock sc = new StepClock(mc, 10000); mc.setWallTime(5000); Assertions.assertEquals(0L, sc.wallTime()); mc.setWallTime(10000); Assertions.assertEquals(10000L, sc.wallTime()); mc.setWallTime(20212); Assertions.assertEquals(20000L, sc.wallTime()); }