Java Code Examples for com.netflix.spectator.api.Timer#record()

The following examples show how to use com.netflix.spectator.api.Timer#record() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void totalOfSquaresManyBigValues() {
  Timer t = newTimer("foo");
  BigInteger sumOfSq = new BigInteger("0");
  for (int i = 0; i < 100000; ++i) {
    final long nanos = TimeUnit.SECONDS.toNanos(i);
    final BigInteger s = new BigInteger("" + nanos);
    final BigInteger s2 = s.multiply(s);
    sumOfSq = sumOfSq.add(s2);
    t.record(i, TimeUnit.SECONDS);
  }
  clock.setWallTime(61000L);

  final double v = Utils.first(t.measure(), Statistic.totalOfSquares).value();

  // Expected :3.3332833335E14
  // Actual   :3.3332833334999825E14
  final double factor = 1e9 * 1e9;
  sumOfSq = sumOfSq.divide(BigInteger.valueOf(60));
  Assertions.assertEquals(sumOfSq.doubleValue() / factor, v, 2.0);
}
 
Example 2
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: DefaultPlaceholderTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@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());
    }
  }
}
 
Example 4
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 5
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void timerMeasure() {
  Timer t = registry.timer("foo");
  t.record(42, TimeUnit.SECONDS);
  int i = 0;
  for (Measurement m : t.measure()) {
    ++i;
    Assertions.assertEquals("foo", m.id().name());
    switch (Utils.getTagValue(m.id(), "statistic")) {
      case "count":
        Assertions.assertEquals(1.0, m.value(), 1e-12);
        break;
      case "total":
        Assertions.assertEquals(42.0, m.value(), 1e-12);
        break;
      case "max":
        Assertions.assertEquals(42.0, m.value(), 1e-12);
        break;
      default:
        Assertions.fail("invalid statistic for measurment: " + m);
    }
  }
  Assertions.assertEquals(3, i);
}
 
Example 6
Source File: GcLogger.java    From spectator with Apache License 2.0 6 votes vote down vote up
private void processGcEvent(GarbageCollectionNotificationInfo info) {
  GcEvent event = new GcEvent(info, jvmStartTime + info.getGcInfo().getStartTime());
  gcLogs.get(info.getGcName()).add(event);
  if (LOGGER.isDebugEnabled()) {
    LOGGER.debug(event.toString());
  }

  // Update pause timer for the action and cause...
  Id eventId = (isConcurrentPhase(info) ? CONCURRENT_PHASE_TIME : PAUSE_TIME)
    .withTag("action", info.getGcAction())
    .withTag("cause", info.getGcCause());
  Timer timer = Spectator.globalRegistry().timer(eventId);
  timer.record(info.getGcInfo().getDuration(), TimeUnit.MILLISECONDS);

  // Update promotion and allocation counters
  updateMetrics(info.getGcName(), info.getGcInfo());

  // Notify an event listener if registered
  if (eventListener != null) {
    try {
      eventListener.onComplete(event);
    } catch (Exception e) {
      LOGGER.warn("exception thrown by event listener", e);
    }
  }
}
 
Example 7
Source File: DefaultPlaceholderTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 8
Source File: DefaultPlaceholderTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecordCallableException() throws Exception {
  Timer timer = factory.timer(factory.createId("testRecordCallableException"));
  clock.setMonotonicTime(100L);
  boolean seen = false;
  try {
    timer.record(() -> {
      clock.setMonotonicTime(500L);
      throw new Exception("foo");
    });
  } catch (Exception e) {
    seen = true;
  }
  Assertions.assertTrue(seen);
  Assertions.assertEquals(1L, timer.count());
  Assertions.assertEquals(400L, timer.totalTime());
}
 
