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      this.metricsRate.pushMetric(this.metricsRecord);
74      this.intValue.set(5);
75      this.intValue.pushMetric(this.metricsRecord);
76      this.varyRate.inc(10);
77      this.varyRate.inc(50);
78      this.varyRate.pushMetric(this.metricsRecord);
79  
80  
81      assertEquals( 2.0, (Float)this.stats.getAttribute("metricsRate"), 0.001 );
82      assertEquals( 5, this.stats.getAttribute("intValue") );
83      assertEquals( 10L, this.stats.getAttribute("varyRateMinTime") );
84      assertEquals( 50L, this.stats.getAttribute("varyRateMaxTime") );
85      assertEquals( 30L, this.stats.getAttribute("varyRateAvgTime") );
86      assertEquals( 2, this.stats.getAttribute("varyRateNumOps") );
87    }
88  
89    public void testGetMBeanInfo() {
90      MBeanInfo info = this.stats.getMBeanInfo();
91      MBeanAttributeInfo[] attributes = info.getAttributes();
92      assertEquals( 6, attributes.length );
93  
94      Map<String,MBeanAttributeInfo> attributeByName =
95          new HashMap<String,MBeanAttributeInfo>(attributes.length);
96      for (MBeanAttributeInfo attr : attributes)
97        attributeByName.put(attr.getName(), attr);
98  
99      assertAttribute( attributeByName.get("metricsRate"),
100         "metricsRate", "java.lang.Float", "test");
101     assertAttribute( attributeByName.get("intValue"),
102         "intValue", "java.lang.Integer", "test");
103     assertAttribute( attributeByName.get("varyRateMinTime"),
104         "varyRateMinTime", "java.lang.Long", "test");
105     assertAttribute( attributeByName.get("varyRateMaxTime"),
106         "varyRateMaxTime", "java.lang.Long", "test");
107     assertAttribute( attributeByName.get("varyRateAvgTime"),
108         "varyRateAvgTime", "java.lang.Long", "test");
109     assertAttribute( attributeByName.get("varyRateNumOps"),
110         "varyRateNumOps", "java.lang.Integer", "test");
111   }
112 
113   protected void assertAttribute(MBeanAttributeInfo attr, String name,
114       String type, String description) {
115 
116     assertEquals(attr.getName(), name);
117     assertEquals(attr.getType(), type);
118     assertEquals(attr.getDescription(), description);
119   }
120 
121 }