public static void recordQueueWaitTime(String taskType, long queueWaitTime) { getTimer(classQualifier, "task_queue_wait", "taskType", taskType).record(queueWaitTime, TimeUnit.MILLISECONDS); }
@Override public long count() { return get().count(); }
@Override public long totalTime() { return get().totalTime(); } }
@Test public void totalOfSquaresOverflow() { final long seconds = 10; final long nanos = TimeUnit.SECONDS.toNanos(seconds); final BigInteger s = new BigInteger("" + nanos); final BigInteger s2 = s.multiply(s); Timer t = newTimer("foo"); t.record(seconds, TimeUnit.SECONDS); clock.setWallTime(61000L); final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value(); final double factor = 1e9 * 1e9; final BigInteger perSec = s2.divide(BigInteger.valueOf(60)); Assertions.assertEquals(perSec.doubleValue() / factor, v, 1e-12); }
@Test public void testRecord() { String[] tagValue = new String[] { "default" }; Timer timer = factory.timer(factory.createId("testRecord", Collections.singleton(new TestTagFactory(tagValue)))); timer.record(42, TimeUnit.MILLISECONDS); Assertions.assertEquals("testRecord:tag=default", timer.id().toString()); Assertions.assertEquals(timer.count(), 1L); Assertions.assertEquals(42000000L, timer.totalTime()); tagValue[0] = "value2"; Assertions.assertEquals("testRecord:tag=value2", timer.id().toString()); Assertions.assertEquals(0L, timer.count()); Assertions.assertEquals(0L, timer.totalTime()); }
@Test public void testMeasure() { Timer t = newTimer(); t.record(42, TimeUnit.MILLISECONDS); clock.setWallTime(3712345L); for (Measurement m : t.measure()) { Assertions.assertEquals(m.timestamp(), 3712345L); if (m.id().equals(t.id().withTag(Statistic.count))) { Assertions.assertEquals(m.value(), 1.0, 0.1e-12); } else if (m.id().equals(t.id().withTag(Statistic.totalTime))) { Assertions.assertEquals(m.value(), 42e6, 0.1e-12); } else { Assertions.fail("unexpected id: " + m.id()); } } } }
@Test public void expiration() { final long initTime = TimeUnit.MINUTES.toMillis(30); final long fifteenMinutes = TimeUnit.MINUTES.toMillis(15); // Not expired on init, wait for activity to mark as active clock.setWallTime(initTime); Timer t = newTimer("foo"); Assertions.assertFalse(t.hasExpired()); t.record(1, TimeUnit.SECONDS); Assertions.assertFalse(t.hasExpired()); // Expires with inactivity clock.setWallTime(initTime + fifteenMinutes + 1); Assertions.assertTrue(t.hasExpired()); // Activity brings it back t.record(42, TimeUnit.SECONDS); Assertions.assertFalse(t.hasExpired()); }
@Test public void testInit() { Timer t = new DefaultTimer(clock, NoopId.INSTANCE); Assertions.assertEquals(t.count(), 0L); Assertions.assertEquals(t.totalTime(), 0L); Assertions.assertFalse(t.hasExpired()); }
@Override public Iterable<Measurement> measure() { return get().measure(); }
@Override public boolean hasExpired() { return get().hasExpired(); }
@Test public void totalOfSquaresManySmallValues() { Timer t = newTimer("foo"); BigInteger sumOfSq = new BigInteger("0"); for (int i = 0; i < 100000; ++i) { final long nanos = i; final BigInteger s = new BigInteger("" + nanos); final BigInteger s2 = s.multiply(s); sumOfSq = sumOfSq.add(s2); t.record(i, TimeUnit.NANOSECONDS); } clock.setWallTime(61000L); final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value(); final double factor = 1e9 * 1e9; sumOfSq = sumOfSq.divide(BigInteger.valueOf(60)); Assertions.assertEquals(sumOfSq.doubleValue() / factor, v, 1e-12); }
@Test public void testMeasure() { Timer timer = factory.timer(factory.createId("testMeasure")); timer.record(42, TimeUnit.MILLISECONDS); clock.setWallTime(3712345L); for (Measurement m : timer.measure()) { Assertions.assertEquals(m.timestamp(), 3712345L); if (m.id().equals(timer.id().withTag(Statistic.count))) { Assertions.assertEquals(1.0, m.value(), 0.1e-12); } else if (m.id().equals(timer.id().withTag(Statistic.totalTime))) { Assertions.assertEquals(42e6, m.value(), 0.1e-12); } else { Assertions.fail("unexpected id: " + m.id()); } } } }
@Override public Iterable<Measurement> measure() { return get().measure(); }
@Override public boolean hasExpired() { return timer.hasExpired(); }