@Test
  public void testScheduleFixedRateCallable() throws Exception {
    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isZero();
    assertThat(duration.getCount()).isZero();

    assertThat(scheduledOnce.getCount()).isZero();
    assertThat(scheduledRepetitively.getCount()).isZero();
    assertThat(scheduledOverrun.getCount()).isZero();
    assertThat(percentOfPeriod.getCount()).isZero();

    ScheduledFuture<?> theFuture =
        instrumentedScheduledExecutor.scheduleAtFixedRate(
            new Runnable() {
              public void run() {
                assertThat(submitted.getCount()).isZero();

                assertThat(running.getCount()).isEqualTo(1);

                assertThat(scheduledOnce.getCount()).isEqualTo(0);
                assertThat(scheduledRepetitively.getCount()).isEqualTo(1);

                try {
                  TimeUnit.MILLISECONDS.sleep(50);
                } catch (InterruptedException ex) {
                  Thread.currentThread().interrupt();
                }

                return;
              }
            },
            10L,
            10L,
            TimeUnit.MILLISECONDS);

    TimeUnit.MILLISECONDS.sleep(100);
    theFuture.cancel(true);
    TimeUnit.MILLISECONDS.sleep(100);

    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isNotEqualTo(0);
    assertThat(duration.getCount()).isNotEqualTo(0);
    assertThat(duration.getSnapshot().size()).isNotEqualTo(0);

    assertThat(scheduledOnce.getCount()).isZero();
    assertThat(scheduledRepetitively.getCount()).isEqualTo(1);
    assertThat(scheduledOverrun.getCount()).isNotEqualTo(0);
    assertThat(percentOfPeriod.getCount()).isNotEqualTo(0);
  }
  @Test
  public void testScheduleCallable() throws Exception {
    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isZero();
    assertThat(duration.getCount()).isZero();

    assertThat(scheduledOnce.getCount()).isZero();
    assertThat(scheduledRepetitively.getCount()).isZero();
    assertThat(scheduledOverrun.getCount()).isZero();
    assertThat(percentOfPeriod.getCount()).isZero();

    final Object obj = new Object();

    ScheduledFuture<Object> theFuture =
        instrumentedScheduledExecutor.schedule(
            new Callable<Object>() {
              public Object call() {
                assertThat(submitted.getCount()).isZero();

                assertThat(running.getCount()).isEqualTo(1);
                assertThat(completed.getCount()).isZero();
                assertThat(duration.getCount()).isZero();

                assertThat(scheduledOnce.getCount()).isEqualTo(1);
                assertThat(scheduledRepetitively.getCount()).isZero();
                assertThat(scheduledOverrun.getCount()).isZero();
                assertThat(percentOfPeriod.getCount()).isZero();

                return obj;
              }
            },
            10L,
            TimeUnit.MILLISECONDS);

    assertThat(theFuture.get()).isEqualTo(obj);

    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isEqualTo(1);
    assertThat(duration.getCount()).isEqualTo(1);
    assertThat(duration.getSnapshot().size()).isEqualTo(1);

    assertThat(scheduledOnce.getCount()).isEqualTo(1);
    assertThat(scheduledRepetitively.getCount()).isZero();
    assertThat(scheduledOverrun.getCount()).isZero();
    assertThat(percentOfPeriod.getCount()).isZero();
  }
  private void collectTimerReports(
      List<DBObject> docs, SortedMap<String, Timer> timers, Date timestamp) {
    if (timers.isEmpty()) return;
    for (Map.Entry<String, Timer> entry : timers.entrySet()) {
      final BasicDBObject report = getBasicDBObject(timestamp, entry.getKey(), "timer");
      final Timer v = entry.getValue();
      final Snapshot s = v.getSnapshot();
      // meter part
      report.put("count", v.getCount());
      report.put("rate-unit", getRateUnit());
      report.put("1-minute-rate", convertRate(v.getOneMinuteRate()));
      report.put("5-minute-rate", convertRate(v.getFiveMinuteRate()));
      report.put("15-minute-rate", convertRate(v.getFifteenMinuteRate()));
      report.put("mean-rate", convertRate(v.getMeanRate()));

      // histogram part
      report.put("duration-unit", getDurationUnit());
      report.put("75-percentile", convertDuration(s.get75thPercentile()));
      report.put("95-percentile", convertDuration(s.get95thPercentile()));
      report.put("98-percentile", convertDuration(s.get98thPercentile()));
      report.put("99-percentile", convertDuration(s.get99thPercentile()));
      report.put("999-percentile", convertDuration(s.get999thPercentile()));
      report.put("max", convertDuration(s.getMax()));
      report.put("min", convertDuration(s.getMin()));
      report.put("mean", convertDuration(s.getMean()));
      report.put("median", convertDuration(s.getMedian()));
      report.put("stddev", convertDuration(s.getStdDev()));
      docs.add(report);
    }
  }
  @Test
  public void testScheduleRunnable() throws Exception {
    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isZero();
    assertThat(duration.getCount()).isZero();

    assertThat(scheduledOnce.getCount()).isZero();
    assertThat(scheduledRepetitively.getCount()).isZero();
    assertThat(scheduledOverrun.getCount()).isZero();
    assertThat(percentOfPeriod.getCount()).isZero();

    ScheduledFuture<?> theFuture =
        instrumentedScheduledExecutor.schedule(
            new Runnable() {
              public void run() {
                assertThat(submitted.getCount()).isZero();

                assertThat(running.getCount()).isEqualTo(1);
                assertThat(completed.getCount()).isZero();
                assertThat(duration.getCount()).isZero();

                assertThat(scheduledOnce.getCount()).isEqualTo(1);
                assertThat(scheduledRepetitively.getCount()).isZero();
                assertThat(scheduledOverrun.getCount()).isZero();
                assertThat(percentOfPeriod.getCount()).isZero();
              }
            },
            10L,
            TimeUnit.MILLISECONDS);

    theFuture.get();

    assertThat(submitted.getCount()).isZero();

    assertThat(running.getCount()).isZero();
    assertThat(completed.getCount()).isEqualTo(1);
    assertThat(duration.getCount()).isEqualTo(1);
    assertThat(duration.getSnapshot().size()).isEqualTo(1);

    assertThat(scheduledOnce.getCount()).isEqualTo(1);
    assertThat(scheduledRepetitively.getCount()).isZero();
    assertThat(scheduledOverrun.getCount()).isZero();
    assertThat(percentOfPeriod.getCount()).isZero();
  }
