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.AttributeNotFoundException;
28  import javax.management.MBeanAttributeInfo;
29  import javax.management.MBeanException;
30  import javax.management.MBeanInfo;
31  import javax.management.ReflectionException;
32  import org.apache.hadoop.hbase.metrics.histogram.MetricsHistogram;
33  import com.yammer.metrics.stats.Snapshot;
34  
35  import org.apache.hadoop.hbase.MediumTests;
36  import org.apache.hadoop.metrics.MetricsContext;
37  import org.apache.hadoop.metrics.MetricsRecord;
38  import org.apache.hadoop.metrics.MetricsUtil;
39  import org.apache.hadoop.metrics.util.MetricsIntValue;
40  import org.apache.hadoop.metrics.util.MetricsRegistry;
41  import org.apache.hadoop.metrics.util.MetricsTimeVaryingRate;
42  
43  import static org.mockito.Mockito.mock;
44  import static org.mockito.Mockito.when;
45  
46  import junit.framework.TestCase;
47  import org.junit.experimental.categories.Category;
48  
49  @Category(MediumTests.class)
50  public class TestMetricsMBeanBase extends TestCase {
51  
52    private class TestStatistics extends MetricsMBeanBase {
53      public TestStatistics(MetricsRegistry registry) {
54        super(registry, "TestStatistics");
55      }
56    }
57  
58    private MetricsRegistry registry;
59    private MetricsRecord metricsRecord;
60    private TestStatistics stats;
61    private MetricsRate metricsRate;
62    private MetricsIntValue intValue;
63    private MetricsTimeVaryingRate varyRate;
64  
65    public void setUp() {
66      this.registry = new MetricsRegistry();
67      this.metricsRate = new MetricsRate("metricsRate", registry, "test");
68      this.intValue = new MetricsIntValue("intValue", registry, "test");
69      this.varyRate = new MetricsTimeVaryingRate("varyRate", registry, "test");
70      this.stats = new TestStatistics(registry);
71      MetricsContext context = MetricsUtil.getContext("hbase");
72      this.metricsRecord = MetricsUtil.createRecord(context, "test");
73      this.metricsRecord.setTag("TestStatistics", "test");
74      //context.registerUpdater(this);
75  
76    }
77  
78    public void tearDown() {
79  
80    }
81  
82    public void testGetAttribute() throws Exception {
83      this.metricsRate.inc(2);
84      Thread.sleep(1000);
85      this.metricsRate.pushMetric(this.metricsRecord);
86      this.intValue.set(5);
87      this.intValue.pushMetric(this.metricsRecord);
88      this.varyRate.inc(10);
89      this.varyRate.inc(50);
90      this.varyRate.pushMetric(this.metricsRecord);
91  
92  
93      assertEquals( 2.0, (Float)this.stats.getAttribute("metricsRate"), 0.005 );
94      assertEquals( 5, this.stats.getAttribute("intValue") );
95      assertEquals( 10L, this.stats.getAttribute("varyRateMinTime") );
96      assertEquals( 50L, this.stats.getAttribute("varyRateMaxTime") );
97      assertEquals( 30L, this.stats.getAttribute("varyRateAvgTime") );
98      assertEquals( 2, this.stats.getAttribute("varyRateNumOps") );
99    }
100 
101   public void testGetMBeanInfo() {
102     MBeanInfo info = this.stats.getMBeanInfo();
103     MBeanAttributeInfo[] attributes = info.getAttributes();
104     assertEquals( 6, attributes.length );
105 
106     Map<String,MBeanAttributeInfo> attributeByName =
107         new HashMap<String,MBeanAttributeInfo>(attributes.length);
108     for (MBeanAttributeInfo attr : attributes)
109       attributeByName.put(attr.getName(), attr);
110 
111     assertAttribute( attributeByName.get("metricsRate"),
112         "metricsRate", "java.lang.Float", "test");
113     assertAttribute( attributeByName.get("intValue"),
114         "intValue", "java.lang.Integer", "test");
115     assertAttribute( attributeByName.get("varyRateMinTime"),
116         "varyRateMinTime", "java.lang.Long", "test");
117     assertAttribute( attributeByName.get("varyRateMaxTime"),
118         "varyRateMaxTime", "java.lang.Long", "test");
119     assertAttribute( attributeByName.get("varyRateAvgTime"),
120         "varyRateAvgTime", "java.lang.Long", "test");
121     assertAttribute( attributeByName.get("varyRateNumOps"),
122         "varyRateNumOps", "java.lang.Integer", "test");
123   }
124 
125   public void testMetricsMBeanBaseHistogram()
126       throws ReflectionException, AttributeNotFoundException, MBeanException {
127     MetricsRegistry mr = new MetricsRegistry();
128     MetricsHistogram histo = mock(MetricsHistogram.class);
129     Snapshot snap = mock(Snapshot.class);
130 
131     //Set up the mocks
132     String histoName = "MockHisto";
133     when(histo.getName()).thenReturn(histoName);
134     when(histo.getCount()).thenReturn(20l);
135     when(histo.getMin()).thenReturn(1l);
136     when(histo.getMax()).thenReturn(999l);
137     when(histo.getMean()).thenReturn(500.2);
138     when(histo.getStdDev()).thenReturn(1.2);
139     when(histo.getSnapshot()).thenReturn(snap);
140 
141     when(snap.getMedian()).thenReturn(490.0);
142     when(snap.get75thPercentile()).thenReturn(550.0);
143     when(snap.get95thPercentile()).thenReturn(900.0);
144     when(snap.get99thPercentile()).thenReturn(990.0);
145 
146     mr.add("myTestHisto", histo);
147 
148     MetricsMBeanBase mBeanBase = new MetricsMBeanBase(mr, "test");
149 
150     assertEquals(new Long(20), mBeanBase
151         .getAttribute(histoName + MetricsHistogram.NUM_OPS_METRIC_NAME));
152     assertEquals(new Long(1), mBeanBase
153         .getAttribute(histoName + MetricsHistogram.MIN_METRIC_NAME));
154     assertEquals(new Long(999), mBeanBase
155         .getAttribute(histoName + MetricsHistogram.MAX_METRIC_NAME));
156     assertEquals(new Float(500.2), mBeanBase
157         .getAttribute(histoName + MetricsHistogram.MEAN_METRIC_NAME));
158     assertEquals(new Float(1.2), mBeanBase
159         .getAttribute(histoName + MetricsHistogram.STD_DEV_METRIC_NAME));
160 
161     assertEquals(new Float(490.0), mBeanBase
162         .getAttribute(histoName + MetricsHistogram.MEDIAN_METRIC_NAME));
163     assertEquals(new Float(550.0), mBeanBase
164         .getAttribute(histoName + MetricsHistogram.SEVENTY_FIFTH_PERCENTILE_METRIC_NAME));
165     assertEquals(new Float(900.0), mBeanBase
166         .getAttribute(histoName + MetricsHistogram.NINETY_FIFTH_PERCENTILE_METRIC_NAME));
167     assertEquals(new Float(990.0), mBeanBase
168         .getAttribute(histoName + MetricsHistogram.NINETY_NINETH_PERCENTILE_METRIC_NAME));
169   }
170 
171   protected void assertAttribute(MBeanAttributeInfo attr, String name,
172       String type, String description) {
173 
174     assertEquals(attr.getName(), name);
175     assertEquals(attr.getType(), type);
176     assertEquals(attr.getDescription(), description);
177   }
178 
179 
180   @org.junit.Rule
181   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
182     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
183 }
184