Search in sources :

Example 76 with Counter

use of org.apache.flink.metrics.Counter in project flink by apache.

the class MetricListenerTest method testRegisterMetrics.

@Test
public void testRegisterMetrics() {
    MetricListener metricListener = new MetricListener();
    final MetricGroup metricGroup = metricListener.getMetricGroup();
    // Counter
    final Counter counter = metricGroup.counter(COUNTER_NAME);
    counter.inc(15213);
    final Optional<Counter> registeredCounter = metricListener.getCounter(COUNTER_NAME);
    assertTrue(registeredCounter.isPresent());
    assertEquals(15213L, registeredCounter.get().getCount());
    // Gauge
    metricGroup.gauge(GAUGE_NAME, () -> 15213);
    final Optional<Gauge<Integer>> registeredGauge = metricListener.getGauge(GAUGE_NAME);
    assertTrue(registeredGauge.isPresent());
    assertEquals(Integer.valueOf(15213), registeredGauge.get().getValue());
    // Meter
    metricGroup.meter(METER_NAME, new Meter() {

        @Override
        public void markEvent() {
        }

        @Override
        public void markEvent(long n) {
        }

        @Override
        public double getRate() {
            return 15213.0;
        }

        @Override
        public long getCount() {
            return 18213L;
        }
    });
    final Optional<Meter> registeredMeter = metricListener.getMeter(METER_NAME);
    assertTrue(registeredMeter.isPresent());
    assertEquals(15213.0, registeredMeter.get().getRate(), 0.1);
    assertEquals(18213L, registeredMeter.get().getCount());
    // Histogram
    metricGroup.histogram(HISTOGRAM_NAME, new Histogram() {

        @Override
        public void update(long value) {
        }

        @Override
        public long getCount() {
            return 15213L;
        }

        @Override
        public HistogramStatistics getStatistics() {
            return null;
        }
    });
    final Optional<Histogram> registeredHistogram = metricListener.getHistogram(HISTOGRAM_NAME);
    assertTrue(registeredHistogram.isPresent());
    assertEquals(15213L, registeredHistogram.get().getCount());
}
Also used : Histogram(org.apache.flink.metrics.Histogram) Meter(org.apache.flink.metrics.Meter) MetricGroup(org.apache.flink.metrics.MetricGroup) MetricListener(org.apache.flink.metrics.testutils.MetricListener) Gauge(org.apache.flink.metrics.Gauge) Counter(org.apache.flink.metrics.Counter) HistogramStatistics(org.apache.flink.metrics.HistogramStatistics) Test(org.junit.Test)

Example 77 with Counter

use of org.apache.flink.metrics.Counter in project flink by apache.

the class MetricListenerTest method testRegisterMetricGroup.

@Test
public void testRegisterMetricGroup() {
    MetricListener metricListener = new MetricListener();
    final MetricGroup rootGroup = metricListener.getMetricGroup();
    final MetricGroup groupA1 = rootGroup.addGroup(GROUP_A).addGroup(GROUP_A_1);
    final MetricGroup groupB = rootGroup.addGroup(GROUP_B);
    final MetricGroup groupB1 = groupB.addGroup(GROUP_B_1);
    final MetricGroup groupB2 = groupB.addGroup(GROUP_B_2);
    groupA1.counter(COUNTER_NAME).inc(18213L);
    groupB1.gauge(GAUGE_NAME, () -> 15213L);
    groupB2.counter(COUNTER_NAME).inc(15513L);
    // groupA.groupA_1.testCounter
    final Optional<Counter> counterA = metricListener.getCounter(GROUP_A, GROUP_A_1, COUNTER_NAME);
    assertTrue(counterA.isPresent());
    assertEquals(18213L, counterA.get().getCount());
    // groupB.groupB_1.testGauge
    final Optional<Gauge<Long>> gauge = metricListener.getGauge(GROUP_B, GROUP_B_1, GAUGE_NAME);
    assertTrue(gauge.isPresent());
    assertEquals(15213L, (long) gauge.get().getValue());
    // groupB.groupB_2.testCounter
    final Optional<Counter> counterB = metricListener.getCounter(GROUP_B, GROUP_B_2, COUNTER_NAME);
    assertTrue(counterB.isPresent());
    assertEquals(15513L, counterB.get().getCount());
}
Also used : Counter(org.apache.flink.metrics.Counter) MetricGroup(org.apache.flink.metrics.MetricGroup) MetricListener(org.apache.flink.metrics.testutils.MetricListener) Gauge(org.apache.flink.metrics.Gauge) Test(org.junit.Test)

Example 78 with Counter

use of org.apache.flink.metrics.Counter in project flink by apache.

the class RowTimeRowsUnboundedPrecedingFunctionTest method testLateRecordMetrics.

@Test
public void testLateRecordMetrics() throws Exception {
    RowTimeRowsUnboundedPrecedingFunction<RowData> function = new RowTimeRowsUnboundedPrecedingFunction<>(1000, 2000, aggsHandleFunction, accTypes, inputFieldTypes, 2);
    KeyedProcessOperator<RowData, RowData, RowData> operator = new KeyedProcessOperator<>(function);
    OneInputStreamOperatorTestHarness<RowData, RowData> testHarness = createTestHarness(operator);
    testHarness.open();
    Counter counter = function.getCounter();
    // put some records
    testHarness.processElement(insertRecord("key", 1L, 100L));
    testHarness.processElement(insertRecord("key", 1L, 100L));
    testHarness.processElement(insertRecord("key", 1L, 500L));
    testHarness.processWatermark(new Watermark(500L));
    // late record
    testHarness.processElement(insertRecord("key", 1L, 400L));
    assertEquals(1L, counter.getCount());
}
Also used : RowData(org.apache.flink.table.data.RowData) Counter(org.apache.flink.metrics.Counter) KeyedProcessOperator(org.apache.flink.streaming.api.operators.KeyedProcessOperator) Watermark(org.apache.flink.streaming.api.watermark.Watermark) Test(org.junit.Test)

Aggregations

Counter (org.apache.flink.metrics.Counter)78 SimpleCounter (org.apache.flink.metrics.SimpleCounter)24 Test (org.junit.Test)22 CountingCollector (org.apache.flink.runtime.operators.util.metrics.CountingCollector)18 Test (org.junit.jupiter.api.Test)16 Histogram (org.apache.flink.metrics.Histogram)15 Meter (org.apache.flink.metrics.Meter)15 Gauge (org.apache.flink.metrics.Gauge)14 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)12 CountingMutableObjectIterator (org.apache.flink.runtime.operators.util.metrics.CountingMutableObjectIterator)10 MetricGroup (org.apache.flink.metrics.MetricGroup)7 TestHistogram (org.apache.flink.metrics.util.TestHistogram)7 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)6 TestMeter (org.apache.flink.metrics.util.TestMeter)5 TaskConfig (org.apache.flink.runtime.operators.util.TaskConfig)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 OperatorIOMetricGroup (org.apache.flink.metrics.groups.OperatorIOMetricGroup)4 MetricListener (org.apache.flink.metrics.testutils.MetricListener)4