Search in sources :

Example 1 with MetricsRegistry

use of org.apache.hadoop.metrics2.lib.MetricsRegistry 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 MetricsRegistry

use of org.apache.hadoop.metrics2.lib.MetricsRegistry in project hadoop by apache.

the class TestFSNamesystemLock method testDetailedHoldMetrics.

@Test
public void testDetailedHoldMetrics() throws Exception {
    Configuration conf = new Configuration();
    conf.setBoolean(DFSConfigKeys.DFS_NAMENODE_LOCK_DETAILED_METRICS_KEY, true);
    FakeTimer timer = new FakeTimer();
    MetricsRegistry registry = new MetricsRegistry("Test");
    MutableRatesWithAggregation rates = registry.newRatesWithAggregation("Test");
    FSNamesystemLock fsLock = new FSNamesystemLock(conf, rates, timer);
    fsLock.readLock();
    timer.advance(1);
    fsLock.readUnlock("foo");
    fsLock.readLock();
    timer.advance(2);
    fsLock.readUnlock("foo");
    fsLock.readLock();
    timer.advance(1);
    fsLock.readLock();
    timer.advance(1);
    fsLock.readUnlock("bar");
    fsLock.readUnlock("bar");
    fsLock.writeLock();
    timer.advance(1);
    fsLock.writeUnlock("baz");
    MetricsRecordBuilder rb = MetricsAsserts.mockMetricsRecordBuilder();
    rates.snapshot(rb, true);
    assertGauge("FSNReadLockFooAvgTime", 1.5, rb);
    assertCounter("FSNReadLockFooNumOps", 2L, rb);
    assertGauge("FSNReadLockBarAvgTime", 2.0, rb);
    assertCounter("FSNReadLockBarNumOps", 1L, rb);
    assertGauge("FSNWriteLockBazAvgTime", 1.0, rb);
    assertCounter("FSNWriteLockBazNumOps", 1L, rb);
}
Also used : MutableRatesWithAggregation(org.apache.hadoop.metrics2.lib.MutableRatesWithAggregation) MetricsRegistry(org.apache.hadoop.metrics2.lib.MetricsRegistry) Configuration(org.apache.hadoop.conf.Configuration) FakeTimer(org.apache.hadoop.util.FakeTimer) MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder) Test(org.junit.Test)

Example 3 with MetricsRegistry

use of org.apache.hadoop.metrics2.lib.MetricsRegistry in project hadoop by apache.

the class TestMutableMetrics method testMutableQuantilesEmptyRollover.

/**
   * Test that {@link MutableQuantiles} rolls over correctly even if no items
   * have been added to the window
   */
@Test(timeout = 30000)
public void testMutableQuantilesEmptyRollover() throws Exception {
    MetricsRecordBuilder mb = mockMetricsRecordBuilder();
    MetricsRegistry registry = new MetricsRegistry("test");
    // Use a 5s rollover period
    MutableQuantiles quantiles = registry.newQuantiles("foo", "stat", "Ops", "Latency", 5);
    // Check it initially
    quantiles.snapshot(mb, true);
    verify(mb).addGauge(info("FooNumOps", "Number of ops for stat with 5s interval"), (long) 0);
    Thread.sleep(6000);
    quantiles.snapshot(mb, false);
    verify(mb, times(2)).addGauge(info("FooNumOps", "Number of ops for stat with 5s interval"), (long) 0);
}
Also used : MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder) Test(org.junit.Test)

Example 4 with MetricsRegistry

use of org.apache.hadoop.metrics2.lib.MetricsRegistry in project hadoop by apache.

the class TestMutableMetrics method testSnapshot.

/**
   * Test the snapshot method
   */
