1 // This file is part of OpenTSDB. 2 // Copyright (C) 2010-2012 The OpenTSDB Authors. 3 // 4 // This program is free software: you can redistribute it and/or modify it 5 // under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 2.1 of the License, or (at your 7 // option) any later version. This program is distributed in the hope that it 8 // will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 9 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License for more details. You should have received a copy 11 // of the GNU Lesser General Public License along with this program. If not, 12 // see <http://www.gnu.org/licenses/>. 13 package net.opentsdb.stats; 14 15 import junit.framework.TestCase; 16 17 public final class TestHistogram extends TestCase { 18 test_percentile_empty_histogram()19 public void test_percentile_empty_histogram() { 20 final Histogram histo = new Histogram(16000, (short) 2, 100); 21 assertEquals(0, histo.percentile(1)); 22 assertEquals(0, histo.percentile(50)); 23 assertEquals(0, histo.percentile(99)); 24 } 25 test_16Max_1Interval_5Cutoff()26 public void test_16Max_1Interval_5Cutoff() { 27 final Histogram histo = new Histogram(16, (short) 1, 5); 28 assertEquals(10, histo.buckets()); 29 30 histo.add(4); 31 assertBucketEquals(histo, 4, 1); 32 33 histo.add(5); 34 assertBucketEquals(histo, 5, 1); 35 36 histo.add(5); 37 assertBucketEquals(histo, 5, 2); 38 39 histo.add(0); 40 assertBucketEquals(histo, 0, 1); 41 42 histo.add(42); 43 assertBucketEquals(histo, 9, 1); 44 45 histo.add(6); 46 assertBucketEquals(histo, 5, 3); 47 48 histo.add(9); 49 assertBucketEquals(histo, 7, 1); 50 51 histo.add(10); 52 assertBucketEquals(histo, 7, 2); 53 54 assertBucketEquals(histo, 0, 1); 55 assertBucketEquals(histo, 1, 0); 56 assertBucketEquals(histo, 2, 0); 57 assertBucketEquals(histo, 3, 0); 58 assertBucketEquals(histo, 4, 1); 59 assertBucketEquals(histo, 5, 3); 60 assertBucketEquals(histo, 6, 0); 61 assertBucketEquals(histo, 7, 2); 62 assertBucketEquals(histo, 8, 0); 63 assertBucketEquals(histo, 9, 1); 64 } 65 test_16Max_2Interval_5Cutoff()66 public void test_16Max_2Interval_5Cutoff() { 67 final Histogram histo = new Histogram(16, (short) 2, 5); 68 assertEquals(6, histo.buckets()); 69 70 histo.add(4); 71 assertBucketEquals(histo, 2, 1); 72 73 histo.add(6); 74 assertBucketEquals(histo, 2, 2); 75 76 histo.add(7); 77 assertBucketEquals(histo, 2, 3); 78 79 histo.add(0); 80 assertBucketEquals(histo, 0, 1); 81 82 histo.add(42); 83 assertBucketEquals(histo, 5, 1); 84 85 histo.add(8); 86 assertBucketEquals(histo, 3, 1); 87 88 histo.add(9); 89 assertBucketEquals(histo, 3, 2); 90 91 histo.add(10); 92 assertBucketEquals(histo, 3, 3); 93 94 histo.add(11); 95 assertBucketEquals(histo, 3, 4); 96 97 histo.add(12); 98 assertBucketEquals(histo, 5, 1); 99 100 assertBucketEquals(histo, 0, 1); 101 assertBucketEquals(histo, 1, 0); 102 assertBucketEquals(histo, 2, 3); 103 assertBucketEquals(histo, 3, 4); 104 assertBucketEquals(histo, 4, 1); 105 assertBucketEquals(histo, 5, 1); 106 } 107 test_160Max_20Interval_50Cutoff()108 public void test_160Max_20Interval_50Cutoff() { 109 final Histogram histo = new Histogram(160, (short) 20, 50); 110 assertEquals(6, histo.buckets()); 111 112 histo.add(0); 113 assertBucketEquals(histo, 0, 1); 114 115 histo.add(40); 116 assertBucketEquals(histo, 2, 1); 117 118 histo.add(50); 119 assertBucketEquals(histo, 2, 2); 120 121 histo.add(60); 122 assertBucketEquals(histo, 2, 3); 123 124 histo.add(71); 125 assertBucketEquals(histo, 2, 4); 126 127 histo.add(72); 128 assertBucketEquals(histo, 3, 1); 129 130 histo.add(103); 131 assertBucketEquals(histo, 3, 2); 132 133 histo.add(104); 134 assertBucketEquals(histo, 4, 1); 135 136 histo.add(130); 137 assertBucketEquals(histo, 4, 2); 138 139 histo.add(160); 140 assertBucketEquals(histo, 4, 3); 141 142 histo.add(167); 143 assertBucketEquals(histo, 4, 4); 144 145 histo.add(168); 146 assertBucketEquals(histo, 5, 1); 147 148 histo.add(420); 149 assertBucketEquals(histo, 5, 2); 150 151 assertBucketEquals(histo, 0, 1); 152 assertBucketEquals(histo, 1, 0); 153 assertBucketEquals(histo, 2, 4); 154 assertBucketEquals(histo, 3, 2); 155 assertBucketEquals(histo, 4, 4); 156 assertBucketEquals(histo, 5, 2); 157 } 158 assertBucketEquals(final Histogram histo, final int bucket, final int expected)159 static void assertBucketEquals(final Histogram histo, 160 final int bucket, final int expected) { 161 int actual = histo.valueInBucket(bucket); 162 if (actual != expected) { 163 final StringBuilder buf = new StringBuilder(); 164 final int nbuckets = histo.buckets(); 165 for (int i = 0; i < nbuckets; i++) { 166 histo.printAsciiBucket(buf, i); 167 if (i == bucket) { 168 buf.setCharAt(buf.length() - 1, ' '); 169 buf.append(" <=== should have been ").append(expected).append('\n'); 170 } 171 } 172 fail("Bucket #" + bucket + " contains " + actual + " instead of " 173 + expected + "\nHistogram:\n" + buf); 174 } 175 } 176 printHisto(final Histogram histo)177 static void printHisto(final Histogram histo) { 178 final StringBuilder buf = new StringBuilder(); 179 histo.printAscii(buf); 180 System.err.println(buf); 181 } 182 183 } 184