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(); |
| 32 | EXPECT_EQ(mean, 20.5); |
| 33 | } |
| 34 | ; |
| 35 | |
| 36 | TEST(Histtest, VarianceTest) { |
| 37 | |
| 38 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("VarianceTest"); |
| 39 | double variance; |
| 40 | hist->AddValue(9); |
| 41 | hist->AddValue(17); |
| 42 | hist->AddValue(28); |
| 43 | hist->AddValue(28); |
| 44 | hist->CreateHistogram(); |
| 45 | variance = hist->Variance(); |
| 46 | EXPECT_EQ(variance, 64.25); |
| 47 | delete hist; |
| 48 | } |
| 49 | ; |
| 50 | |
| 51 | TEST(Histtest, Percentile) { |
| 52 | |
| 53 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("Percentile"); |
| 54 | double PerValue; |
| 55 | |
| 56 | hist->AddValue(20); |
| 57 | hist->AddValue(31); |
| 58 | hist->AddValue(42); |
| 59 | hist->AddValue(50); |
| 60 | hist->AddValue(60); |
| 61 | hist->AddValue(70); |
| 62 | hist->AddValue(98); |
| 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); |
| 72 | EXPECT_GE(PerValue, 70); |
| 73 | EXPECT_LE(PerValue, 110); |
| 74 | |
| 75 | delete hist; |
| 76 | } |
| 77 | ; |
| 78 | |
| 79 | TEST(Histtest, UpdateRange) { |
| 80 | |
| 81 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("UpdateRange"); |
| 82 | double PerValue; |
| 83 | |
| 84 | hist->AddValue(15); |
| 85 | hist->AddValue(17); |
| 86 | hist->AddValue(35); |
| 87 | hist->AddValue(50); |
| 88 | hist->AddValue(68); |
| 89 | hist->AddValue(75); |
| 90 | hist->AddValue(93); |
| 91 | hist->AddValue(110); |
| 92 | hist->AddValue(121); |
| 93 | hist->AddValue(132); |
| 94 | hist->AddValue(140); //Median value |
| 95 | hist->AddValue(145); |
| 96 | hist->AddValue(155); |
| 97 | hist->AddValue(163); |
| 98 | hist->AddValue(168); |
| 99 | hist->AddValue(175); |
| 100 | hist->AddValue(182); |
| 101 | hist->AddValue(193); |
| 102 | hist->AddValue(200); |
| 103 | hist->AddValue(205); |
| 104 | hist->AddValue(212); |
| 105 | hist->CreateHistogram(); |
| 106 | PerValue = hist->Percentile(0.50); |
| 107 | |
| 108 | std::string text; |
| 109 | std::stringstream stream; |
| 110 | std::string expected = |
| 111 | "UpdateRange:\t0.99% C.I. 15.262us-214.475us Avg: 126.380us Max: 212us\n"; |
| 112 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 113 | |
| 114 | EXPECT_EQ(expected, stream.str()); |
| 115 | EXPECT_GE(PerValue, 132); |
| 116 | EXPECT_LE(PerValue, 145); |
| 117 | |
| 118 | delete hist; |
| 119 | } |
| 120 | ; |
| 121 | |
| 122 | TEST(Histtest, Reset) { |
| 123 | |
| 124 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("Reset"); |
| 125 | double PerValue; |
| 126 | hist->AddValue(0); |
| 127 | hist->AddValue(189); |
| 128 | hist->AddValue(389); |
| 129 | hist->Reset(); |
| 130 | hist->AddValue(15); |
| 131 | hist->AddValue(17); |
| 132 | hist->AddValue(35); |
| 133 | hist->AddValue(50); |
| 134 | hist->AddValue(68); |
| 135 | hist->AddValue(75); |
| 136 | hist->AddValue(93); |
| 137 | hist->AddValue(110); |
| 138 | hist->AddValue(121); |
| 139 | hist->AddValue(132); |
| 140 | hist->AddValue(140); //Median value |
| 141 | hist->AddValue(145); |
| 142 | hist->AddValue(155); |
| 143 | hist->AddValue(163); |
| 144 | hist->AddValue(168); |
| 145 | hist->AddValue(175); |
| 146 | hist->AddValue(182); |
| 147 | hist->AddValue(193); |
| 148 | hist->AddValue(200); |
| 149 | hist->AddValue(205); |
| 150 | hist->AddValue(212); |
| 151 | hist->CreateHistogram(); |
| 152 | PerValue = hist->Percentile(0.50); |
| 153 | |
| 154 | std::string text; |
| 155 | std::stringstream stream; |
| 156 | std::string expected = |
| 157 | "Reset:\t0.99% C.I. 15.262us-214.475us Avg: 126.380us Max: 212us\n"; |
| 158 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 159 | |
| 160 | EXPECT_EQ(expected, stream.str()); |
| 161 | EXPECT_GE(PerValue, 132); |
| 162 | EXPECT_LE(PerValue, 145); |
| 163 | |
| 164 | delete hist; |
| 165 | } |
| 166 | ; |
| 167 | |
| 168 | TEST(Histtest, MultipleCreateHist) { |
| 169 | |
| 170 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("MultipleCreateHist"); |
| 171 | double PerValue; |
| 172 | hist->AddValue(15); |
| 173 | hist->AddValue(17); |
| 174 | hist->AddValue(35); |
| 175 | hist->AddValue(50); |
| 176 | hist->AddValue(68); |
| 177 | hist->AddValue(75); |
| 178 | hist->AddValue(93); |
| 179 | hist->CreateHistogram(); |
| 180 | hist->AddValue(110); |
| 181 | hist->AddValue(121); |
| 182 | hist->AddValue(132); |
| 183 | hist->AddValue(140); //Median value |
| 184 | hist->AddValue(145); |
| 185 | hist->AddValue(155); |
| 186 | hist->AddValue(163); |
| 187 | hist->AddValue(168); |
| 188 | hist->CreateHistogram(); |
| 189 | hist->AddValue(175); |
| 190 | hist->AddValue(182); |
| 191 | hist->AddValue(193); |
| 192 | hist->AddValue(200); |
| 193 | hist->AddValue(205); |
| 194 | hist->AddValue(212); |
| 195 | hist->CreateHistogram(); |
| 196 | PerValue = hist->Percentile(0.50); |
| 197 | |
| 198 | std::string text; |
| 199 | std::stringstream stream; |
| 200 | std::string expected = |
| 201 | "MultipleCreateHist:\t0.99% C.I. 15.262us-214.475us Avg: 126.380us Max: 212us\n"; |
| 202 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 203 | |
| 204 | EXPECT_EQ(expected, stream.str()); |
| 205 | EXPECT_GE(PerValue, 132); |
| 206 | EXPECT_LE(PerValue, 145); |
| 207 | |
| 208 | delete hist; |
| 209 | } |
| 210 | ; |
| 211 | |
| 212 | TEST(Histtest, SingleValue) { |
| 213 | |
| 214 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("SingleValue"); |
| 215 | double PerValue_10; |
| 216 | double PerValue_90; |
| 217 | |
| 218 | hist->AddValue(1); |
| 219 | hist->CreateHistogram(); |
| 220 | PerValue_10 = hist->Percentile(0.1); |
| 221 | PerValue_90 = hist->Percentile(0.9); |
| 222 | |
| 223 | EXPECT_GT(PerValue_10, 0); |
| 224 | EXPECT_LT(PerValue_90, 5); |
| 225 | |
| 226 | delete hist; |
| 227 | } |
| 228 | ; |
| 229 | |
| 230 | TEST(Histtest, SpikyValues) { |
| 231 | |
| 232 | Histogram<uint64_t> *hist = new Histogram<uint64_t>("SpikyValues"); |
| 233 | double PerValue_005; |
| 234 | double PerValue_995; |
| 235 | |
| 236 | for (size_t Idx = 0; Idx < 300; Idx++) { |
| 237 | hist->AddValue(rand() % 200); |
| 238 | } |
| 239 | |
| 240 | hist->AddValue(10000); |
| 241 | hist->CreateHistogram(); |
| 242 | PerValue_005 = hist->Percentile(0.005); |
| 243 | PerValue_995 = hist->Percentile(0.995); |
| 244 | |
| 245 | EXPECT_EQ(1.075, PerValue_005); |
| 246 | EXPECT_EQ(199747, static_cast<int>(PerValue_995 * 1000)); |
| 247 | delete hist; |
| 248 | } |
| 249 | ; |
| 250 | |
| 251 | |
| 252 | |