Exemplo n.º 1
0
  @Test
  public void testTimer() throws Exception {
    ManualClock clock = new ManualClock(0);
    DurationTimer timer = new DurationTimer(MonitorConfig.builder("test").build(), clock);
    Stopwatch s = timer.start();
    clock.set(10 * 1000L);
    assertEquals(10, s.getDuration());

    Monitor<Long> duration = getDuration(timer.getMonitors());
    Monitor<Long> activeTasks = getActiveTasks(timer.getMonitors());
    assertEquals(10L, duration.getValue().longValue());
    assertEquals(1L, activeTasks.getValue().longValue());
    clock.set(20 * 1000L);

    assertEquals(20L, duration.getValue().longValue());
    assertEquals(1L, activeTasks.getValue().longValue());

    Stopwatch anotherTask = timer.start();
    assertEquals(20L, duration.getValue().longValue());
    assertEquals(2L, activeTasks.getValue().longValue());

    clock.set(30 * 1000L);
    assertEquals(40L, duration.getValue().longValue()); // 30s for the first, 10s for the second
    assertEquals(2L, activeTasks.getValue().longValue());

    s.stop();
    assertEquals(10L, duration.getValue().longValue()); // 30s for the first, 10s for the second
    assertEquals(1L, activeTasks.getValue().longValue());

    anotherTask.stop();
    assertEquals(0L, duration.getValue().longValue());
    assertEquals(0L, activeTasks.getValue().longValue());
  }
Exemplo n.º 2
0
  /**
   * Returns a stopwatch that has been started and will automatically record its result to the
   * dynamic timer specified by the given name, and sequence of (key, value) pairs. The timer uses a
   * TimeUnit of milliseconds.
   */
  public static Stopwatch start(String name, String... tags) {
    final MonitorConfig.Builder configBuilder = MonitorConfig.builder(name);
    Preconditions.checkArgument(
        tags.length % 2 == 0,
        "The sequence of (key, value) pairs must have even size: one key, one value");
    for (int i = 0; i < tags.length; i += 2) {
      configBuilder.withTag(tags[i], tags[i + 1]);
    }

    return INSTANCE.get(configBuilder.build(), TimeUnit.MILLISECONDS).start();
  }
Exemplo n.º 3
0
 @Test
 public void testValue() throws Exception {
   ManualClock clock = new ManualClock(0L);
   DurationTimer timer = new DurationTimer(MonitorConfig.builder("test").build(), clock);
   assertEquals(0L, timer.getValue().longValue());
   Stopwatch s = timer.start();
   clock.set(10 * 1000L);
   assertEquals(10L, timer.getValue().longValue());
   s.stop();
   assertEquals(0L, timer.getValue().longValue());
 }
Exemplo n.º 4
0
 /** Increment a counter specified by a name, and a sequence of (key, value) pairs. */
 public static void increment(String name, String... tags) {
   final MonitorConfig.Builder configBuilder = MonitorConfig.builder(name);
   Preconditions.checkArgument(
       tags.length % 2 == 0,
       "The sequence of (key, value) pairs must have even size: one key, one value");
   try {
     for (int i = 0; i < tags.length; i += 2) {
       configBuilder.withTag(tags[i], tags[i + 1]);
     }
     increment(configBuilder.build());
   } catch (IllegalArgumentException e) {
     LOGGER.warn("Failed to get a counter to increment: {}", e.getMessage());
   }
 }
Exemplo n.º 5
0
 /** Increment the counter for a given name, tagList by a given delta. */
 public static void increment(String name, TagList list, long delta) {
   final MonitorConfig config = MonitorConfig.builder(name).withTags(list).build();
   increment(config, delta);
 }
Exemplo n.º 6
0
 @Override
 public Monitor<?> newInstance(String name) {
   return new DurationTimer(MonitorConfig.builder(name).build());
 }