Search in sources :

Example 31 with MetricsRecordBuilder

use of org.apache.hadoop.metrics2.MetricsRecordBuilder 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 32 with MetricsRecordBuilder

use of org.apache.hadoop.metrics2.MetricsRecordBuilder in project hadoop by apache.

the class TestRollingAverages method testRollingAveragesRollover.

/**
   * Tests the case:
   * <p>
   * 5s interval and 2 sliding windows
   * </p>
   * <p>
   * sample stream: 1000 times 1, 2, and 3, respectively, e.g. [1, 1...1], [2,
   * 2...2] and [3, 3...3]
   * </p>
   */
@Test(timeout = 30000)
public void testRollingAveragesRollover() throws Exception {
    final MetricsRecordBuilder rb = mockMetricsRecordBuilder();
    final String name = "foo2";
    // 5s roll over interval
    final int windowSizeMs = 5000;
    final int numWindows = 2;
    final int numOpsPerIteration = 1000;
    try (RollingAverages rollingAverages = new RollingAverages(windowSizeMs, numWindows)) {
        /* Push values for three intervals */
        final long start = Time.monotonicNow();
        for (int i = 1; i <= 3; i++) {
            /* insert value */
            for (long j = 1; j <= numOpsPerIteration; j++) {
                rollingAverages.add(name, i);
            }
            /**
         * Sleep until 1s after the next windowSize seconds interval, to let the
         * metrics roll over
         */
            final long sleep = (start + (windowSizeMs * i) + 1000) - Time.monotonicNow();
            Thread.sleep(sleep);
            /* Verify that the window reset, check it has the values we pushed in */
            rollingAverages.snapshot(rb, false);
            /*
         * #1 window with a series of 1 1000
         * times, e.g. [1, 1...1], similarly, #2 window, e.g. [2, 2...2],
         * #3 window, e.g. [3, 3...3]
         */
            final double rollingSum = numOpsPerIteration * (i > 1 ? (i - 1) : 0) + numOpsPerIteration * i;
            /* one empty window or all 2 windows full */
            final long rollingTotal = i > 1 ? 2 * numOpsPerIteration : numOpsPerIteration;
            verify(rb).addGauge(info("[Foo2]RollingAvgTime", "Rolling average time for foo2"), rollingSum / rollingTotal);
            /* Verify the metrics were added the right number of times */
            verify(rb, times(i)).addGauge(eq(info("[Foo2]RollingAvgTime", "Rolling average time for foo2")), anyDouble());
        }
    }
}
Also used : MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder) MetricsAsserts.mockMetricsRecordBuilder(org.apache.hadoop.test.MetricsAsserts.mockMetricsRecordBuilder) Test(org.junit.Test)

Example 33 with MetricsRecordBuilder

use of org.apache.hadoop.metrics2.MetricsRecordBuilder in project hadoop by apache.

the class TestMetricsAnnotations method testHybrid.

@Test
public void testHybrid() {
    HybridMetrics metrics = new HybridMetrics();
    MetricsSource source = MetricsAnnotations.makeSource(metrics);
    assertSame(metrics, source);
    metrics.C0.incr();
    MetricsRecordBuilder rb = getMetrics(source);
    MetricsCollector collector = rb.parent();
    verify(collector).addRecord("foo");
    verify(collector).addRecord("bar");
    verify(collector).addRecord(info("HybridMetrics", "HybridMetrics"));
    verify(rb).setContext("foocontext");
    verify(rb).addCounter(info("C1", "C1 desc"), 1);
    verify(rb).setContext("barcontext");
    verify(rb).addGauge(info("G1", "G1 desc"), 1);
    verify(rb).add(tag(MsInfo.Context, "hybrid"));
    verify(rb).addCounter(info("C0", "C0 desc"), 1);
    verify(rb).addGauge(info("G0", "G0"), 0);
}
Also used : MetricsCollector(org.apache.hadoop.metrics2.MetricsCollector) MetricsSource(org.apache.hadoop.metrics2.MetricsSource) MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder) Test(org.junit.Test)

