Search in sources :

Example 1 with TestingHistogram

use of org.apache.flink.runtime.metrics.util.TestingHistogram in project flink by apache.

the class MetricDumpSerializerTest method testSerialization.

@Test
public void testSerialization() throws IOException {
    MetricDumpSerialization.MetricDumpSerializer serializer = new MetricDumpSerialization.MetricDumpSerializer();
    MetricDumpSerialization.MetricDumpDeserializer deserializer = new MetricDumpSerialization.MetricDumpDeserializer();
    Map<Counter, Tuple2<QueryScopeInfo, String>> counters = new HashMap<>();
    Map<Gauge<?>, Tuple2<QueryScopeInfo, String>> gauges = new HashMap<>();
    Map<Histogram, Tuple2<QueryScopeInfo, String>> histograms = new HashMap<>();
    Map<Meter, Tuple2<QueryScopeInfo, String>> meters = new HashMap<>();
    SimpleCounter c1 = new SimpleCounter();
    SimpleCounter c2 = new SimpleCounter();
    c1.inc(1);
    c2.inc(2);
    Gauge<Integer> g1 = new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            return 4;
        }
    };
    Histogram h1 = new TestingHistogram();
    Meter m1 = new Meter() {

        @Override
        public void markEvent() {
        }

        @Override
        public void markEvent(long n) {
        }

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

        @Override
        public long getCount() {
            return 10;
        }
    };
    counters.put(c1, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.JobManagerQueryScopeInfo("A"), "c1"));
    counters.put(c2, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.TaskManagerQueryScopeInfo("tmid", "B"), "c2"));
    meters.put(m1, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.JobQueryScopeInfo("jid", "C"), "c3"));
    gauges.put(g1, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.TaskQueryScopeInfo("jid", "vid", 2, "D"), "g1"));
    histograms.put(h1, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.OperatorQueryScopeInfo("jid", "vid", 2, "opname", "E"), "h1"));
    MetricDumpSerialization.MetricSerializationResult serialized = serializer.serialize(counters, gauges, histograms, meters);
    List<MetricDump> deserialized = deserializer.deserialize(serialized);
    // ===== Counters ==============================================================================================
    assertEquals(5, deserialized.size());
    for (MetricDump metric : deserialized) {
        switch(metric.getCategory()) {
            case METRIC_CATEGORY_COUNTER:
                MetricDump.CounterDump counterDump = (MetricDump.CounterDump) metric;
                switch((byte) counterDump.count) {
                    case 1:
                        assertTrue(counterDump.scopeInfo instanceof QueryScopeInfo.JobManagerQueryScopeInfo);
                        assertEquals("A", counterDump.scopeInfo.scope);
                        assertEquals("c1", counterDump.name);
                        counters.remove(c1);
                        break;
                    case 2:
                        assertTrue(counterDump.scopeInfo instanceof QueryScopeInfo.TaskManagerQueryScopeInfo);
                        assertEquals("B", counterDump.scopeInfo.scope);
                        assertEquals("c2", counterDump.name);
                        assertEquals("tmid", ((QueryScopeInfo.TaskManagerQueryScopeInfo) counterDump.scopeInfo).taskManagerID);
                        counters.remove(c2);
                        break;
                    default:
                        fail();
                }
                break;
            case METRIC_CATEGORY_GAUGE:
                MetricDump.GaugeDump gaugeDump = (MetricDump.GaugeDump) metric;
                assertEquals("4", gaugeDump.value);
                assertEquals("g1", gaugeDump.name);
                assertTrue(gaugeDump.scopeInfo instanceof QueryScopeInfo.TaskQueryScopeInfo);
                QueryScopeInfo.TaskQueryScopeInfo taskInfo = (QueryScopeInfo.TaskQueryScopeInfo) gaugeDump.scopeInfo;
                assertEquals("D", taskInfo.scope);
                assertEquals("jid", taskInfo.jobID);
                assertEquals("vid", taskInfo.vertexID);
                assertEquals(2, taskInfo.subtaskIndex);
                gauges.remove(g1);
                break;
            case METRIC_CATEGORY_HISTOGRAM:
                MetricDump.HistogramDump histogramDump = (MetricDump.HistogramDump) metric;
                assertEquals("h1", histogramDump.name);
                assertEquals(0.5, histogramDump.median, 0.1);
                assertEquals(0.75, histogramDump.p75, 0.1);
                assertEquals(0.90, histogramDump.p90, 0.1);
                assertEquals(0.95, histogramDump.p95, 0.1);
                assertEquals(0.98, histogramDump.p98, 0.1);
                assertEquals(0.99, histogramDump.p99, 0.1);
                assertEquals(0.999, histogramDump.p999, 0.1);
                assertEquals(4, histogramDump.mean, 0.1);
                assertEquals(5, histogramDump.stddev, 0.1);
                assertEquals(6, histogramDump.max);
                assertEquals(7, histogramDump.min);
                assertTrue(histogramDump.scopeInfo instanceof QueryScopeInfo.OperatorQueryScopeInfo);
                QueryScopeInfo.OperatorQueryScopeInfo opInfo = (QueryScopeInfo.OperatorQueryScopeInfo) histogramDump.scopeInfo;
                assertEquals("E", opInfo.scope);
                assertEquals("jid", opInfo.jobID);
                assertEquals("vid", opInfo.vertexID);
                assertEquals(2, opInfo.subtaskIndex);
                assertEquals("opname", opInfo.operatorName);
                histograms.remove(h1);
                break;
            case METRIC_CATEGORY_METER:
                MetricDump.MeterDump meterDump = (MetricDump.MeterDump) metric;
                assertEquals(5.0, meterDump.rate, 0.1);
                assertTrue(meterDump.scopeInfo instanceof QueryScopeInfo.JobQueryScopeInfo);
                assertEquals("C", meterDump.scopeInfo.scope);
                assertEquals("c3", meterDump.name);
                assertEquals("jid", ((QueryScopeInfo.JobQueryScopeInfo) meterDump.scopeInfo).jobID);
                break;
            default:
                fail();
        }
    }
    assertTrue(counters.isEmpty());
    assertTrue(gauges.isEmpty());
    assertTrue(histograms.isEmpty());
}
Also used : TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram) Histogram(org.apache.flink.metrics.Histogram) HashMap(java.util.HashMap) Meter(org.apache.flink.metrics.Meter) Gauge(org.apache.flink.metrics.Gauge) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Counter(org.apache.flink.metrics.Counter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram) Test(org.junit.Test)