Example 9
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@Test
public void testRecordRunnableException() throws Exception {
  Timer t = newTimer("foo");
  clock.setMonotonicTime(100L);
  boolean seen = false;
  try {
    t.record((Runnable) () -> {
      clock.setMonotonicTime(500L);
      throw new RuntimeException("foo");
    });
  } catch (Exception e) {
    seen = true;
  }
  Assertions.assertTrue(seen);
  Assertions.assertEquals(t.count(), 1L);
  Assertions.assertEquals(t.totalTime(), 400L);
}
 
Example 10
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 11
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecordNegative() {
  Timer t = newTimer("foo");
  t.record(-42, TimeUnit.MILLISECONDS);
  Assertions.assertEquals(t.count(), 0L);
  Assertions.assertEquals(t.totalTime(), 0L);
}
 
Example 12
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecord() {
  Timer t = newTimer("foo");
  t.record(42, TimeUnit.MILLISECONDS);
  Assertions.assertEquals(t.count(), 1L);
  Assertions.assertEquals(t.totalTime(), 42000000L);
}
 
Example 13
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecordCallable() throws Exception {
  Timer t = newTimer("foo");
  clock.setMonotonicTime(100L);
  int v = t.record(() -> {
    clock.setMonotonicTime(500L);
    return 42;
  });
  Assertions.assertEquals(v, 42);
  Assertions.assertEquals(t.count(), 1L);
  Assertions.assertEquals(t.totalTime(), 400L);
}
 
Example 14
Source File: DefaultPlaceholderTimerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecordRunnable() throws Exception {
  Timer timer = factory.timer(factory.createId("testRecordRunnable"));
  clock.setMonotonicTime(100L);
  timer.record(() -> clock.setMonotonicTime(500L));
  Assertions.assertEquals(1L, timer.count());
  Assertions.assertEquals(timer.totalTime(), 400L);
}
 
Example 15
Source File: DefaultPlaceholderTimerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecordNegative() {
  Timer timer = factory.timer(factory.createId("testRecordNegative"));
  timer.record(-42, TimeUnit.MILLISECONDS);
  Assertions.assertEquals(timer.count(), 0L);
  Assertions.assertEquals(0L, timer.totalTime());
}
 
Example 16
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void timerGet() {
  Timer t1 = registry.timer("foo");
  t1.record(1, TimeUnit.SECONDS);

  Timer t2 = (Timer) registry.get(registry.createId("foo"));
  Assertions.assertEquals(1, t2.count());
}
 
Example 17
Source File: ServoTimerTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRecordRunnable() throws Exception {
  Timer t = newTimer("foo");
  clock.setMonotonicTime(100L);
  t.record(() -> clock.setMonotonicTime(500L));
  Assertions.assertEquals(t.count(), 1L);
  Assertions.assertEquals(t.totalTime(), 400L);
}
 
Example 18
Source File: MicrometerRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void timerRecord() {
  Timer t = registry.timer("foo");
  Assertions.assertEquals(0, t.count());
  Assertions.assertEquals(0, t.totalTime());
  t.record(42, TimeUnit.SECONDS);
  Assertions.assertEquals(1, t.count());
  Assertions.assertEquals(TimeUnit.SECONDS.toNanos(42), t.totalTime());
}
 
Example 19
Source File: AtlasAutoConfigurationIntegrationTests.java    From spring-cloud-netflix-contrib with Apache License 2.0 5 votes vote down vote up
@Test
public void metricsAreSentToAtlasPeriodically() throws InterruptedException {
	Timer t = registry.timer("t");

	for (int i = 0; i < 1000; i++)
		t.record(100, TimeUnit.MILLISECONDS);
	Thread.sleep(1500);

	Mockito.verify(exporter, Mockito.atLeastOnce()).export();
}
 
Example 20
Source File: SNSNotificationMetric.java    From metacat with Apache License 2.0 5 votes vote down vote up
void recordTime(final SNSMessage<?> message, final String timeName) {
    final Timer timer = this.registry.timer(
        timeName,
        Metrics.TagEventsType.getMetricName(),
        message.getClass().getName()
    );
    timer.record(this.registry.clock().wallTime() - message.getTimestamp(), TimeUnit.MILLISECONDS);
}