Example 34 with MetricsRecordBuilder

use of org.apache.hadoop.metrics2.MetricsRecordBuilder in project hadoop by apache.

the class TestMetricsAnnotations method testFields.

@Test
public void testFields() {
    MyMetrics metrics = new MyMetrics();
    MetricsSource source = MetricsAnnotations.makeSource(metrics);
    metrics.c1.incr();
    metrics.c2.incr();
    metrics.g1.incr();
    metrics.g2.incr();
    metrics.g3.incr();
    metrics.r1.add(1);
    metrics.s1.add(1);
    metrics.rs1.add("rs1", 1);
    MetricsRecordBuilder rb = getMetrics(source);
    verify(rb).addCounter(info("C1", "C1"), 1);
    verify(rb).addCounter(info("Counter2", "Counter2 desc"), 1L);
    verify(rb).addGauge(info("G1", "G1"), 1);
    verify(rb).addGauge(info("G2", "G2"), 1);
    verify(rb).addGauge(info("G3", "g3 desc"), 1L);
    verify(rb).addCounter(info("R1NumOps", "Number of ops for r1"), 1L);
    verify(rb).addGauge(info("R1AvgTime", "Average time for r1"), 1.0);
    verify(rb).addCounter(info("S1NumOps", "Number of ops for s1"), 1L);
    verify(rb).addGauge(info("S1AvgTime", "Average time for s1"), 1.0);
    verify(rb).addCounter(info("Rs1NumOps", "Number of ops for rs1"), 1L);
    verify(rb).addGauge(info("Rs1AvgTime", "Average time for rs1"), 1.0);
}
Also used : MetricsSource(org.apache.hadoop.metrics2.MetricsSource) MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder) Test(org.junit.Test)

Example 35 with MetricsRecordBuilder

use of org.apache.hadoop.metrics2.MetricsRecordBuilder in project hadoop by apache.

the class TestMutableMetrics method snapshotMutableRatesWithAggregation.

private static void snapshotMutableRatesWithAggregation(MutableRatesWithAggregation rates, long[] opCount, double[] opTotalTime) {
    MetricsRecordBuilder rb = mockMetricsRecordBuilder();
    rates.snapshot(rb, true);
    for (int i = 0; i < opCount.length; i++) {
        long prevOpCount = opCount[i];
        long newOpCount = getLongCounter("Metric" + i + "NumOps", rb);
        opCount[i] = newOpCount;
        double avgTime = getDoubleGauge("Metric" + i + "AvgTime", rb);
        opTotalTime[i] += avgTime * (newOpCount - prevOpCount);
    }
}
Also used : MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder)

Aggregations

MetricsRecordBuilder (org.apache.hadoop.metrics2.MetricsRecordBuilder)99 Test (org.junit.Test)47 Path (org.apache.hadoop.fs.Path)20 Configuration (org.apache.hadoop.conf.Configuration)14 DistributedFileSystem (org.apache.hadoop.hdfs.DistributedFileSystem)12 HdfsConfiguration (org.apache.hadoop.hdfs.HdfsConfiguration)11 MiniDFSCluster (org.apache.hadoop.hdfs.MiniDFSCluster)11 FileSystem (org.apache.hadoop.fs.FileSystem)8 MetricsInfo (org.apache.hadoop.metrics2.MetricsInfo)7 IOException (java.io.IOException)6 MetricsCollector (org.apache.hadoop.metrics2.MetricsCollector)6 MetricsSource (org.apache.hadoop.metrics2.MetricsSource)5 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)4 Quantile (org.apache.hadoop.metrics2.util.Quantile)4 ServiceException (com.google.protobuf.ServiceException)3 InterruptedIOException (java.io.InterruptedIOException)2 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)2 Map (java.util.Map)2 CacheDirectiveInfo (org.apache.hadoop.hdfs.protocol.CacheDirectiveInfo)2 CachePoolInfo (org.apache.hadoop.hdfs.protocol.CachePoolInfo)2