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.assertFalse;
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.assertTrue;
024
025import java.util.Map;
026import java.util.Optional;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.metrics.Counter;
029import org.apache.hadoop.hbase.metrics.Gauge;
030import org.apache.hadoop.hbase.metrics.Meter;
031import org.apache.hadoop.hbase.metrics.Metric;
032import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
033import org.apache.hadoop.hbase.metrics.Timer;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.junit.Before;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040@Category(SmallTests.class)
041public class TestMetricRegistryImpl {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestMetricRegistryImpl.class);
046
047  private MetricRegistryInfo info;
048  private MetricRegistryImpl registry;
049
050  @Before
051  public void setUp() {
052    info = new MetricRegistryInfo("foo", "bar", "baz", "foobar", false);
053    registry = new MetricRegistryImpl(info);
054  }
055
056  @Test
057  public void testCounter() {
058    Counter counter = registry.counter("mycounter");
059    assertNotNull(counter);
060    counter.increment(42L);
061    Optional<Metric> metric = registry.get("mycounter");
062    assertTrue(metric.isPresent());
063    assertEquals(42L, (long) ((Counter) metric.get()).getCount());
064  }
065
066  @Test
067  public void testRegisterGauge() {
068    registry.register("mygauge", new Gauge<Long>() {
069      @Override
070      public Long getValue() {
071        return 42L;
072      }
073    });
074    Optional<Metric> metric = registry.get("mygauge");
075    assertTrue(metric.isPresent());
076    assertEquals(42L, (long) ((Gauge<Long>) metric.get()).getValue());
077  }
078
079  @Test
080  public void testRegisterGaugeLambda() {
081    // register a Gauge using lambda expression
082    registry.register("gaugeLambda", () -> 42L);
083    Optional<Metric> metric = registry.get("gaugeLambda");
084    assertTrue(metric.isPresent());
085    assertEquals(42L, (long) ((Gauge<Long>) metric.get()).getValue());
086  }
087
088  @Test
089  public void testTimer() {
090    Timer timer = registry.timer("mytimer");
091    assertNotNull(timer);
092    timer.updateNanos(100);
093  }
094
095  @Test
096  public void testMeter() {
097    Meter meter = registry.meter("mymeter");
098    assertNotNull(meter);
099    meter.mark();
100  }
101
102  @Test
103  public void testRegister() {
104    CounterImpl counter = new CounterImpl();
105    registry.register("mycounter", counter);
106    counter.increment(42L);
107
108    Optional<Metric> metric = registry.get("mycounter");
109    assertTrue(metric.isPresent());
110    assertEquals(42L, (long) ((Counter) metric.get()).getCount());
111  }
112
113  @Test
114  public void testDoubleRegister() {
115    Gauge g1 = registry.register("mygauge", () -> 42L);
116    Gauge g2 = registry.register("mygauge", () -> 52L);
117
118    // second gauge is ignored if it exists
119    assertEquals(g1, g2);
120
121    Optional<Metric> metric = registry.get("mygauge");
122    assertTrue(metric.isPresent());
123    assertEquals(42L, (long) ((Gauge<Long>) metric.get()).getValue());
124
125    Counter c1 = registry.counter("mycounter");
126    Counter c2 = registry.counter("mycounter");
127
128    assertEquals(c1, c2);
129  }
130
131  @Test
132  public void testGetMetrics() {
133    CounterImpl counter = new CounterImpl();
134    registry.register("mycounter", counter);
135    Gauge gauge = registry.register("mygauge", () -> 42L);
136    Timer timer = registry.timer("mytimer");
137
138    Map<String, Metric> metrics = registry.getMetrics();
139    assertEquals(3, metrics.size());
140
141    assertEquals(counter, metrics.get("mycounter"));
142    assertEquals(gauge, metrics.get("mygauge"));
143    assertEquals(timer, metrics.get("mytimer"));
144  }
145
146  @Test
147  public void testRemove() {
148    CounterImpl counter1 = new CounterImpl();
149    CounterImpl counter2 = new CounterImpl();
150    registry.register("mycounter", counter1);
151
152    boolean removed = registry.remove("mycounter", counter2);
153    Optional<Metric> metric = registry.get("mycounter");
154    assertFalse(removed);
155    assertTrue(metric.isPresent());
156    assertEquals(metric.get(), counter1);
157
158    removed = registry.remove("mycounter");
159    metric = registry.get("mycounter");
160    assertTrue(removed);
161    assertFalse(metric.isPresent());
162  }
163}