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