Search in sources :

Example 1 with Reservoir

use of com.codahale.metrics.Reservoir in project riposte by Nike-Inc.

the class SignalFxEndpointMetricsHandlerTest method RollingWindowTimerBuilder_newMetric_creates_new_timer_with_SlidingTimeWindowReservoir_with_expected_values.

@DataProvider(value = { "42     |   DAYS", "123    |   SECONDS", "999    |   MILLISECONDS", "3      |   HOURS" }, splitBy = "\\|")
@Test
public void RollingWindowTimerBuilder_newMetric_creates_new_timer_with_SlidingTimeWindowReservoir_with_expected_values(long amount, TimeUnit timeUnit) {
    // given
    RollingWindowTimerBuilder rwtb = new RollingWindowTimerBuilder(amount, timeUnit);
    // when
    Timer timer = rwtb.newMetric();
    // then
    Histogram histogram = (Histogram) getInternalState(timer, "histogram");
    Reservoir reservoir = (Reservoir) getInternalState(histogram, "reservoir");
    assertThat(reservoir).isInstanceOf(SlidingTimeWindowReservoir.class);
    // The expected value here comes from logic in the SlidingTimeWindowReservoir constructor.
    assertThat(getInternalState(reservoir, "window")).isEqualTo(timeUnit.toNanos(amount) * 256);
}
Also used : Histogram(com.codahale.metrics.Histogram) Timer(com.codahale.metrics.Timer) Reservoir(com.codahale.metrics.Reservoir) SlidingTimeWindowReservoir(com.codahale.metrics.SlidingTimeWindowReservoir) RollingWindowTimerBuilder(com.nike.riposte.metrics.codahale.impl.SignalFxEndpointMetricsHandler.RollingWindowTimerBuilder) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 2 with Reservoir

use of com.codahale.metrics.Reservoir in project lucene-solr by apache.

the class MetricSuppliers method getReservoir.

private static final Reservoir getReservoir(SolrResourceLoader loader, PluginInfo info) {
    if (info == null) {
        return new ExponentiallyDecayingReservoir();
    }
    Clock clk = getClock(info, CLOCK);
    String clazz = ExponentiallyDecayingReservoir.class.getName();
    int size = -1;
    double alpha = -1;
    long window = -1;
    if (info.initArgs != null) {
        if (info.initArgs.get(RESERVOIR) != null) {
            String val = String.valueOf(info.initArgs.get(RESERVOIR)).trim();
            if (!val.isEmpty()) {
                clazz = val;
            }
        }
        Number n = (Number) info.initArgs.get(RESERVOIR_SIZE);
        if (n != null) {
            size = n.intValue();
        }
        n = (Number) info.initArgs.get(RESERVOIR_EDR_ALPHA);
        if (n != null) {
            alpha = n.doubleValue();
        }
        n = (Number) info.initArgs.get(RESERVOIR_WINDOW);
        if (n != null) {
            window = n.longValue();
        }
    }
    if (size <= 0) {
        size = DEFAULT_SIZE;
    }
    if (alpha <= 0) {
        alpha = DEFAULT_ALPHA;
    }
    // special case for core implementations
    if (clazz.equals(EDR_CLAZZ)) {
        return new ExponentiallyDecayingReservoir(size, alpha, clk);
    } else if (clazz.equals(UNI_CLAZZ)) {
        return new UniformReservoir(size);
    } else if (clazz.equals(STW_CLAZZ)) {
        if (window <= 0) {
            // 5 minutes, comparable to EDR
            window = DEFAULT_WINDOW;
        }
        return new SlidingTimeWindowReservoir(window, TimeUnit.SECONDS);
    } else if (clazz.equals(SW_CLAZZ)) {
        return new SlidingWindowReservoir(size);
    } else {
        // custom reservoir
        Reservoir reservoir;
        try {
            reservoir = loader.newInstance(clazz, Reservoir.class);
            if (reservoir instanceof PluginInfoInitialized) {
                ((PluginInfoInitialized) reservoir).init(info);
            } else {
                SolrPluginUtils.invokeSetters(reservoir, info.initArgs, true);
            }
            return reservoir;
        } catch (Exception e) {
            log.warn("Error initializing custom Reservoir implementation (will use default): " + info, e);
            return new ExponentiallyDecayingReservoir(size, alpha, clk);
        }
    }
}
Also used : Reservoir(com.codahale.metrics.Reservoir) ExponentiallyDecayingReservoir(com.codahale.metrics.ExponentiallyDecayingReservoir) UniformReservoir(com.codahale.metrics.UniformReservoir) SlidingWindowReservoir(com.codahale.metrics.SlidingWindowReservoir) SlidingTimeWindowReservoir(com.codahale.metrics.SlidingTimeWindowReservoir) Clock(com.codahale.metrics.Clock) SlidingTimeWindowReservoir(com.codahale.metrics.SlidingTimeWindowReservoir) ExponentiallyDecayingReservoir(com.codahale.metrics.ExponentiallyDecayingReservoir) SlidingWindowReservoir(com.codahale.metrics.SlidingWindowReservoir) UniformReservoir(com.codahale.metrics.UniformReservoir) PluginInfoInitialized(org.apache.solr.util.plugin.PluginInfoInitialized)

