コード例 #1
0
  public double correlation(final Histogram first, final int displacement, final Histogram second) {
    // number of bins (classes)
    final int numberOfClasses = first.getNumberOfClasses();
    // start value
    final double start = first.getStart();
    // stop value
    final double stop = first.getStop();
    // classWidth
    final double classWidth = first.getClassWidth();

    int actualDisplacement = displacement;
    // make displacement positive
    if (actualDisplacement < 0) {
      actualDisplacement =
          ((actualDisplacement % numberOfClasses) + numberOfClasses) % numberOfClasses;
    }

    double distance = 0.0;

    for (double current = start + classWidth / 2; current <= stop; current += classWidth) {
      final double displacedValue =
          (current + actualDisplacement * classWidth) % (numberOfClasses * classWidth);
      distance += Math.pow(first.getCount(current) * second.getCount(displacedValue), 0.5);
    }

    return Math.log(distance);
  }
  @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();
  }
  @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 reportsHistogramValues() throws Exception {
    final Histogram histogram = mock(Histogram.class);
    when(histogram.getCount()).thenReturn(1L);
    when(histogram.getMax()).thenReturn(2L);
    when(histogram.getMean()).thenReturn(3.0);
    when(histogram.getMin()).thenReturn(4L);
    when(histogram.getStdDev()).thenReturn(5.0);

    final Snapshot snapshot = mock(Snapshot.class);
    when(snapshot.getMedian()).thenReturn(6.0);
    when(snapshot.get75thPercentile()).thenReturn(7.0);
    when(snapshot.get95thPercentile()).thenReturn(8.0);
    when(snapshot.get98thPercentile()).thenReturn(9.0);
    when(snapshot.get99thPercentile()).thenReturn(10.0);
    when(snapshot.get999thPercentile()).thenReturn(11.0);

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

    reporter.report(
        this.<Gauge>map(),
        this.<Counter>map(),
        map("test.histogram", histogram),
        this.<Meter>map(),
        this.<Timer>map());

    verify(ganglia)
        .announce(
            "test.histogram.count", "1", GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, "test");
    verify(ganglia)
        .announce(
            "test.histogram.max", "2", GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, "test");
    verify(ganglia)
        .announce(
            "test.histogram.mean", "3.0", GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, "test");
    verify(ganglia)
        .announce(
            "test.histogram.min", "4", GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, "test");
    verify(ganglia)
        .announce(
            "test.histogram.stddev",
            "5.0",
            GMetricType.DOUBLE,
            "",
            GMetricSlope.BOTH,
            60,
            0,
            "test");
    verify(ganglia)
        .announce(
            "test.histogram.p50", "6.0", GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, "test");
    verify(ganglia)
        .announce(
            "test.histogram.p75", "7.0", GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, "test");
    verify(ganglia)
        .announce(
            "test.histogram.p95", "8.0", GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, "test");
    verify(ganglia)
        .announce(
            "test.histogram.p98", "9.0", GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, "test");
    verify(ganglia)
        .announce(
            "test.histogram.p99", "10.0", GMetricType.DOUBLE, "", GMetricSlope.BOTH, 60, 0, "test");
    verify(ganglia)
        .announce(
            "test.histogram.p999",
            "11.0",
            GMetricType.DOUBLE,
            "",
            GMetricSlope.BOTH,
            60,
            0,
            "test");
    verifyNoMoreInteractions(ganglia);
  }