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