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