Added: 
incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestMetricsConfig.java
URL: 
http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestMetricsConfig.java?rev=1207083&view=auto
==============================================================================
--- 
incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestMetricsConfig.java
 (added)
+++ 
incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestMetricsConfig.java
 Mon Nov 28 11:21:27 2011
@@ -0,0 +1,139 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.metrics;
+
+import java.util.Properties;
+import java.util.Set;
+
+import junit.framework.TestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hama.HamaConfiguration;
+import static junit.framework.Assert.*;
+
+public class TestMetricsConfig extends TestCase {
+  
+  public static final Log LOG = LogFactory.getLog(TestMetricsConfig.class); 
+
+  private HamaConfiguration conf;
+
+  public void setUp() throws Exception{
+    this.conf = new HamaConfiguration();
+  }
+
+  public void testCreate() throws Exception {
+    this.conf.set("bsp.metrics.conf_files", 
+                  "hama-metrics-notexist.properties, 
hama-metrics-test-config.properties");
+    MetricsConfig config = MetricsConfig.create(this.conf);
+    assertNotNull("Test if creating with hama-metrics-test.properties " + 
+                  "successfully.", config);
+    Set<MetricsConfig.Entry<String, String>> sub = config.subset("bspmaster"); 
+    for(MetricsConfig.Entry<String, String> entry: sub){
+      String key = entry.key();
+      String value = entry.value();
+      LOG.info("key "+key+" value "+value);
+      if(-1 != key.indexOf("file_jvm")){
+        if(-1 != key.indexOf("options")){
+          assertEquals("Test if key is bspmaster.sink.file_jvm.options.", 
+                       key, "bspmaster.sink.file_jvm.options");
+          assertEquals("Test if value is jvm.", value, "jvm");
+        }
+      }else{
+        fail("Key in subset should contain options.");
+      }
+    }
+  }
+ 
+  public void testSubset() throws Exception {
+    final Properties prop = new Properties();
+    prop.setProperty("test.source.instance.options", "jvm");
+    prop.setProperty("test.sink.instance.options", "systemmonitorsink");
+    prop.setProperty("bspmaster.source.instance.options", 
"testbspmastersource");
+    prop.setProperty("bspmaster.sink.instance.options", "testbspmastersink");
+    prop.setProperty("groom.source.instance.options", "testgroomsource");
+    prop.setProperty("groom.sink.instance1.options", "testgroomsink");
+    MetricsConfig mc = new MetricsConfig(prop); // only for test
+    assertNotNull("Test initialize MetricsConfig instance.", mc);
+    LOG.info("Test subset(prefix)");
+    Set<MetricsConfig.Entry<String, String>> prefix = mc.subset("test");
+    for(MetricsConfig.Entry<String, String> entry: prefix){
+      String key = entry.key();
+      LOG.info("key:"+key);
+      if(-1 != key.indexOf("source")){
+        assertEquals("Test key with prefix.", key, 
+                   "test.source.instance.options");
+      }
+      if(-1 != key.indexOf("sink")){
+        assertEquals("Test key with prefix.", key, 
+                     "test.sink.instance.options");
+      }
+      String value = entry.value();
+      LOG.info("value:"+value);
+      if(-1 != key.indexOf("source")){
+        assertEquals("Test value with test prefix.", value, "jvm");
+      }
+      if(-1 != key.indexOf("sink")){
+        assertEquals("Test value with test prefix.", value, 
+                     "systemmonitorsink");
+      }
+    }
+
+    LOG.info("Test subset(prefix, type)");
+    Set<MetricsConfig.Entry<String, String>> mtype = 
+      mc.subset("bspmaster", "sink");
+    int count = 0;
+    for(MetricsConfig.Entry<String, String> entry: mtype){
+      String key = entry.key();
+      String value = entry.value();
+      LOG.info("key: "+key+" value: "+value);
+      if(key.startsWith("bspmaster")) {
+        if(-1 != key.indexOf("sink")){
+          assertEquals("Test key.", key, "bspmaster.sink.instance.options");
+          assertEquals("Test value.", value, "testbspmastersink");
+          count++;
+        }
+      }else {
+        fail("Key should start with bspmaster!");
+      }
+    }
+    assertEquals("Test result count for subset(prefix, type).", 
+                 count, 1);
+
+    LOG.info("Test subset(prefix, type, instance)");
+    Set<MetricsConfig.Entry<String, String>> instances = 
+      mc.subset("groom", "source", "instance");
+    count = 0;
+    for(MetricsConfig.Entry<String, String> entry: instances){
+      String key = entry.key();
+      String value = entry.value();
+      if(key.startsWith("groom")){ 
+        if(-1 != key.indexOf("source")) {
+          assertEquals("Test key.", key, "groom.source.instance.options");
+          assertEquals("Test value.", value, "testgroomsource");
+          count++;
+        }
+      }else{
+        fail("Key should start with groom!");
+      }
+    } 
+    assertEquals("Test result count for subset(prefix, type, instance).", 
+                 count, 1);
+  }
+
+  public void tearDown() throws Exception { }
+}

Added: 
incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestMetricsSystem.java
URL: 
http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestMetricsSystem.java?rev=1207083&view=auto
==============================================================================
--- 
incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestMetricsSystem.java
 (added)
+++ 
incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestMetricsSystem.java
 Mon Nov 28 11:21:27 2011
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.metrics;
+
+import junit.framework.TestCase;
+import org.apache.hama.HamaConfiguration;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class TestMetricsSystem extends TestCase {
+
+  public static final Log LOG = LogFactory.getLog(TestMetricsSystem.class);
+
+  private MetricsSystem metricsSystem;
+  private HamaConfiguration conf;
+
+  public void setUp() throws Exception{
+    this.conf = new HamaConfiguration();
+    this.conf.set("bsp.metrics.conf_files", "hama-metrics-msys.properties");
+    this.metricsSystem = MetricsFactory.createMetricsSystem("test", conf);
+    assertNotNull("Ensure metrics system is not null.", this.metricsSystem);
+    this.metricsSystem.start();
+  }
+
+  public void testDefaultMetricsSystem() throws Exception {
+    MetricsSource src = MetricsFactory.createSource(JvmMetrics.class, 
+      new Object[]{ "processName", "sessionId", conf });
+    this.metricsSystem.register(JvmMetrics.class.getName(), 
+                                "Test Jvm metrics source.", src);
+    // default value the metrics system will periodically harvet 
+    // metrics information from source to sink.
+    long period = this.metricsSystem.period();
+    LOG.info("period (in seconds) the metrics system is configured: "+period);
+    Thread.sleep(period*1000); 
+    MetricsSink sink =  this.metricsSystem.findSink(
+      "org.apache.hama.metrics.SystemMonitorSink");
+    assertNotNull("Sink should be auto registered.", sink);
+    MetricsRecord record = ((SystemMonitorSink)sink).get();
+    assertNotNull("Check if MetriscRecord has data.", record);
+    for(Metric<? extends Number> metric: record.metrics()){
+      Number num = metric.value();
+      assertTrue("Check metrics data returned.", 
+                 num.longValue() > 0 || num.longValue() <= 0);
+    }
+  }
+
+  public void tearDown() throws Exception {
+    this.metricsSystem.stop();
+  }
+}

Added: 
incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestPatternFilter.java
URL: 
http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestPatternFilter.java?rev=1207083&view=auto
==============================================================================
--- 
incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestPatternFilter.java
 (added)
+++ 
incubator/hama/trunk/core/src/test/java/org/apache/hama/metrics/TestPatternFilter.java
 Mon Nov 28 11:21:27 2011
@@ -0,0 +1,143 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.metrics;
+
+import java.util.Arrays;
+import java.util.Properties;
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.TestCase;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.metrics.MetricsConfig.Entry;
+import static junit.framework.Assert.*;
+
+public class TestPatternFilter extends TestCase {
+  
+  public static final Log LOG = LogFactory.getLog(TestPatternFilter.class); 
+
+  private HamaConfiguration conf;
+
+  public void setUp() throws Exception{
+    this.conf = new HamaConfiguration();
+  }
+
+  public void testEmptyConfig() throws Exception {
+    Set<Entry<String, String>> sub = 
+      new MetricsConfig(new Properties()).subset("");
+    shouldAccept(sub, "anything");
+    shouldAccept(sub, tags(newTag("key", "value")));
+  }
+
+  public void testIncludeAndIncludeTag() throws Exception {
+    final Properties prop = new Properties();
+    prop.setProperty("test.sink.include", "garbage");
+    prop.setProperty("test.source.include", "foo");
+    prop.setProperty("test.source.include.tags", "foo:f");
+    final MetricsConfig mc = new MetricsConfig(prop); 
+    assertNotNull("Test initialize MetricsConfig instance.", mc);
+
+    Set<Entry<String, String>> sub = mc.subset("test", "source");
+    assertEquals("Subset size is as expected.", 2, sub.size());
+    shouldReject(sub, "bar");
+    shouldReject(sub, tags(newTag("bar", "")));
+    shouldReject(sub, tags(newTag("foo", "boo")));
+  }
+
+  public void testExcludeAndExcludeTag() throws Exception {
+    final Properties prop = new Properties();
+    prop.setProperty("p.exclude", "foo");
+    prop.setProperty("p.exclude.tags", "foo:f");
+    final MetricsConfig mc = new MetricsConfig(prop);
+    Set<Entry<String, String>> sub = mc.subset("p");
+    shouldAccept(sub, "bar");
+    shouldAccept(sub, tags(newTag("bar", "")));
+    shouldReject(sub, "foo");
+    shouldReject(sub, tags(newTag("bar", ""), newTag("foo", "f")));
+  }
+
+  public void testAcceptUnmatchedWhenBothAreConfigured() throws Exception {
+    final Properties prop = new Properties();
+    prop.setProperty("p.include", "foo");
+    prop.setProperty("p.include.tags", "foo:f");
+    prop.setProperty("p.exclude", "bar");
+    prop.setProperty("p.exclude.tags", "bar:b");
+    final MetricsConfig mc = new MetricsConfig(prop);
+    Set<Entry<String, String>> sub = mc.subset("p"); 
+    shouldAccept(sub, "foo");
+    shouldAccept(sub, tags(newTag("foo", "f")));
+    shouldReject(sub, "bar");
+    shouldReject(sub, tags(newTag("bar", "b")));
+    shouldAccept(sub, "foobar");
+    shouldAccept(sub, tags(newTag("foobar", "")));
+  }
+
+  public void testIncludeOverrideExclude() throws Exception {
+    final Properties prop = new Properties();
+    prop.setProperty("p.include", "foo");
+    prop.setProperty("p.include.tags", "foo:f");
+    prop.setProperty("p.exclude", "foo");
+    prop.setProperty("p.exclude.tags", "foo:f");
+    final MetricsConfig mc = new MetricsConfig(prop);
+    Set<Entry<String, String>> sub = mc.subset("p"); 
+    shouldAccept(sub, "foo");
+    shouldAccept(sub, tags(newTag("foo", "f")));
+  }
+
+  static void shouldAccept(Set<Entry<String, String>> sub, String s) {
+    assertTrue("Accepts "+ s, newGlobalFilter(sub).accepts(s));
+    assertTrue("Accepts "+ s, newRegexFilter(sub).accepts(s));
+  }
+
+  static void shouldAccept(Set<Entry<String, String>> sub, 
+      Iterable<MetricsTag> tags) {
+    assertTrue("Accepts "+ tags, newGlobalFilter(sub).accepts(tags));
+    assertTrue("Accepts "+ tags, newRegexFilter(sub).accepts(tags));
+  }
+
+  static void shouldReject(Set<Entry<String, String>> sub, String s) {
+    assertTrue("Rejects "+ s, !newGlobalFilter(sub).accepts(s));
+    assertTrue("Rejects "+ s, !newRegexFilter(sub).accepts(s));
+  }
+
+  static void shouldReject(Set<Entry<String, String>> sub, 
+      Iterable<MetricsTag> tags) {
+    assertTrue("Rejects "+ tags, !newGlobalFilter(sub).accepts(tags));
+    assertTrue("Rejects "+ tags, !newRegexFilter(sub).accepts(tags));
+  }
+
+  static GlobalFilter newGlobalFilter(Set<Entry<String, String>> sub) {
+    return new GlobalFilter(sub);
+  }
+
+  static RegexFilter newRegexFilter(Set<Entry<String, String>> sub) {
+    return new RegexFilter(sub);
+  }
+
+  private Iterable<MetricsTag> tags(MetricsTag ... tags){
+    return Arrays.asList(tags);
+  }
+
+  private MetricsTag newTag(String pre, String t){
+    return new MetricsTag(pre, t);
+  }
+
+  public void tearDown() throws Exception { }
+}

Added: incubator/hama/trunk/core/src/test/resources/hama-metrics-msys.properties
URL: 
http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/test/resources/hama-metrics-msys.properties?rev=1207083&view=auto
==============================================================================
--- incubator/hama/trunk/core/src/test/resources/hama-metrics-msys.properties 
(added)
+++ incubator/hama/trunk/core/src/test/resources/hama-metrics-msys.properties 
Mon Nov 28 11:21:27 2011
@@ -0,0 +1,3 @@
+# syntax: [prefix].[source|sink].[instance].[options]
+
+test.sink.systemmonitor.class=org.apache.hama.metrics.SystemMonitorSink

Added: 
incubator/hama/trunk/core/src/test/resources/hama-metrics-test-config.properties
URL: 
http://svn.apache.org/viewvc/incubator/hama/trunk/core/src/test/resources/hama-metrics-test-config.properties?rev=1207083&view=auto
==============================================================================
--- 
incubator/hama/trunk/core/src/test/resources/hama-metrics-test-config.properties
 (added)
+++ 
incubator/hama/trunk/core/src/test/resources/hama-metrics-test-config.properties
 Mon Nov 28 11:21:27 2011
@@ -0,0 +1,7 @@
+# syntax: [prefix].[source|sink].[instance].[options]
+
+bspmaster.sink.file_jvm.name=jvm
+bspmaster.sink.file_jvm.filename=bspmaster-jvm-metrics.out
+
+groom.sink.file.filename=groom-metrics.out
+


Reply via email to