001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.metrics.impl;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNotNull;
022import static org.junit.Assert.assertTrue;
023
024import java.util.Map;
025import java.util.Optional;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.metrics.Counter;
028import org.apache.hadoop.hbase.metrics.Gauge;
029import org.apache.hadoop.hbase.metrics.Meter;
030import org.apache.hadoop.hbase.metrics.Metric;
031import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
032import org.apache.hadoop.hbase.metrics.Timer;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039@Category(SmallTests.class)
040public class TestMetricRegistryImpl {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestMetricRegistryImpl.class);
045
046  private MetricRegistryInfo info;
047  private MetricRegistryImpl registry;
048
049  @Before
050  public void setUp() {
051    info = new MetricRegistryInfo("foo", "bar", "baz", "foobar", false);
052    registry = new MetricRegistryImpl(info);
053  }
054
055  @Test
056  public void testCounter() {
057    Counter counter = registry.counter("mycounter");
058    assertNotNull(counter);
059    counter.increment(42L);
060    Optional<Metric> metric = registry.get("mycounter");
061    assertTrue(metric.isPresent());
062    assertEquals(42L, (long) ((Counter) metric.get()).getCount());
063  }
064
065  @Test
066  public void testRegisterGauge() {
067    registry.register("mygauge", new Gauge<Long>() {
068      @Override
069      public Long getValue() {
070        return 42L;
071      }
072    });
073    Optional<Metric> metric = registry.get("mygauge");
074    assertTrue(metric.isPresent());
075    assertEquals(42L, (long) ((Gauge<Long>) metric.get()).getValue());
076  }
077
078  @Test
079  public void testRegisterGaugeLambda() {
080    // register a Gauge using lambda expression
081    registry.register("gaugeLambda", () -> 42L);
082    Optional<Metric> metric = registry.get("gaugeLambda");
083    assertTrue(metric.isPresent());
084    assertEquals(42L, (long) ((Gauge<Long>) metric.get()).getValue());
085  }
086
087  @Test
088  public void testTimer() {
089    Timer timer = registry.timer("mytimer");
090    assertNotNull(timer);
091    timer.updateNanos(100);
092  }
093
094  @Test
095  public void testMeter() {
096    Meter meter = registry.meter("mymeter");
097    assertNotNull(meter);
098    meter.mark();
099  }
100
101  @Test
102  public void testRegister() {
103    CounterImpl counter = new CounterImpl();
104    registry.register("mycounter", counter);
105    counter.increment(42L);
106
107    Optional<Metric> metric = registry.get("mycounter");
108    assertTrue(metric.isPresent());
109    assertEquals(42L, (long) ((Counter) metric.get()).getCount());
110  }
111
112  @Test
113  public void testDoubleRegister() {
114    Gauge g1 = registry.register("mygauge", () -> 42L);
115    Gauge g2 = registry.register("mygauge", () -> 52L);
116
117    // second gauge is ignored if it exists
118    assertEquals(g1, g2);
119
120    Optional<Metric> metric = registry.get("mygauge");
121    assertTrue(metric.isPresent());
122    assertEquals(42L, (long) ((Gauge<Long>) metric.get()).getValue());
123
124    Counter c1 = registry.counter("mycounter");
125    Counter c2 = registry.counter("mycounter");
126
127    assertEquals(c1, c2);
128  }
129
130  @Test
131  public void testGetMetrics() {
132    CounterImpl counter = new CounterImpl();
133    registry.register("mycounter", counter);
134    Gauge gauge = registry.register("mygauge", () -> 42L);
135    Timer timer = registry.timer("mytimer");
136
137    Map<String, Metric> metrics = registry.getMetrics();
138    assertEquals(3, metrics.size());
139
140    assertEquals(counter, metrics.get("mycounter"));
141    assertEquals(gauge, metrics.get("mygauge"));
142    assertEquals(timer, metrics.get("mytimer"));
143  }
144}