public virtual void TestMetricsCache() { MetricsSystem ms = new MetricsSystemImpl("cache"); ms.Start(); try { string p1 = "root1"; string leafQueueName = "root1.leaf"; QueueMetrics p1Metrics = QueueMetrics.ForQueue(ms, p1, null, true, conf); Queue parentQueue1 = MockitoMaker.Make(MockitoMaker.Stub <Queue>().Returning(p1Metrics ).from.GetMetrics()); QueueMetrics metrics = QueueMetrics.ForQueue(ms, leafQueueName, parentQueue1, true , conf); NUnit.Framework.Assert.IsNotNull("QueueMetrics for A shoudn't be null", metrics); // Re-register to check for cache hit, shouldn't blow up metrics-system... // also, verify parent-metrics QueueMetrics alterMetrics = QueueMetrics.ForQueue(ms, leafQueueName, parentQueue1 , true, conf); NUnit.Framework.Assert.IsNotNull("QueueMetrics for alterMetrics shoudn't be null" , alterMetrics); } finally { ms.Shutdown(); } }
/// <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 ()); }