Example 3 with Reservoir

use of com.codahale.metrics.Reservoir in project torodb by torodb.

the class ToroMetricRegistry method histogram.

/**
   *
   * @param name
   * @param resetOnSnapshot This is usually true if you're using snapshots as a means of defining
   *                        the window in which you want to calculate, say, the 99.9th percentile
   * @return
   */
public Histogram histogram(MetricName name, boolean resetOnSnapshot) {
    Reservoir reservoir;
    if (resetOnSnapshot) {
        reservoir = new HdrHistogramResetOnSnapshotReservoir();
    } else {
        reservoir = new HdrHistogramReservoir();
    }
    Histogram histogram = register(name, new Histogram(reservoir));
    return histogram;
}
Also used : HdrHistogramReservoir(org.mpierce.metrics.reservoir.hdrhistogram.HdrHistogramReservoir) Histogram(com.codahale.metrics.Histogram) HdrHistogramResetOnSnapshotReservoir(org.mpierce.metrics.reservoir.hdrhistogram.HdrHistogramResetOnSnapshotReservoir) HdrHistogramReservoir(org.mpierce.metrics.reservoir.hdrhistogram.HdrHistogramReservoir) HdrHistogramResetOnSnapshotReservoir(org.mpierce.metrics.reservoir.hdrhistogram.HdrHistogramResetOnSnapshotReservoir) Reservoir(com.codahale.metrics.Reservoir)

Example 4 with Reservoir

use of com.codahale.metrics.Reservoir in project riposte by Nike-Inc.

the class SignalFxEndpointMetricsHandlerTest method RollingWindowHistogramBuilder_newMetric_creates_new_histogram_with_SlidingTimeWindowReservoir_with_expected_values.

@DataProvider(value = { "42     |   DAYS", "123    |   SECONDS", "999    |   MILLISECONDS", "3      |   HOURS" }, splitBy = "\\|")
@Test
public void RollingWindowHistogramBuilder_newMetric_creates_new_histogram_with_SlidingTimeWindowReservoir_with_expected_values(long amount, TimeUnit timeUnit) {
    // given
    RollingWindowHistogramBuilder rwhb = new RollingWindowHistogramBuilder(amount, timeUnit);
    // when
    Histogram histogram = rwhb.newMetric();
    // then
    Reservoir reservoir = (Reservoir) getInternalState(histogram, "reservoir");
    assertThat(reservoir).isInstanceOf(SlidingTimeWindowReservoir.class);
    // The expected value here comes from logic in the SlidingTimeWindowReservoir constructor.
    assertThat(getInternalState(reservoir, "window")).isEqualTo(timeUnit.toNanos(amount) * 256);
}
Also used : Histogram(com.codahale.metrics.Histogram) RollingWindowHistogramBuilder(com.nike.riposte.metrics.codahale.impl.SignalFxEndpointMetricsHandler.RollingWindowHistogramBuilder) Reservoir(com.codahale.metrics.Reservoir) SlidingTimeWindowReservoir(com.codahale.metrics.SlidingTimeWindowReservoir) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 5 with Reservoir