@Test
public void testSnapshot() {
    MetricsRecordBuilder mb = mockMetricsRecordBuilder();
    MetricsRegistry registry = new MetricsRegistry("test");
    registry.newCounter("c1", "int counter", 1);
    registry.newCounter("c2", "long counter", 2L);
    registry.newGauge("g1", "int gauge", 3);
    registry.newGauge("g2", "long gauge", 4L);
    registry.newStat("s1", "stat", "Ops", "Time", true).add(0);
    registry.newRate("s2", "stat", false).add(0);
    registry.snapshot(mb, true);
    MutableStat s2 = (MutableStat) registry.get("s2");
    // should get the same back.
    s2.snapshot(mb, true);
    s2.add(1);
    // should get new interval values back
    s2.snapshot(mb, true);
    verify(mb).addCounter(info("c1", "int counter"), 1);
    verify(mb).addCounter(info("c2", "long counter"), 2L);
    verify(mb).addGauge(info("g1", "int gauge"), 3);
    verify(mb).addGauge(info("g2", "long gauge"), 4L);
    verify(mb).addCounter(info("S1NumOps", "Number of ops for stat"), 1L);
    verify(mb).addGauge(eq(info("S1AvgTime", "Average time for stat")), eq(0.0, EPSILON));
    verify(mb).addGauge(eq(info("S1StdevTime", "Standard deviation of time for stat")), eq(0.0, EPSILON));
    verify(mb).addGauge(eq(info("S1IMinTime", "Interval min time for stat")), eq(0.0, EPSILON));
    verify(mb).addGauge(eq(info("S1IMaxTime", "Interval max time for stat")), eq(0.0, EPSILON));
    verify(mb).addGauge(eq(info("S1MinTime", "Min time for stat")), eq(0.0, EPSILON));
    verify(mb).addGauge(eq(info("S1MaxTime", "Max time for stat")), eq(0.0, EPSILON));
    verify(mb).addGauge(eq(info("S1INumOps", "Interval number of ops for stat")), eq(1L));
    verify(mb, times(2)).addCounter(info("S2NumOps", "Number of ops for stat"), 1L);
    verify(mb, times(2)).addGauge(eq(info("S2AvgTime", "Average time for stat")), eq(0.0, EPSILON));
    verify(mb).addCounter(info("S2NumOps", "Number of ops for stat"), 2L);
    verify(mb).addGauge(eq(info("S2AvgTime", "Average time for stat")), eq(1.0, EPSILON));
    // Add one more sample to s1 and verify that total number of ops
    // has increased to 2, but interval number is 1 for both intervals.
    MutableStat s1 = (MutableStat) registry.get("s1");
    s1.add(0);
    registry.snapshot(mb, true);
    verify(mb).addCounter(info("S1NumOps", "Number of ops for stat"), 2L);
    verify(mb, times(2)).addGauge(eq(info("S1INumOps", "Interval number of ops for stat")), eq(1L));
}
Also used : MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder) Test(org.junit.Test)

Example 5 with MetricsRegistry

use of org.apache.hadoop.metrics2.lib.MetricsRegistry in project hadoop by apache.

the class MetricsSourceBuilder method initRegistry.

private MetricsRegistry initRegistry(Object source) {
    Class<?> cls = source.getClass();
    MetricsRegistry r = null;
    // Get the registry if it already exists.
    for (Field field : ReflectionUtils.getDeclaredFieldsIncludingInherited(cls)) {
        if (field.getType() != MetricsRegistry.class)
            continue;
        try {
            field.setAccessible(true);
            r = (MetricsRegistry) field.get(source);
            hasRegistry = r != null;
            break;
        } catch (Exception e) {
            LOG.warn("Error accessing field " + field, e);
            continue;
        }
    }
    // Create a new registry according to annotation
    for (Annotation annotation : cls.getAnnotations()) {
        if (annotation instanceof Metrics) {
            Metrics ma = (Metrics) annotation;
            info = factory.getInfo(cls, ma);
            if (r == null) {
                r = new MetricsRegistry(info);
            }
            r.setContext(ma.context());
        }
    }
    if (r == null)
        return new MetricsRegistry(cls.getSimpleName());
    return r;
}
Also used : Field(java.lang.reflect.Field) Metrics(org.apache.hadoop.metrics2.annotation.Metrics) MetricsException(org.apache.hadoop.metrics2.MetricsException) Annotation(java.lang.annotation.Annotation)

Aggregations

Test (org.junit.Test)9 MetricsRecordBuilder (org.apache.hadoop.metrics2.MetricsRecordBuilder)8 MetricsRegistry (org.apache.hadoop.metrics2.lib.MetricsRegistry)3 MetricsException (org.apache.hadoop.metrics2.MetricsException)2 MetricsInfo (org.apache.hadoop.metrics2.MetricsInfo)2 Quantile (org.apache.hadoop.metrics2.util.Quantile)2 Annotation (java.lang.annotation.Annotation)1 Field (java.lang.reflect.Field)1 Configuration (org.apache.hadoop.conf.Configuration)1 AbstractMetric (org.apache.hadoop.metrics2.AbstractMetric)1 MetricsSystem (org.apache.hadoop.metrics2.MetricsSystem)1 MetricsVisitor (org.apache.hadoop.metrics2.MetricsVisitor)1 Metrics (org.apache.hadoop.metrics2.annotation.Metrics)1 DefaultMetricsSystem (org.apache.hadoop.metrics2.lib.DefaultMetricsSystem)1 MutableRatesWithAggregation (org.apache.hadoop.metrics2.lib.MutableRatesWithAggregation)1 FakeTimer (org.apache.hadoop.util.FakeTimer)1 Matchers.anyLong (org.mockito.Matchers.anyLong)1