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