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