MetricsCollector implements MetricsData { public synchronized void addValueNow(final float value) { addValue(value, UTCTime.now()); } MetricsCollector(@Nonnull final Duration totalMetricDuration, final int maxEntries); @Nonnull static MetricsCollector create(@Nonnull final Period interval); @Nonnull synchronized List<MetricsTimeSlot> getSlots(); synchronized void setSlots(@Nonnull final Collection<MetricsTimeSlot> slots); @Nonnull Duration getTotalMetricDuration(); synchronized void addValueNow(final float value); synchronized void addValue(final float value, @Nonnull final DateTime now); @Override synchronized int getCount(); @Override synchronized float getSum(); @Override synchronized float getAvg(); @Override synchronized float getStdDev(); @Override synchronized float getMax(); @Override synchronized float getMin();  }
@Test public void testWithOneValue() { LOG.info("testWithOneValue"); final MetricsCollector metricsCollector = new MetricsCollector(Duration.millis(METRIC_DURATION_MSECS), METRIC_NR_ENTRIES); final float value = 8.0f; metricsCollector.addValueNow(value); verifyCollectorCountMinMaxSumAvgStdDev(metricsCollector, 1, value, value, value, value, 0.0f); }
@Test public void testPositiveAndNegativeValue() { LOG.info("testPositiveAndNegativeValue"); final MetricsCollector metricsCollector = new MetricsCollector(Duration.millis(METRIC_DURATION_MSECS), METRIC_NR_ENTRIES); final float positive = 8.0f; final float negative = -4.0f; metricsCollector.addValueNow(positive); metricsCollector.addValueNow(negative); verifyCollectorCountMinMaxSumAvgStdDev(metricsCollector, 2, negative, positive, 4.0f, 2.0f, 8.485281374f); }
@Test public void testNegativeValues1() { LOG.info("testNegativeValues1"); final MetricsCollector metricsCollector = new MetricsCollector(Duration.millis(METRIC_DURATION_MSECS), METRIC_NR_ENTRIES); metricsCollector.addValueNow(-26.0f); metricsCollector.addValueNow(-27.0f); metricsCollector.addValueNow(-28.0f); verifyCollectorCountMinMaxSumAvgStdDev(metricsCollector, 3, -28.0f, -26.0f, -81.0f, -27.0f, 1.0f); }
@Test public void testNegativeValues2() { LOG.info("testNegativeValues2"); final MetricsCollector metricsCollector = new MetricsCollector(Duration.millis(METRIC_DURATION_MSECS), METRIC_NR_ENTRIES); metricsCollector.addValueNow(-28.0f); metricsCollector.addValueNow(-27.0f); metricsCollector.addValueNow(-26.0f); verifyCollectorCountMinMaxSumAvgStdDev(metricsCollector, 3, -28.0f, -26.0f, -81.0f, -27.0f, 1.0f); }
@Test public void testPositiveValues1() { LOG.info("testPositiveValues1"); final MetricsCollector metricsCollector = new MetricsCollector(Duration.millis(METRIC_DURATION_MSECS), METRIC_NR_ENTRIES); metricsCollector.addValueNow(26.0f); metricsCollector.addValueNow(27.0f); metricsCollector.addValueNow(28.0f); verifyCollectorCountMinMaxSumAvgStdDev(metricsCollector, 3, 26.0f, 28.0f, 81.0f, 27.0f, 1.0f); }
@Test public void testPositiveValues2() { LOG.info("testPositiveValues2"); final MetricsCollector metricsCollector = new MetricsCollector(Duration.millis(METRIC_DURATION_MSECS), METRIC_NR_ENTRIES); metricsCollector.addValueNow(28.0f); metricsCollector.addValueNow(27.0f); metricsCollector.addValueNow(26.0f); verifyCollectorCountMinMaxSumAvgStdDev(metricsCollector, 3, 26.0f, 28.0f, 81.0f, 27.0f, 1.0f); }
@Test public void testStandardDeviation() { LOG.info("testStandardDeviation"); final MetricsCollector metricsCollector = new MetricsCollector(Duration.millis(METRIC_DURATION_MSECS), METRIC_NR_ENTRIES); verifyCollectorCountMinMaxSumAvgStdDev(metricsCollector, 0, Float.NaN, Float.NaN, 0.0f, Float.NaN, Float.NaN); metricsCollector.addValueNow(2); verifyCollectorCountMinMaxSumAvgStdDev(metricsCollector, 1, 2, 2, 2, 2, 0.0f); metricsCollector.addValueNow(4); metricsCollector.addValueNow(6); metricsCollector.addValueNow(7); verifyCollectorCountMinMaxSumAvgStdDev(metricsCollector, 4, 2.0f, 7.0f, 19.0f, 4.75f, 2.217355783f); }