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.metrics.impl;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.stream.IntStream;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.metrics.Snapshot;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029
030/**
031 * Test case for {@link HistogramImpl}
032 */
033@Category(SmallTests.class)
034public class TestHistogramImpl {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestHistogramImpl.class);
039
040  @Test
041  public void testUpdate() {
042    HistogramImpl histogram = new HistogramImpl();
043
044    assertEquals(0, histogram.getCount());
045
046    histogram.update(0);
047    assertEquals(1, histogram.getCount());
048
049    histogram.update(10);
050    assertEquals(2, histogram.getCount());
051
052    histogram.update(20);
053    histogram.update(30);
054
055    assertEquals(4, histogram.getCount());
056  }
057
058  @Test
059  public void testSnapshot() {
060    HistogramImpl histogram = new HistogramImpl();
061    IntStream.range(0, 100).forEach(histogram::update);
062
063    Snapshot snapshot = histogram.snapshot();
064
065    assertEquals(100, snapshot.getCount());
066    assertEquals(49, snapshot.getMedian());
067    assertEquals(49, snapshot.getMean());
068    assertEquals(0, snapshot.getMin());
069    assertEquals(99, snapshot.getMax());
070    assertEquals(24, snapshot.get25thPercentile());
071    assertEquals(74, snapshot.get75thPercentile());
072    assertEquals(89, snapshot.get90thPercentile());
073    assertEquals(94, snapshot.get95thPercentile());
074    assertEquals(97, snapshot.get98thPercentile());
075    assertEquals(98, snapshot.get99thPercentile());
076    assertEquals(98, snapshot.get999thPercentile());
077
078    assertEquals(100, snapshot.getCountAtOrBelow(50));
079
080    // check that histogram is reset.
081    assertEquals(100, histogram.getCount()); // count does not reset
082
083    // put more data after reset
084    IntStream.range(100, 200).forEach(histogram::update);
085
086    assertEquals(200, histogram.getCount());
087
088    snapshot = histogram.snapshot();
089    assertEquals(100, snapshot.getCount()); // only 100 more events
090    assertEquals(150, snapshot.getMedian());
091    assertEquals(149, snapshot.getMean());
092    assertEquals(100, snapshot.getMin());
093    assertEquals(199, snapshot.getMax());
094    assertEquals(125, snapshot.get25thPercentile());
095    assertEquals(175, snapshot.get75thPercentile());
096    assertEquals(190, snapshot.get90thPercentile());
097    assertEquals(195, snapshot.get95thPercentile());
098    assertEquals(198, snapshot.get98thPercentile());
099    assertEquals(199, snapshot.get99thPercentile());
100    assertEquals(199, snapshot.get999thPercentile());
101
102    IntStream.range(500, 1000).forEach(histogram::update);
103
104    snapshot = histogram.snapshot();
105
106    assertEquals(500, snapshot.getCount());
107    assertEquals(749, snapshot.getMedian());
108    assertEquals(749, snapshot.getMean());
109    assertEquals(500, snapshot.getMin());
110    assertEquals(999, snapshot.getMax());
111    assertEquals(624, snapshot.get25thPercentile());
112    assertEquals(874, snapshot.get75thPercentile());
113    assertEquals(949, snapshot.get90thPercentile());
114    assertEquals(974, snapshot.get95thPercentile());
115    assertEquals(989, snapshot.get98thPercentile());
116    assertEquals(994, snapshot.get99thPercentile());
117    assertEquals(998, snapshot.get999thPercentile());
118
119  }
120}