use of com.codahale.metrics.Reservoir in project ambry by linkedin.

the class CachedHistogramTest method testCache.

/**
 * Test caching behavior.
 */
@Test
public void testCache() {
    AtomicInteger snapshotCalls = new AtomicInteger(0);
    MockClock clock = new MockClock();
    Reservoir reservoir = new ExponentiallyDecayingReservoir();
    CachedHistogram histogram = new CachedHistogram(clock, reservoir, TimeUnit.SECONDS.toMillis(1), 0.50) {

        @Override
        public Snapshot getSnapshot() {
            // count number of calls to test caching
            snapshotCalls.getAndIncrement();
            return super.getSnapshot();
        }
    };
    long value = 2;
    double epsilon = 0.01;
    histogram.update(value);
    // getSnapshot should be called the first time
    assertEquals(value, histogram.getCachedValue(), epsilon);
    assertEquals(1, snapshotCalls.get());
    // the cached value should be used and getSnapshot should not be called.
    assertEquals(value, histogram.getCachedValue(), epsilon);
    assertEquals(1, snapshotCalls.get());
    // after progressing time, the cached value should expire and getSnapshot should be called
    clock.tick(1);
    assertEquals(value, histogram.getCachedValue(), epsilon);
    assertEquals(2, snapshotCalls.get());
}
Also used : ExponentiallyDecayingReservoir(com.codahale.metrics.ExponentiallyDecayingReservoir) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExponentiallyDecayingReservoir(com.codahale.metrics.ExponentiallyDecayingReservoir) Reservoir(com.codahale.metrics.Reservoir) Test(org.junit.Test)

Aggregations

Reservoir (com.codahale.metrics.Reservoir)13 Test (org.junit.Test)7 Histogram (com.codahale.metrics.Histogram)6 ExponentiallyDecayingReservoir (com.codahale.metrics.ExponentiallyDecayingReservoir)5 SlidingTimeWindowReservoir (com.codahale.metrics.SlidingTimeWindowReservoir)5 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)4 Timer (com.codahale.metrics.Timer)3 UniformReservoir (com.codahale.metrics.UniformReservoir)3 Clock (com.codahale.metrics.Clock)2 SlidingTimeWindowArrayReservoir (com.codahale.metrics.SlidingTimeWindowArrayReservoir)2 RollingWindowHistogramBuilder (com.nike.riposte.metrics.codahale.impl.SignalFxEndpointMetricsHandler.RollingWindowHistogramBuilder)2 RollingWindowTimerBuilder (com.nike.riposte.metrics.codahale.impl.SignalFxEndpointMetricsHandler.RollingWindowTimerBuilder)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NodeConfig (org.apache.solr.core.NodeConfig)2 HdrHistogramReservoir (org.mpierce.metrics.reservoir.hdrhistogram.HdrHistogramReservoir)2 HdrHistogramResetOnSnapshotReservoir (org.mpierce.metrics.reservoir.hdrhistogram.HdrHistogramResetOnSnapshotReservoir)2 Metric (com.codahale.metrics.Metric)1 SlidingWindowReservoir (com.codahale.metrics.SlidingWindowReservoir)1 CachedHistogram (com.github.ambry.utils.CachedHistogram)1 PluginInfoInitialized (org.apache.solr.util.plugin.PluginInfoInitialized)1