View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.util;
19  
20  import java.util.Arrays;
21  import java.util.Random;
22  
23  
24  import org.apache.hadoop.hbase.testclassification.SmallTests;
25  import org.junit.Assert;
26  import org.junit.Test;
27  import org.junit.experimental.categories.Category;
28  
29  import static org.junit.Assert.assertEquals;
30  
31  /**
32   * Testcases for FastLongHistogram.
33   */
34  @Category(SmallTests.class)
35  public class TestFastLongHistogram {
36  
37    private static void doTestUniform(FastLongHistogram hist) {
38      long[] VALUES = { 0, 10, 20, 30, 40, 50 };
39      double[] qs = new double[VALUES.length];
40      for (int i = 0; i < qs.length; i++) {
41        qs[i] = (double) VALUES[i] / VALUES[VALUES.length - 1];
42      }
43  
44      for (int i = 0; i < 10; i++) {
45        for (long v : VALUES) {
46          hist.add(v, 1);
47        }
48        long[] vals = hist.getQuantiles(qs);
49        System.out.println(Arrays.toString(vals));
50        for (int j = 0; j < qs.length; j++) {
51          Assert.assertTrue(j + "-th element org: " + VALUES[j] + ", act: " + vals[j],
52            Math.abs(vals[j] - VALUES[j]) <= 10);
53        }
54        hist.reset();
55      }
56    }
57  
58    @Test
59    public void testUniform() {
60      FastLongHistogram hist = new FastLongHistogram(100, 0, 50);
61      doTestUniform(hist);
62    }
63  
64    @Test
65    public void testAdaptionOfChange() {
66      // assumes the uniform distribution
67      FastLongHistogram hist = new FastLongHistogram(100, 0, 100);
68  
69      Random rand = new Random();
70  
71      for (int n = 0; n < 10; n++) {
72        for (int i = 0; i < 900; i++) {
73          hist.add(rand.nextInt(100), 1);
74        }
75  
76        // add 10% outliers, this breaks the assumption, hope bin10xMax works
77        for (int i = 0; i < 100; i++) {
78          hist.add(1000 + rand.nextInt(100), 1);
79        }
80  
81        long[] vals = hist.getQuantiles(new double[] { 0.25, 0.75, 0.95 });
82        System.out.println(Arrays.toString(vals));
83        if (n == 0) {
84          Assert.assertTrue("Out of possible value", vals[0] >= 0 && vals[0] <= 50);
85          Assert.assertTrue("Out of possible value", vals[1] >= 50 && vals[1] <= 100);
86          Assert.assertTrue("Out of possible value", vals[2] >= 900 && vals[2] <= 1100);
87        }
88  
89        hist.reset();
90      }
91    }
92  
93  
94    @Test
95    public void testGetNumAtOrBelow() {
96      long[] VALUES = { 1, 10, 20, 30, 40, 50 };
97  
98      FastLongHistogram h = new FastLongHistogram();
99      for (long v : VALUES) {
100       for (int i = 0; i < 100; i++) {
101         h.add(v, 1);
102       }
103     }
104 
105     h.add(Integer.MAX_VALUE, 1);
106 
107     h.reset();
108 
109     for (long v : VALUES) {
110       for (int i = 0; i < 100; i++) {
111         h.add(v, 1);
112       }
113     }
114     // Add something way out there to make sure it doesn't throw off the counts.
115     h.add(Integer.MAX_VALUE, 1);
116 
117     assertEquals(100, h.getNumAtOrBelow(1));
118     assertEquals(200, h.getNumAtOrBelow(11));
119     assertEquals(601, h.getNumAtOrBelow(Long.MAX_VALUE));
120   }
121 
122 
123   @Test
124   public void testSameValues() {
125     FastLongHistogram hist = new FastLongHistogram(100);
126 
127     hist.add(50, 100);
128 
129     hist.reset();
130     doTestUniform(hist);
131   }
132 }