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 */
018
019package org.apache.hadoop.metrics2.lib;
020
021import static org.junit.Assert.assertEquals;
022
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.testclassification.MetricsTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.apache.hadoop.metrics2.AbstractMetric;
027import org.apache.hadoop.metrics2.MetricsRecord;
028import org.apache.hadoop.metrics2.impl.MetricsCollectorImpl;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033import java.util.ArrayList;
034import java.util.List;
035
036@Category({ MetricsTests.class, SmallTests.class })
037public class TestMutableRangeHistogram {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestMutableRangeHistogram.class);
042
043  private static final String RECORD_NAME = "test";
044  private static final String SIZE_HISTOGRAM_NAME = "TestSize";
045
046  /**
047   * calculate the distribution for last bucket, see HBASE-24615 for detail.
048   */
049  @Test
050  public void testLastBucketWithSizeHistogram() {
051    // create and init histogram minValue and maxValue
052    MetricsCollectorImpl collector = new MetricsCollectorImpl();
053    MutableSizeHistogram histogram = new MutableSizeHistogram(SIZE_HISTOGRAM_NAME, "");
054    long[] ranges = histogram.getRanges();
055    int len = ranges.length;
056    histogram.add(0L);
057    histogram.add(ranges[len - 1]);
058    histogram.snapshot(collector.addRecord(RECORD_NAME), true);
059    collector.clear();
060
061    // fill up values and snapshot
062    histogram.add(ranges[len - 2] * 2);
063    histogram.add(ranges[len - 1] * 2);
064    histogram.snapshot(collector.addRecord(RECORD_NAME), true);
065    List<? extends MetricsRecord> records = collector.getRecords();
066    assertEquals(1, records.size());
067    MetricsRecord record = records.iterator().next();
068    assertEquals(RECORD_NAME, record.name());
069
070    // get size range metrics
071    String histogramMetricPrefix = SIZE_HISTOGRAM_NAME + "_" + histogram.getRangeType();
072    List<AbstractMetric> metrics = new ArrayList<>();
073    for (AbstractMetric metric : record.metrics()) {
074      if (metric.name().startsWith(histogramMetricPrefix)) {
075        metrics.add(metric);
076      }
077    }
078    assertEquals(2, metrics.size());
079
080    // check range [10000000,100000000]
081    String metricName = histogramMetricPrefix + "_" + ranges[len - 2] + "-" + ranges[len - 1];
082    assertEquals(metricName, metrics.get(0).name());
083    assertEquals(1, metrics.get(0).value().longValue());
084
085    // check range [100000000, inf]
086    metricName = histogramMetricPrefix + "_" + ranges[len - 1] + "-inf";
087    assertEquals(metricName, metrics.get(1).name(), metricName);
088    assertEquals(1, metrics.get(1).value().longValue());
089  }
090}