Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 1 | #include "gtest/gtest.h" |
| 2 | #include "histogram-inl.h" |
| 3 | #include <sstream> |
| 4 | using namespace art; |
| 5 | |
| 6 | //Simple usage: |
| 7 | // Histogram *hist = new Histogram("SimplePercentiles"); |
| 8 | // Percentile PerValue |
| 9 | // hist->AddValue(121); |
| 10 | // hist->AddValue(132); |
| 11 | // hist->AddValue(140); |
| 12 | // hist->AddValue(145); |
| 13 | // hist->AddValue(155); |
| 14 | // hist->CreateHistogram(); |
| 15 | // PerValue = hist->PercentileVal(0.50); finds the 50th percentile(median). |
| 16 | |
| 17 | TEST(Histtest, MeanTest) { |
| 18 | |
| 19 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("MeanTest"); |
| 20 | double mean; |
| 21 | for (size_t Idx = 0; Idx < 90; Idx++) { |
| 22 | hist->AddValue(static_cast<uint64_t>(50)); |
| 23 | } |
| 24 | mean = hist->Mean(); |
| 25 | EXPECT_EQ(mean, 50); |
| 26 | hist->Reset(); |
| 27 | hist->AddValue(9); |
| 28 | hist->AddValue(17); |
| 29 | hist->AddValue(28); |
| 30 | hist->AddValue(28); |
| 31 | mean = hist->Mean(); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 32 | EXPECT_EQ(20.5, mean); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 33 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 34 | |
| 35 | TEST(Histtest, VarianceTest) { |
| 36 | |
| 37 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("VarianceTest"); |
| 38 | double variance; |
| 39 | hist->AddValue(9); |
| 40 | hist->AddValue(17); |
| 41 | hist->AddValue(28); |
| 42 | hist->AddValue(28); |
| 43 | hist->CreateHistogram(); |
| 44 | variance = hist->Variance(); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 45 | EXPECT_EQ(64.25, variance); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 46 | delete hist; |
| 47 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 48 | |
| 49 | TEST(Histtest, Percentile) { |
| 50 | |
| 51 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("Percentile"); |
| 52 | double PerValue; |
| 53 | |
| 54 | hist->AddValue(20); |
| 55 | hist->AddValue(31); |
| 56 | hist->AddValue(42); |
| 57 | hist->AddValue(50); |
| 58 | hist->AddValue(60); |
| 59 | hist->AddValue(70); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 60 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 61 | hist->AddValue(98); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 62 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 63 | hist->AddValue(110); |
| 64 | hist->AddValue(121); |
| 65 | hist->AddValue(132); |
| 66 | hist->AddValue(140); |
| 67 | hist->AddValue(145); |
| 68 | hist->AddValue(155); |
| 69 | |
| 70 | hist->CreateHistogram(); |
| 71 | PerValue = hist->Percentile(0.50); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 72 | EXPECT_EQ(875, static_cast<int>(PerValue * 10)); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 73 | |
| 74 | delete hist; |
| 75 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 76 | |
| 77 | TEST(Histtest, UpdateRange) { |
| 78 | |
| 79 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("UpdateRange"); |
| 80 | double PerValue; |
| 81 | |
| 82 | hist->AddValue(15); |
| 83 | hist->AddValue(17); |
| 84 | hist->AddValue(35); |
| 85 | hist->AddValue(50); |
| 86 | hist->AddValue(68); |
| 87 | hist->AddValue(75); |
| 88 | hist->AddValue(93); |
| 89 | hist->AddValue(110); |
| 90 | hist->AddValue(121); |
| 91 | hist->AddValue(132); |
| 92 | hist->AddValue(140); //Median value |
| 93 | hist->AddValue(145); |
| 94 | hist->AddValue(155); |
| 95 | hist->AddValue(163); |
| 96 | hist->AddValue(168); |
| 97 | hist->AddValue(175); |
| 98 | hist->AddValue(182); |
| 99 | hist->AddValue(193); |
| 100 | hist->AddValue(200); |
| 101 | hist->AddValue(205); |
| 102 | hist->AddValue(212); |
| 103 | hist->CreateHistogram(); |
| 104 | PerValue = hist->Percentile(0.50); |
| 105 | |
| 106 | std::string text; |
| 107 | std::stringstream stream; |
| 108 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 109 | "UpdateRange:\t99% C.I. 15us-212us Avg: 126.380us Max: 212us\n"; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 110 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 111 | |
| 112 | EXPECT_EQ(expected, stream.str()); |
| 113 | EXPECT_GE(PerValue, 132); |
| 114 | EXPECT_LE(PerValue, 145); |
| 115 | |
| 116 | delete hist; |
| 117 | } |
| 118 | ; |
| 119 | |
| 120 | TEST(Histtest, Reset) { |
| 121 | |
| 122 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("Reset"); |
| 123 | double PerValue; |
| 124 | hist->AddValue(0); |
| 125 | hist->AddValue(189); |
| 126 | hist->AddValue(389); |
| 127 | hist->Reset(); |
| 128 | hist->AddValue(15); |
| 129 | hist->AddValue(17); |
| 130 | hist->AddValue(35); |
| 131 | hist->AddValue(50); |
| 132 | hist->AddValue(68); |
| 133 | hist->AddValue(75); |
| 134 | hist->AddValue(93); |
| 135 | hist->AddValue(110); |
| 136 | hist->AddValue(121); |
| 137 | hist->AddValue(132); |
| 138 | hist->AddValue(140); //Median value |
| 139 | hist->AddValue(145); |
| 140 | hist->AddValue(155); |
| 141 | hist->AddValue(163); |
| 142 | hist->AddValue(168); |
| 143 | hist->AddValue(175); |
| 144 | hist->AddValue(182); |
| 145 | hist->AddValue(193); |
| 146 | hist->AddValue(200); |
| 147 | hist->AddValue(205); |
| 148 | hist->AddValue(212); |
| 149 | hist->CreateHistogram(); |
| 150 | PerValue = hist->Percentile(0.50); |
| 151 | |
| 152 | std::string text; |
| 153 | std::stringstream stream; |
| 154 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 155 | "Reset:\t99% C.I. 15us-212us Avg: 126.380us Max: 212us\n"; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 156 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 157 | |
| 158 | EXPECT_EQ(expected, stream.str()); |
| 159 | EXPECT_GE(PerValue, 132); |
| 160 | EXPECT_LE(PerValue, 145); |
| 161 | |
| 162 | delete hist; |
| 163 | } |
| 164 | ; |
| 165 | |
| 166 | TEST(Histtest, MultipleCreateHist) { |
| 167 | |
| 168 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("MultipleCreateHist"); |
| 169 | double PerValue; |
| 170 | hist->AddValue(15); |
| 171 | hist->AddValue(17); |
| 172 | hist->AddValue(35); |
| 173 | hist->AddValue(50); |
| 174 | hist->AddValue(68); |
| 175 | hist->AddValue(75); |
| 176 | hist->AddValue(93); |
| 177 | hist->CreateHistogram(); |
| 178 | hist->AddValue(110); |
| 179 | hist->AddValue(121); |
| 180 | hist->AddValue(132); |
| 181 | hist->AddValue(140); //Median value |
| 182 | hist->AddValue(145); |
| 183 | hist->AddValue(155); |
| 184 | hist->AddValue(163); |
| 185 | hist->AddValue(168); |
| 186 | hist->CreateHistogram(); |
| 187 | hist->AddValue(175); |
| 188 | hist->AddValue(182); |
| 189 | hist->AddValue(193); |
| 190 | hist->AddValue(200); |
| 191 | hist->AddValue(205); |
| 192 | hist->AddValue(212); |
| 193 | hist->CreateHistogram(); |
| 194 | PerValue = hist->Percentile(0.50); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 195 | std::stringstream stream; |
| 196 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 197 | "MultipleCreateHist:\t99% C.I. 15us-212us Avg: 126.380us Max: 212us\n"; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 198 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 199 | |
| 200 | EXPECT_EQ(expected, stream.str()); |
| 201 | EXPECT_GE(PerValue, 132); |
| 202 | EXPECT_LE(PerValue, 145); |
| 203 | |
| 204 | delete hist; |
| 205 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 206 | |
| 207 | TEST(Histtest, SingleValue) { |
| 208 | |
| 209 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("SingleValue"); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 210 | hist->AddValue(1); |
| 211 | hist->CreateHistogram(); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 212 | std::stringstream stream; |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 213 | std::string expected = "SingleValue:\t99% C.I. 1us-1us Avg: 1us Max: 1us\n"; |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 214 | hist->PrintConfidenceIntervals(stream, 0.99); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 215 | EXPECT_EQ(expected, stream.str()); |
| 216 | delete hist; |
| 217 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 218 | |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 219 | TEST(Histtest, CappingPercentiles) { |
| 220 | |
| 221 | double per_995; |
| 222 | double per_005; |
| 223 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("CappingPercentiles"); |
| 224 | // All values are similar. |
| 225 | for (uint64_t idx = 0ull; idx < 150ull; idx++) { |
| 226 | hist->AddValue(0); |
| 227 | } |
| 228 | hist->CreateHistogram(); |
| 229 | per_995 = hist->Percentile(0.995); |
| 230 | EXPECT_EQ(per_995, 0); |
| 231 | hist->Reset(); |
| 232 | for (size_t idx = 0; idx < 200; idx++) { |
| 233 | for (uint64_t val = 1ull; val <= 4ull; val++) { |
| 234 | hist->AddValue(val); |
| 235 | } |
| 236 | } |
| 237 | hist->CreateHistogram(); |
| 238 | per_005 = hist->Percentile(0.005); |
| 239 | per_995 = hist->Percentile(0.995); |
| 240 | EXPECT_EQ(1, per_005); |
| 241 | EXPECT_EQ(4, per_995); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 242 | delete hist; |
| 243 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 244 | |
| 245 | TEST(Histtest, SpikyValues) { |
| 246 | |
| 247 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("SpikyValues"); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 248 | |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 249 | for (uint64_t idx = 0ull; idx < 30ull; idx++) { |
| 250 | for (uint64_t idx_inner = 0ull; idx_inner < 5ull; idx_inner++) { |
| 251 | hist->AddValue(idx * idx_inner); |
| 252 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 253 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 254 | hist->AddValue(10000); |
| 255 | hist->CreateHistogram(); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 256 | std::stringstream stream; |
| 257 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 258 | "SpikyValues:\t99% C.I. 0.089us-2541.825us Avg: 95.033us Max: 10000us\n"; |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 259 | hist->PrintConfidenceIntervals(stream, 0.99); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame^] | 260 | EXPECT_EQ(expected, stream.str()); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 261 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 262 | delete hist; |
| 263 | } |