private void assertMetricCount(String sourceId, String metricName, int expectedCount) {
    String key = sourceId + "." + metricName;
    SourceMetric sourceMetric = sourceMetrics.metrics.get(key);

    if (sourceMetric.isHistogram()) {
      Histogram histogram = (Histogram) sourceMetric.getMetric();
      assertThat(histogram.getCount(), is((long) expectedCount));
    } else {
      Meter meter = (Meter) sourceMetric.getMetric();
      assertThat(meter.getCount(), is((long) expectedCount));
    }
  }
 private void collectMeterReports(
     List<DBObject> docs, SortedMap<String, Meter> meters, Date timestamp) {
   if (meters.isEmpty()) return;
   for (Map.Entry<String, Meter> entry : meters.entrySet()) {
     final BasicDBObject report = getBasicDBObject(timestamp, entry.getKey(), "meter");
     final Meter v = entry.getValue();
     report.put("count", v.getCount());
     report.put("1-minute-rate", v.getOneMinuteRate());
     report.put("5-minute-rate", v.getFiveMinuteRate());
     report.put("15-minute-rate", v.getFifteenMinuteRate());
     report.put("mean-rate", v.getMeanRate());
     docs.add(report);
   }
 }
  protected Map<String, Object> buildMeterMap(Meter m) {
    Map<String, Object> metrics = Maps.newHashMap();

    if (m == null) {
      return metrics;
    }

    Map<String, Object> rate = Maps.newHashMap();
    rate.put("one_minute", m.getOneMinuteRate());
    rate.put("five_minute", m.getFiveMinuteRate());
    rate.put("fifteen_minute", m.getFifteenMinuteRate());
    rate.put("total", m.getCount());
    rate.put("mean", m.getMeanRate());

    metrics.put("rate_unit", "events/second");
    metrics.put("rate", rate);

    return metrics;
  }
    @Override
    public boolean matchesSafely(JsonObject jsonObject) {

      JsonObject jsonMetric = jsonObject.get("metric").asObject();
      JsonObject jsonCounter;
      JsonObject jsonMeter;
      JsonObject jsonTimer;
      Counter counter;
      Meter meter;
      Timer timer;

      // check counter metric
      if (jsonMetric.get("counter") != null) {
        jsonCounter = jsonMetric.get("counter").asObject();
        counter = (Counter) metric;
        if (jsonCounter.get("counter").asLong() != counter.getCount()) {
          reason = "counter " + counter.getCount();
          return false;
        }
      }

      // check meter metric
      if (jsonMetric.get("meter") != null) {
        jsonMeter = jsonMetric.get("meter").asObject();
        meter = (Meter) metric;

        if (jsonMeter.get("counter").asLong() != meter.getCount()) {
          reason = "counter " + meter.getCount();
          return false;
        }

        if (jsonMeter.get("1_min_rate").asDouble() != meter.getOneMinuteRate()) {
          reason = "1 minute rate " + meter.getOneMinuteRate();
          return false;
        }

        if (jsonMeter.get("5_min_rate").asDouble() != meter.getOneMinuteRate()) {
          reason = "5 minute rate " + meter.getFiveMinuteRate();
          return false;
        }

        if (jsonMeter.get("15_min_rate").asDouble() != meter.getFifteenMinuteRate()) {
          reason = "15 minute rate " + meter.getFifteenMinuteRate();
          return false;
        }
      }

      if (jsonMetric.get("timer") != null) {
        jsonTimer = jsonMetric.get("timer").asObject();
        timer = (Timer) metric;

        if (jsonTimer.get("counter").asLong() != timer.getCount()) {
          reason = "counter " + timer.getCount();
          return false;
        }

        if (jsonTimer.get("1_min_rate").asDouble() != timer.getOneMinuteRate()) {
          reason = "1 minute rate " + timer.getOneMinuteRate();
          return false;
        }

        if (jsonTimer.get("5_min_rate").asDouble() != timer.getOneMinuteRate()) {
          reason = "5 minute rate " + timer.getFiveMinuteRate();
          return false;
        }

        if (jsonTimer.get("15_min_rate").asDouble() != timer.getFifteenMinuteRate()) {
          reason = "15 minute rate " + timer.getFifteenMinuteRate();
          return false;
        }

        if (jsonTimer.get("mean").asDouble() != nanoToMs(timer.getSnapshot().getMean())) {
          reason = "mean " + timer.getSnapshot().getMean();
          return false;
        }

        if (jsonTimer.get("min").asDouble() != nanoToMs(timer.getSnapshot().getMin())) {
          reason = "min " + timer.getSnapshot().getMin();
          return false;
        }

        if (jsonTimer.get("max").asDouble() != nanoToMs(timer.getSnapshot().getMax())) {
          reason = "max " + timer.getSnapshot().getMax();
          return false;
        }

        if (jsonTimer.get("stddev").asDouble() != nanoToMs(timer.getSnapshot().getStdDev())) {
          reason = "stddev " + timer.getSnapshot().getStdDev();
          return false;
        }
      }

      return true;
    }