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);
    }
  }
  public void addSampling(String name, Sampling sampling, boolean convert) {
    final Snapshot snapshot = sampling.getSnapshot();
    maybeAdd(MEDIAN, name, convertDuration(snapshot.getMedian(), convert));
    maybeAdd(PCT_75, name, convertDuration(snapshot.get75thPercentile(), convert));
    maybeAdd(PCT_95, name, convertDuration(snapshot.get95thPercentile(), convert));
    maybeAdd(PCT_98, name, convertDuration(snapshot.get98thPercentile(), convert));
    maybeAdd(PCT_99, name, convertDuration(snapshot.get99thPercentile(), convert));
    maybeAdd(PCT_999, name, convertDuration(snapshot.get999thPercentile(), convert));

    if (!omitComplexGauges) {
      final double sum = snapshot.size() * snapshot.getMean();
      final long count = (long) snapshot.size();
      if (count > 0) {
        SourceInformation info = SourceInformation.from(sourceRegex, name);
        MultiSampleGaugeMeasurement measurement;
        try {
          measurement =
              MultiSampleGaugeMeasurement.builder(addPrefix(info.name))
                  .setSource(info.source)
                  .setCount(count)
                  .setSum(convertDuration(sum, convert))
                  .setMax(convertDuration(snapshot.getMax(), convert))
                  .setMin(convertDuration(snapshot.getMin(), convert))
                  .build();
        } catch (IllegalArgumentException e) {
          log.warn("Could not create gauge", e);
          return;
        }
        addMeasurement(measurement);
      }
    }
  }
  private void collectHistogramReports(
      List<DBObject> docs, SortedMap<String, Histogram> histograms, Date timestamp) {
    if (histograms.isEmpty()) return;

    for (Map.Entry<String, Histogram> entry : histograms.entrySet()) {
      final BasicDBObject report = getBasicDBObject(timestamp, entry.getKey(), "histogram");
      final Histogram histogram = entry.getValue();

      final Snapshot s = histogram.getSnapshot();
      report.put("count", s.size());
      report.put("75th_percentile", s.get75thPercentile());
      report.put("95th_percentile", s.get95thPercentile());
      report.put("98th_percentile", s.get98thPercentile());
      report.put("99th_percentile", s.get99thPercentile());
      report.put("999th_percentile", s.get999thPercentile());
      report.put("max", s.getMax());
      report.put("min", s.getMin());
      report.put("mean", s.getMean());
      report.put("median", s.getMedian());
      report.put("std_dev", s.getStdDev());
      docs.add(report);
    }
  }
  @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);
  }
  @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);
  }
 @Test
 public void hasAp99() throws Exception {
   assertThat(snapshot.get99thPercentile()).isEqualTo(5.0, offset(0.1));
 }