Example 2 with TestingHistogram

use of org.apache.flink.runtime.metrics.util.TestingHistogram in project flink by apache.

the class JMXReporterTest method testHistogramReporting.

/**
	 * Tests that histograms are properly reported via the JMXReporter.
	 */
@Test
public void testHistogramReporting() throws Exception {
    MetricRegistry registry = null;
    String histogramName = "histogram";
    try {
        Configuration config = new Configuration();
        config.setString(ConfigConstants.METRICS_REPORTERS_LIST, "jmx_test");
        config.setString(ConfigConstants.METRICS_REPORTER_PREFIX + "jmx_test." + ConfigConstants.METRICS_REPORTER_CLASS_SUFFIX, JMXReporter.class.getName());
        registry = new MetricRegistry(MetricRegistryConfiguration.fromConfiguration(config));
        TaskManagerMetricGroup metricGroup = new TaskManagerMetricGroup(registry, "localhost", "tmId");
        TestingHistogram histogram = new TestingHistogram();
        metricGroup.histogram(histogramName, histogram);
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        ObjectName objectName = new ObjectName(JMX_DOMAIN_PREFIX + "taskmanager." + histogramName, JMXReporter.generateJmxTable(metricGroup.getAllVariables()));
        MBeanInfo info = mBeanServer.getMBeanInfo(objectName);
        MBeanAttributeInfo[] attributeInfos = info.getAttributes();
        assertEquals(11, attributeInfos.length);
        assertEquals(histogram.getCount(), mBeanServer.getAttribute(objectName, "Count"));
        assertEquals(histogram.getStatistics().getMean(), mBeanServer.getAttribute(objectName, "Mean"));
        assertEquals(histogram.getStatistics().getStdDev(), mBeanServer.getAttribute(objectName, "StdDev"));
        assertEquals(histogram.getStatistics().getMax(), mBeanServer.getAttribute(objectName, "Max"));
        assertEquals(histogram.getStatistics().getMin(), mBeanServer.getAttribute(objectName, "Min"));
        assertEquals(histogram.getStatistics().getQuantile(0.5), mBeanServer.getAttribute(objectName, "Median"));
        assertEquals(histogram.getStatistics().getQuantile(0.75), mBeanServer.getAttribute(objectName, "75thPercentile"));
        assertEquals(histogram.getStatistics().getQuantile(0.95), mBeanServer.getAttribute(objectName, "95thPercentile"));
        assertEquals(histogram.getStatistics().getQuantile(0.98), mBeanServer.getAttribute(objectName, "98thPercentile"));
        assertEquals(histogram.getStatistics().getQuantile(0.99), mBeanServer.getAttribute(objectName, "99thPercentile"));
        assertEquals(histogram.getStatistics().getQuantile(0.999), mBeanServer.getAttribute(objectName, "999thPercentile"));
    } finally {
        if (registry != null) {
            registry.shutdown();
        }
    }
}
Also used : MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) Configuration(org.apache.flink.configuration.Configuration) MBeanInfo(javax.management.MBeanInfo) TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram) MetricRegistry(org.apache.flink.runtime.metrics.MetricRegistry) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 3 with TestingHistogram

