Search in sources :

Example 1 with Quantile

use of org.apache.hadoop.metrics2.util.Quantile in project hadoop by apache.

the class TestMutableMetrics method testMutableQuantilesRollover.

/**
   * Test that {@link MutableQuantiles} rolls the window over at the specified
   * interval.
   */
@Test(timeout = 30000)
public void testMutableQuantilesRollover() throws Exception {
    MetricsRecordBuilder mb = mockMetricsRecordBuilder();
    MetricsRegistry registry = new MetricsRegistry("test");
    // Use a 5s rollover period
    MutableQuantiles quantiles = registry.newQuantiles("foo", "stat", "Ops", "Latency", 5);
    Quantile[] quants = MutableQuantiles.quantiles;
    String name = "Foo%dthPercentileLatency";
    String desc = "%d percentile latency with 5 second interval for stat";
    // Push values for three intervals
    long start = System.nanoTime() / 1000000;
    for (int i = 1; i <= 3; i++) {
        // Insert the values
        for (long j = 1; j <= 1000; j++) {
            quantiles.add(i);
        }
        // Sleep until 1s after the next 5s interval, to let the metrics
        // roll over
        long sleep = (start + (5000 * i) + 1000) - (System.nanoTime() / 1000000);
        Thread.sleep(sleep);
        // Verify that the window reset, check it has the values we pushed in
        registry.snapshot(mb, false);
        for (Quantile q : quants) {
            int percentile = (int) (100 * q.quantile);
            String n = String.format(name, percentile);
            String d = String.format(desc, percentile);
            verify(mb).addGauge(info(n, d), (long) i);
        }
    }
    // Verify the metrics were added the right number of times
    verify(mb, times(3)).addGauge(info("FooNumOps", "Number of ops for stat with 5s interval"), (long) 1000);
    for (Quantile q : quants) {
        int percentile = (int) (100 * q.quantile);
        String n = String.format(name, percentile);
        String d = String.format(desc, percentile);
        verify(mb, times(3)).addGauge(eq(info(n, d)), anyLong());
    }
}
Also used : Quantile(org.apache.hadoop.metrics2.util.Quantile) MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder) Test(org.junit.Test)

Example 2 with Quantile

use of org.apache.hadoop.metrics2.util.Quantile in project hadoop by apache.

the class MetricsAsserts method assertQuantileGauges.

/**
   * Asserts that the NumOps and quantiles for a metric have been changed at
   * some point to a non-zero value.
   * 
   * @param prefix of the metric
   * @param rb MetricsRecordBuilder with the metric
   */
public static void assertQuantileGauges(String prefix, MetricsRecordBuilder rb) {
    verify(rb).addGauge(eqName(info(prefix + "NumOps", "")), geq(0l));
    for (Quantile q : MutableQuantiles.quantiles) {
        String nameTemplate = prefix + "%dthPercentileLatency";
        int percentile = (int) (100 * q.quantile);
        verify(rb).addGauge(eqName(info(String.format(nameTemplate, percentile), "")), geq(0l));
    }
}
Also used : Quantile(org.apache.hadoop.metrics2.util.Quantile)

Example 3 with Quantile

use of org.apache.hadoop.metrics2.util.Quantile in project hadoop by apache.

the class TestMutableMetrics method testMutableQuantilesError.

/**
   * Ensure that quantile estimates from {@link MutableQuantiles} are within
   * specified error bounds.
   */
@Test(timeout = 30000)
public void testMutableQuantilesError() throws Exception {
    MetricsRecordBuilder mb = mockMetricsRecordBuilder();
    MetricsRegistry registry = new MetricsRegistry("test");
    // Use a 5s rollover period
    MutableQuantiles quantiles = registry.newQuantiles("foo", "stat", "Ops", "Latency", 5);
    // Push some values in and wait for it to publish
    long start = System.nanoTime() / 1000000;
    for (long i = 1; i <= 1000; i++) {
        quantiles.add(i);
        quantiles.add(1001 - i);
    }
    long end = System.nanoTime() / 1000000;
    Thread.sleep(6000 - (end - start));
    registry.snapshot(mb, false);
    // Print out the snapshot
    Map<Quantile, Long> previousSnapshot = quantiles.previousSnapshot;
    for (Entry<Quantile, Long> item : previousSnapshot.entrySet()) {
        System.out.println(String.format("Quantile %.2f has value %d", item.getKey().quantile, item.getValue()));
    }
    // Verify the results are within our requirements
    verify(mb).addGauge(info("FooNumOps", "Number of ops for stat with 5s interval"), (long) 2000);
    Quantile[] quants = MutableQuantiles.quantiles;
    String name = "Foo%dthPercentileLatency";
    String desc = "%d percentile latency with 5 second interval for stat";
    for (Quantile q : quants) {
        int percentile = (int) (100 * q.quantile);
        int error = (int) (1000 * q.error);
        String n = String.format(name, percentile);
        String d = String.format(desc, percentile);
        long expected = (long) (q.quantile * 1000);
        verify(mb).addGauge(eq(info(n, d)), leq(expected + error));
        verify(mb).addGauge(eq(info(n, d)), geq(expected - error));
    }
}
Also used : Matchers.anyLong(org.mockito.Matchers.anyLong) Quantile(org.apache.hadoop.metrics2.util.Quantile) MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder) Test(org.junit.Test)

Aggregations

Quantile (org.apache.hadoop.metrics2.util.Quantile)3 MetricsRecordBuilder (org.apache.hadoop.metrics2.MetricsRecordBuilder)2 Test (org.junit.Test)2 Matchers.anyLong (org.mockito.Matchers.anyLong)1