public virtual void TestContainerMetricsFinished()
        {
            MetricsSystemImpl system = new MetricsSystemImpl();

            system.Init("test");
            MetricsCollectorImpl collector    = new MetricsCollectorImpl();
            ApplicationId        appId        = ApplicationId.NewInstance(1234, 3);
            ApplicationAttemptId appAttemptId = ApplicationAttemptId.NewInstance(appId, 4);
            ContainerId          containerId1 = ContainerId.NewContainerId(appAttemptId, 1);
            ContainerMetrics     metrics1     = ContainerMetrics.ForContainer(system, containerId1, 1
                                                                              , 0);
            ContainerId      containerId2 = ContainerId.NewContainerId(appAttemptId, 2);
            ContainerMetrics metrics2     = ContainerMetrics.ForContainer(system, containerId2, 1
                                                                          , 0);
            ContainerId      containerId3 = ContainerId.NewContainerId(appAttemptId, 3);
            ContainerMetrics metrics3     = ContainerMetrics.ForContainer(system, containerId3, 1
                                                                          , 0);

            metrics1.Finished();
            metrics2.Finished();
            system.SampleMetrics();
            system.SampleMetrics();
            Sharpen.Thread.Sleep(100);
            system.Stop();
            // verify metrics1 is unregistered
            NUnit.Framework.Assert.IsTrue(metrics1 != ContainerMetrics.ForContainer(system, containerId1
                                                                                    , 1, 0));
            // verify metrics2 is unregistered
            NUnit.Framework.Assert.IsTrue(metrics2 != ContainerMetrics.ForContainer(system, containerId2
                                                                                    , 1, 0));
            // verify metrics3 is still registered
            NUnit.Framework.Assert.IsTrue(metrics3 == ContainerMetrics.ForContainer(system, containerId3
                                                                                    , 1, 0));
            system.Shutdown();
        }
Beispiel #2
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestFileSink()
        {
            outFile = GetTestTempFile("test-file-sink-", ".out");
            string outPath = outFile.GetAbsolutePath();

            // NB: specify large period to avoid multiple metrics snapshotting:
            new ConfigBuilder().Add("*.period", 10000).Add("test.sink.mysink0.class", typeof(
                                                               FileSink).FullName).Add("test.sink.mysink0.filename", outPath).Add("test.sink.mysink0.context"
                                                                                                                                  , "test1").Save(TestMetricsConfig.GetTestFilename("hadoop-metrics2-test"));
            // NB: we filter by context to exclude "metricssystem" context metrics:
            MetricsSystemImpl ms = new MetricsSystemImpl("test");

            ms.Start();
            TestFileSink.MyMetrics1 mm1 = new TestFileSink.MyMetrics1().RegisterWith(ms);
            new TestFileSink.MyMetrics2().RegisterWith(ms);
            mm1.testMetric1.Incr();
            mm1.testMetric2.Incr(2);
            ms.PublishMetricsNow();
            // publish the metrics
            ms.Stop();
            ms.Shutdown();
            InputStream           @is  = null;
            ByteArrayOutputStream baos = null;
            string outFileContent      = null;

            try
            {
                @is  = new FileInputStream(outFile);
                baos = new ByteArrayOutputStream((int)outFile.Length());
                IOUtils.CopyBytes(@is, baos, 1024, true);
                outFileContent = Runtime.GetStringForBytes(baos.ToByteArray(), "UTF-8");
            }
            finally
            {
                IOUtils.Cleanup(null, baos, @is);
            }
            // Check the out file content. Should be something like the following:
            //1360244820087 test1.testRecord1: Context=test1, testTag1=testTagValue1, testTag2=testTagValue2, Hostname=myhost, testMetric1=1, testMetric2=2
            //1360244820089 test1.testRecord2: Context=test1, testTag22=testTagValue22, Hostname=myhost
            // Note that in the below expression we allow tags and metrics to go in arbitrary order.
            Pattern expectedContentPattern = Pattern.Compile("^\\d+\\s+test1.testRecord1:\\s+Context=test1,\\s+"
                                                             + "(testTag1=testTagValue1,\\s+testTag2=testTagValue2|testTag2=testTagValue2,\\s+testTag1=testTagValue1),"
                                                             + "\\s+Hostname=.*,\\s+(testMetric1=1,\\s+testMetric2=2|testMetric2=2,\\s+testMetric1=1)"
                                                             + "$[\\n\\r]*^\\d+\\s+test1.testRecord2:\\s+Context=test1," + "\\s+testTag22=testTagValue22,\\s+Hostname=.*$[\\n\\r]*"
                                                             , Pattern.Multiline);

            // line #1:
            // line #2:
            Assert.True(expectedContentPattern.Matcher(outFileContent).Matches
                            ());
        }