use of org.apache.flink.runtime.metrics.util.TestingHistogram in project flink by apache.

the class MetricFetcherTest method createRequestDumpAnswer.

private static MetricDumpSerialization.MetricSerializationResult createRequestDumpAnswer(InstanceID tmID, JobID jobID) throws IOException {
    Map<Counter, Tuple2<QueryScopeInfo, String>> counters = new HashMap<>();
    Map<Gauge<?>, Tuple2<QueryScopeInfo, String>> gauges = new HashMap<>();
    Map<Histogram, Tuple2<QueryScopeInfo, String>> histograms = new HashMap<>();
    Map<Meter, Tuple2<QueryScopeInfo, String>> meters = new HashMap<>();
    SimpleCounter c1 = new SimpleCounter();
    SimpleCounter c2 = new SimpleCounter();
    c1.inc(1);
    c2.inc(2);
    counters.put(c1, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.OperatorQueryScopeInfo(jobID.toString(), "taskid", 2, "opname", "abc"), "oc"));
    counters.put(c2, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.TaskQueryScopeInfo(jobID.toString(), "taskid", 2, "abc"), "tc"));
    meters.put(new Meter() {

        @Override
        public void markEvent() {
        }

        @Override
        public void markEvent(long n) {
        }

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

        @Override
        public long getCount() {
            return 10;
        }
    }, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.JobQueryScopeInfo(jobID.toString(), "abc"), "jc"));
    gauges.put(new Gauge<String>() {

        @Override
        public String getValue() {
            return "x";
        }
    }, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.TaskManagerQueryScopeInfo(tmID.toString(), "abc"), "gauge"));
    histograms.put(new TestingHistogram(), new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.JobManagerQueryScopeInfo("abc"), "hist"));
    MetricDumpSerialization.MetricDumpSerializer serializer = new MetricDumpSerialization.MetricDumpSerializer();
    MetricDumpSerialization.MetricSerializationResult dump = serializer.serialize(counters, gauges, histograms, meters);
    serializer.close();
    return dump;
}
Also used : TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram) Histogram(org.apache.flink.metrics.Histogram) HashMap(java.util.HashMap) Meter(org.apache.flink.metrics.Meter) QueryScopeInfo(org.apache.flink.runtime.metrics.dump.QueryScopeInfo) Gauge(org.apache.flink.metrics.Gauge) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Counter(org.apache.flink.metrics.Counter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) MetricDumpSerialization(org.apache.flink.runtime.metrics.dump.MetricDumpSerialization) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram)

Example 4 with TestingHistogram

use of org.apache.flink.runtime.metrics.util.TestingHistogram in project flink by apache.

the class MetricQueryServiceTest method testCreateDump.

