1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.metrics.impl;
19
20 import static org.junit.Assert.assertEquals;
21
22 import java.util.Arrays;
23 import java.util.Random;
24
25
26 import org.apache.hadoop.hbase.testclassification.SmallTests;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31
32
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.snapshotAndReset();
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
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
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.snapshotAndReset();
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.snapshotAndReset();
108
109 for (long v : VALUES) {
110 for (int i = 0; i < 100; i++) {
111 h.add(v, 1);
112 }
113 }
114
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.snapshotAndReset();
130 doTestUniform(hist);
131 }
132 }