1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.metrics;
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.management.MBeanAttributeInfo;
28  import javax.management.MBeanInfo;
29  
30  import org.apache.hadoop.metrics.MetricsContext;
31  import org.apache.hadoop.metrics.MetricsRecord;
32  import org.apache.hadoop.metrics.MetricsUtil;
33  import org.apache.hadoop.metrics.util.MetricsIntValue;
34  import org.apache.hadoop.metrics.util.MetricsRegistry;
35  import org.apache.hadoop.metrics.util.MetricsTimeVaryingRate;
36  
37  import junit.framework.TestCase;
38  
39  public class TestMetricsMBeanBase extends TestCase {
40  
41    private class TestStatistics extends MetricsMBeanBase {
42      public TestStatistics(MetricsRegistry registry) {
43        super(registry, "TestStatistics");
44      }
45    }
46  
47    private MetricsRegistry registry;
48    private MetricsRecord metricsRecord;
49    private TestStatistics stats;
50    private MetricsRate metricsRate;
51    private MetricsIntValue intValue;
52    private MetricsTimeVaryingRate varyRate;
53  
54    public void setUp() {
55      this.registry = new MetricsRegistry();
56      this.metricsRate = new MetricsRate("metricsRate", registry, "test");
57      this.intValue = new MetricsIntValue("intValue", registry, "test");
58      this.varyRate = new MetricsTimeVaryingRate("varyRate", registry, "test");
59      this.stats = new TestStatistics(registry);
60      MetricsContext context = MetricsUtil.getContext("hbase");
61      this.metricsRecord = MetricsUtil.createRecord(context, "test");
62      this.metricsRecord.setTag("TestStatistics", "test");
63      //context.registerUpdater(this);
64  
65    }
66  
67    public void tearDown() {
68  
69    }
70  
71    public void testGetAttribute() throws Exception {
72      this.metricsRate.inc(2);
73      Thread.sleep(1000);
74      this.metricsRate.pushMetric(this.metricsRecord);
75      this.intValue.set(5);
76      this.intValue.pushMetric(this.metricsRecord);
77      this.varyRate.inc(10);
78      this.varyRate.inc(50);
79      this.varyRate.pushMetric(this.metricsRecord);
80  
81  
82      assertEquals( 2.0, (Float)this.stats.getAttribute("metricsRate"), 0.005 );
83      assertEquals( 5, this.stats.getAttribute("intValue") );
84      assertEquals( 10L, this.stats.getAttribute("varyRateMinTime") );
85      assertEquals( 50L, this.stats.getAttribute("varyRateMaxTime") );
86      assertEquals( 30L, this.stats.getAttribute("varyRateAvgTime") );
87      assertEquals( 2, this.stats.getAttribute("varyRateNumOps") );
88    }
89  
90    public void testGetMBeanInfo() {
91      MBeanInfo info = this.stats.getMBeanInfo();
92      MBeanAttributeInfo[] attributes = info.getAttributes();
93      assertEquals( 6, attributes.length );
94  
95      Map<String,MBeanAttributeInfo> attributeByName =
96          new HashMap<String,MBeanAttributeInfo>(attributes.length);
97      for (MBeanAttributeInfo attr : attributes)
98        attributeByName.put(attr.getName(), attr);
99  
100     assertAttribute( attributeByName.get("metricsRate"),
101         "metricsRate", "java.lang.Float", "test");
102     assertAttribute( attributeByName.get("intValue"),
103         "intValue", "java.lang.Integer", "test");
104     assertAttribute( attributeByName.get("varyRateMinTime"),
105         "varyRateMinTime", "java.lang.Long", "test");
106     assertAttribute( attributeByName.get("varyRateMaxTime"),
107         "varyRateMaxTime", "java.lang.Long", "test");
108     assertAttribute( attributeByName.get("varyRateAvgTime"),
109         "varyRateAvgTime", "java.lang.Long", "test");
110     assertAttribute( attributeByName.get("varyRateNumOps"),
111         "varyRateNumOps", "java.lang.Integer", "test");
112   }
113 
114   protected void assertAttribute(MBeanAttributeInfo attr, String name,
115       String type, String description) {
116 
117     assertEquals(attr.getName(), name);
118     assertEquals(attr.getType(), type);
119     assertEquals(attr.getDescription(), description);
120   }
121 
122 }