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.regionserver;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import org.apache.hadoop.hbase.CompatibilityFactory;
024import org.apache.hadoop.hbase.CompatibilitySingletonFactory;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.test.MetricsAssertHelper;
028import org.apache.hadoop.hbase.testclassification.RegionServerTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({RegionServerTests.class, SmallTests.class})
035public class TestMetricsTableLatencies {
036
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039      HBaseClassTestRule.forClass(TestMetricsTableLatencies.class);
040
041  public static MetricsAssertHelper HELPER =
042      CompatibilityFactory.getInstance(MetricsAssertHelper.class);
043
044  @Test
045  public void testTableWrapperAggregateMetrics() throws IOException {
046    TableName tn1 = TableName.valueOf("table1");
047    TableName tn2 = TableName.valueOf("table2");
048    MetricsTableLatencies latencies = CompatibilitySingletonFactory.getInstance(
049        MetricsTableLatencies.class);
050    assertTrue("'latencies' is actually " + latencies.getClass(),
051        latencies instanceof MetricsTableLatenciesImpl);
052    MetricsTableLatenciesImpl latenciesImpl = (MetricsTableLatenciesImpl) latencies;
053    RegionServerTableMetrics tableMetrics = new RegionServerTableMetrics();
054
055    // Metrics to each table should be disjoint
056    // N.B. each call to assertGauge removes all previously acquired metrics so we have to
057    //   make the metrics call and then immediately verify it. Trying to do multiple metrics
058    //   updates followed by multiple verifications will fail on the 2nd verification (as the
059    //   first verification cleaned the data structures in MetricsAssertHelperImpl).
060    tableMetrics.updateGet(tn1, 500L);
061    HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
062        tn1, MetricsTableLatencies.GET_TIME + "_" + "999th_percentile"), 500L, latenciesImpl);
063    tableMetrics.updatePut(tn1, 50L);
064    HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
065        tn1, MetricsTableLatencies.PUT_TIME + "_" + "99th_percentile"), 50L, latenciesImpl);
066
067    tableMetrics.updateGet(tn2, 300L);
068    HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
069        tn2, MetricsTableLatencies.GET_TIME + "_" + "999th_percentile"), 300L, latenciesImpl);
070    tableMetrics.updatePut(tn2, 75L);
071    HELPER.assertGauge(MetricsTableLatenciesImpl.qualifyMetricsName(
072        tn2, MetricsTableLatencies.PUT_TIME + "_" + "99th_percentile"), 75L, latenciesImpl);
073  }
074}