View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  package org.apache.hadoop.hbase.metrics.impl;
22  
23  import static org.junit.Assert.assertEquals;
24  
25  
26  import org.apache.hadoop.hbase.metrics.Snapshot;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * Test case for {@link HistogramImpl}
33   */
34  @Category(SmallTests.class)
35  public class TestHistogramImpl {
36  
37    @Test
38    public void testUpdate() {
39      HistogramImpl histogram = new HistogramImpl();
40  
41      assertEquals(0, histogram.getCount());
42  
43      histogram.update(0);
44      assertEquals(1, histogram.getCount());
45  
46      histogram.update(10);
47      assertEquals(2, histogram.getCount());
48  
49      histogram.update(20);
50      histogram.update(30);
51  
52      assertEquals(4, histogram.getCount());
53    }
54  
55    @Test
56    public void testSnapshot() {
57      HistogramImpl histogram = new HistogramImpl();
58      for (int i = 0; i < 100; i++) {
59        histogram.update(i);
60      }
61  
62      Snapshot snapshot = histogram.snapshot();
63  
64      assertEquals(100, snapshot.getCount());
65      assertEquals(49, snapshot.getMedian());
66      assertEquals(49, snapshot.getMean());
67      assertEquals(0, snapshot.getMin());
68      assertEquals(99, snapshot.getMax());
69      assertEquals(24, snapshot.get25thPercentile());
70      assertEquals(74, snapshot.get75thPercentile());
71      assertEquals(89, snapshot.get90thPercentile());
72      assertEquals(94, snapshot.get95thPercentile());
73      assertEquals(97, snapshot.get98thPercentile());
74      assertEquals(98, snapshot.get99thPercentile());
75      assertEquals(98, snapshot.get999thPercentile());
76  
77      assertEquals(100, snapshot.getCountAtOrBelow(50));
78  
79      // check that histogram is reset.
80      assertEquals(100, histogram.getCount()); // count does not reset
81  
82      // put more data after reset
83      for (int i = 100; i < 200; i++) {
84        histogram.update(i);
85      }
86  
87      assertEquals(200, histogram.getCount());
88  
89      snapshot = histogram.snapshot();
90      assertEquals(100, snapshot.getCount()); // only 100 more events
91      assertEquals(150, snapshot.getMedian());
92      assertEquals(149, snapshot.getMean());
93      assertEquals(100, snapshot.getMin());
94      assertEquals(199, snapshot.getMax());
95      assertEquals(125, snapshot.get25thPercentile());
96      assertEquals(175, snapshot.get75thPercentile());
97      assertEquals(190, snapshot.get90thPercentile());
98      assertEquals(195, snapshot.get95thPercentile());
99      assertEquals(198, snapshot.get98thPercentile());
100     assertEquals(199, snapshot.get99thPercentile());
101     assertEquals(199, snapshot.get999thPercentile());
102 
103     for (int i = 500; i < 1000; i++) {
104       histogram.update(i);
105     }
106 
107     snapshot = histogram.snapshot();
108 
109     assertEquals(500, snapshot.getCount());
110     assertEquals(749, snapshot.getMedian());
111     assertEquals(749, snapshot.getMean());
112     assertEquals(500, snapshot.getMin());
113     assertEquals(999, snapshot.getMax());
114     assertEquals(624, snapshot.get25thPercentile());
115     assertEquals(874, snapshot.get75thPercentile());
116     assertEquals(949, snapshot.get90thPercentile());
117     assertEquals(974, snapshot.get95thPercentile());
118     assertEquals(989, snapshot.get98thPercentile());
119     assertEquals(994, snapshot.get99thPercentile());
120     assertEquals(998, snapshot.get999thPercentile());
121 
122   }
123 }