View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.metrics2.lib;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import org.apache.hadoop.hbase.testclassification.MetricsTests;
24  import org.apache.hadoop.hbase.testclassification.SmallTests;
25  import org.apache.hadoop.metrics2.AbstractMetric;
26  import org.apache.hadoop.metrics2.MetricsRecord;
27  import org.apache.hadoop.metrics2.impl.MetricsCollectorImpl;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  @Category({ MetricsTests.class, SmallTests.class })
35  public class TestMutableRangeHistogram {
36  
37    private static final String RECORD_NAME = "test";
38    private static final String SIZE_HISTOGRAM_NAME = "TestSize";
39  
40    /**
41     * calculate the distribution for last bucket, see HBASE-24615 for detail.
42     */
43    @Test
44    public void testLastBucketWithSizeHistogram() {
45      // create and init histogram minValue and maxValue
46      MetricsCollectorImpl collector = new MetricsCollectorImpl();
47      MutableSizeHistogram histogram = new MutableSizeHistogram(SIZE_HISTOGRAM_NAME, "");
48      long[] ranges = histogram.getRanges();
49      int len = ranges.length;
50      histogram.add(0L);
51      histogram.add(ranges[len - 1]);
52      histogram.snapshot(collector.addRecord(RECORD_NAME), true);
53      collector.clear();
54  
55      // fill up values and snapshot
56      histogram.add(ranges[len - 2] * 2);
57      histogram.add(ranges[len - 1] * 2);
58      histogram.snapshot(collector.addRecord(RECORD_NAME), true);
59      List<? extends MetricsRecord> records = collector.getRecords();
60      assertEquals(1, records.size());
61      MetricsRecord record = records.iterator().next();
62      assertEquals(RECORD_NAME, record.name());
63  
64      // get size range metrics
65      String histogramMetricPrefix = SIZE_HISTOGRAM_NAME + "_" + histogram.getRangeType();
66      List<AbstractMetric> metrics = new ArrayList<>();
67      for (AbstractMetric metric : record.metrics()) {
68        if (metric.name().startsWith(histogramMetricPrefix)) {
69          metrics.add(metric);
70        }
71      }
72      assertEquals(2, metrics.size());
73  
74      // check range [10000000,100000000]
75      String metricName = histogramMetricPrefix + "_" + ranges[len - 2] + "-" + ranges[len - 1];
76      assertEquals(metricName, metrics.get(0).name());
77      assertEquals(1, metrics.get(0).value().longValue());
78  
79      // check range [100000000, inf]
80      metricName = histogramMetricPrefix + "_" + ranges[len - 1] + "-inf";
81      assertEquals(metricName, metrics.get(1).name(), metricName);
82      assertEquals(1, metrics.get(1).value().longValue());
83    }
84  }