View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  package org.apache.hadoop.hbase.metrics.impl;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.util.Map;
28  
29  import org.apache.hadoop.hbase.metrics.Counter;
30  import org.apache.hadoop.hbase.metrics.Gauge;
31  import org.apache.hadoop.hbase.metrics.Meter;
32  import org.apache.hadoop.hbase.metrics.Metric;
33  import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
34  import org.apache.hadoop.hbase.metrics.Timer;
35  import org.apache.hadoop.hbase.testclassification.SmallTests;
36  import org.junit.Before;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  import com.google.common.base.Optional;
41  
42  @Category(SmallTests.class)
43  public class TestMetricRegistryImpl {
44    private MetricRegistryInfo info;
45    private MetricRegistryImpl registry;
46  
47    @Before
48    public void setUp() {
49      info = new MetricRegistryInfo("foo", "bar", "baz", "foobar", false);
50      registry = new MetricRegistryImpl(info);
51    }
52  
53    @Test
54    public void testCounter() {
55      Counter counter = registry.counter("mycounter");
56      assertNotNull(counter);
57      counter.increment(42L);
58      Optional<Metric> metric = registry.get("mycounter");
59      assertTrue(metric.isPresent());
60      assertEquals(42L, (long)((Counter)metric.get()).getCount());
61    }
62  
63    @Test
64    public void testRegisterGauge() {
65      registry.register("mygauge", new Gauge<Long>() {
66        @Override
67        public Long getValue() {
68          return 42L;
69        }
70      });
71      Optional<Metric> metric = registry.get("mygauge");
72      assertTrue(metric.isPresent());
73      assertEquals(42L, (long)((Gauge<Long>)metric.get()).getValue());
74    }
75  
76    @Test
77    public void testRegisterGaugeLambda() {
78      // register a Gauge using lambda expression
79      registry.register("gaugeLambda", new Gauge<Long>(){
80  
81        @Override
82        public Long getValue() {
83          return 42L;
84        }
85      });
86      Optional<Metric> metric = registry.get("gaugeLambda");
87      assertTrue(metric.isPresent());
88      assertEquals(42L, (long)((Gauge<Long>)metric.get()).getValue());
89    }
90  
91    @Test
92    public void testTimer() {
93      Timer timer = registry.timer("mytimer");
94      assertNotNull(timer);
95      timer.updateNanos(100);
96    }
97  
98    @Test
99    public void testMeter() {
100     Meter meter = registry.meter("mymeter");
101     assertNotNull(meter);
102     meter.mark();
103   }
104 
105   @Test
106   public void testRegister() {
107     CounterImpl counter = new CounterImpl();
108     registry.register("mycounter", counter);
109     counter.increment(42L);
110 
111     Optional<Metric> metric = registry.get("mycounter");
112     assertTrue(metric.isPresent());
113     assertEquals(42L, (long)((Counter)metric.get()).getCount());
114   }
115 
116   @Test
117   public void testDoubleRegister() {
118     Gauge g1 = registry.register("mygauge", new Gauge<Long>(){
119 
120       @Override
121       public Long getValue() {
122         return 42L;
123       }});
124     Gauge g2 = registry.register("mygauge", new Gauge<Long>(){
125 
126       @Override
127       public Long getValue() {
128         return 52L;
129       }});
130 
131     // second gauge is ignored if it exists
132     assertEquals(g1, g2);
133 
134     Optional<Metric> metric = registry.get("mygauge");
135     assertTrue(metric.isPresent());
136     assertEquals(42L, (long)((Gauge<Long>)metric.get()).getValue());
137 
138 
139     Counter c1 = registry.counter("mycounter");
140     Counter c2 = registry.counter("mycounter");
141 
142     assertEquals(c1, c2);
143   }
144 
145   @Test
146   public void testGetMetrics() {
147     CounterImpl counter = new CounterImpl();
148     registry.register("mycounter", counter);
149     Gauge gauge = registry.register("mygauge", new Gauge<Long>(){
150 
151       @Override
152       public Long getValue() {
153         return 42L;
154       }});
155     Timer timer = registry.timer("mytimer");
156 
157     Map<String, Metric> metrics = registry.getMetrics();
158     assertEquals(3, metrics.size());
159 
160     assertEquals(counter, metrics.get("mycounter"));
161     assertEquals(gauge, metrics.get("mygauge"));
162     assertEquals(timer, metrics.get("mytimer"));
163   }
164 }