@Test
public void testCreateDump() throws Exception {
    ActorSystem s = AkkaUtils.createLocalActorSystem(new Configuration());
    ActorRef serviceActor = MetricQueryService.startMetricQueryService(s, null);
    TestActorRef testActorRef = TestActorRef.create(s, Props.create(TestActor.class));
    TestActor testActor = (TestActor) testActorRef.underlyingActor();
    final Counter c = new SimpleCounter();
    final Gauge<String> g = new Gauge<String>() {

        @Override
        public String getValue() {
            return "Hello";
        }
    };
    final Histogram h = new TestingHistogram();
    final Meter m = new Meter() {

        @Override
        public void markEvent() {
        }

        @Override
        public void markEvent(long n) {
        }

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

        @Override
        public long getCount() {
            return 10;
        }
    };
    MetricRegistry registry = new MetricRegistry(MetricRegistryConfiguration.defaultMetricRegistryConfiguration());
    final TaskManagerMetricGroup tm = new TaskManagerMetricGroup(registry, "host", "id");
    MetricQueryService.notifyOfAddedMetric(serviceActor, c, "counter", tm);
    MetricQueryService.notifyOfAddedMetric(serviceActor, g, "gauge", tm);
    MetricQueryService.notifyOfAddedMetric(serviceActor, h, "histogram", tm);
    MetricQueryService.notifyOfAddedMetric(serviceActor, m, "meter", tm);
    serviceActor.tell(MetricQueryService.getCreateDump(), testActorRef);
    synchronized (testActor.lock) {
        if (testActor.message == null) {
            testActor.lock.wait();
        }
    }
    MetricDumpSerialization.MetricSerializationResult dump = (MetricDumpSerialization.MetricSerializationResult) testActor.message;
    testActor.message = null;
    assertTrue(dump.serializedMetrics.length > 0);
    MetricQueryService.notifyOfRemovedMetric(serviceActor, c);
    MetricQueryService.notifyOfRemovedMetric(serviceActor, g);
    MetricQueryService.notifyOfRemovedMetric(serviceActor, h);
    MetricQueryService.notifyOfRemovedMetric(serviceActor, m);
    serviceActor.tell(MetricQueryService.getCreateDump(), testActorRef);
    synchronized (testActor.lock) {
        if (testActor.message == null) {
            testActor.lock.wait();
        }
    }
    MetricDumpSerialization.MetricSerializationResult emptyDump = (MetricDumpSerialization.MetricSerializationResult) testActor.message;
    testActor.message = null;
    assertEquals(0, emptyDump.serializedMetrics.length);
    s.shutdown();
}
Also used : ActorSystem(akka.actor.ActorSystem) TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram) Histogram(org.apache.flink.metrics.Histogram) Configuration(org.apache.flink.configuration.Configuration) MetricRegistryConfiguration(org.apache.flink.runtime.metrics.MetricRegistryConfiguration) Meter(org.apache.flink.metrics.Meter) TestActorRef(akka.testkit.TestActorRef) ActorRef(akka.actor.ActorRef) MetricRegistry(org.apache.flink.runtime.metrics.MetricRegistry) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) TestActorRef(akka.testkit.TestActorRef) Gauge(org.apache.flink.metrics.Gauge) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Counter(org.apache.flink.metrics.Counter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram) Test(org.junit.Test)

Aggregations

TestingHistogram (org.apache.flink.runtime.metrics.util.TestingHistogram)4 Counter (org.apache.flink.metrics.Counter)3 Gauge (org.apache.flink.metrics.Gauge)3 Histogram (org.apache.flink.metrics.Histogram)3 Meter (org.apache.flink.metrics.Meter)3 SimpleCounter (org.apache.flink.metrics.SimpleCounter)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)2 Configuration (org.apache.flink.configuration.Configuration)2 MetricRegistry (org.apache.flink.runtime.metrics.MetricRegistry)2 MetricRegistryConfiguration (org.apache.flink.runtime.metrics.MetricRegistryConfiguration)2 TaskManagerMetricGroup (org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup)2 ActorRef (akka.actor.ActorRef)1 ActorSystem (akka.actor.ActorSystem)1 TestActorRef (akka.testkit.TestActorRef)1 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)1 MBeanInfo (javax.management.MBeanInfo)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1