예제 #5
0
  @Test
  public void reportsTimerValues() throws Exception {
    final Timer timer = mock(Timer.class);
    when(timer.getCount()).thenReturn(1L);
    when(timer.getMax()).thenReturn(TimeUnit.MILLISECONDS.toNanos(100));
    when(timer.getMean()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(200));
    when(timer.getMin()).thenReturn(TimeUnit.MILLISECONDS.toNanos(300));
    when(timer.getStdDev()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(400));

    when(timer.getMeanRate()).thenReturn(2.0);
    when(timer.getOneMinuteRate()).thenReturn(3.0);
    when(timer.getFiveMinuteRate()).thenReturn(4.0);
    when(timer.getFifteenMinuteRate()).thenReturn(5.0);

    final Snapshot snapshot = mock(Snapshot.class);
    when(snapshot.getMedian()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(500));
    when(snapshot.get75thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(600));
    when(snapshot.get95thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(700));
    when(snapshot.get98thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(800));
    when(snapshot.get99thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(900));
    when(snapshot.get999thPercentile()).thenReturn((double) TimeUnit.MILLISECONDS.toNanos(1000));

    when(timer.getSnapshot()).thenReturn(snapshot);

    reporter.report(
        this.<Gauge>map(),
        this.<Counter>map(),
        this.<Histogram>map(),
        this.<Meter>map(),
        map("test.another.timer", timer));

    verify(ganglia)
        .announce(
            "test.another.timer.max",
            "100.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.mean",
            "200.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.min",
            "300.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.stddev",
            "400.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.p50",
            "500.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.p75",
            "600.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.p95",
            "700.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.p98",
            "800.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.p99",
            "900.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.p999",
            "1000.0",
            GMetricType.DOUBLE,
            "milliseconds",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");

    verify(ganglia)
        .announce(
            "test.another.timer.count",
            "1",
            GMetricType.DOUBLE,
            "calls",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.mean_rate",
            "2.0",
            GMetricType.DOUBLE,
            "calls/second",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.m1_rate",
            "3.0",
            GMetricType.DOUBLE,
            "calls/second",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.m5_rate",
            "4.0",
            GMetricType.DOUBLE,
            "calls/second",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");
    verify(ganglia)
        .announce(
            "test.another.timer.m15_rate",
            "5.0",
            GMetricType.DOUBLE,
            "calls/second",
            GMetricSlope.BOTH,
            60,
            0,
            "test.another");

    verifyNoMoreInteractions(ganglia);
  }