Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
| 18 | #include "histogram-inl.h" |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 19 | #include "UniquePtr.h" |
| 20 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 21 | #include <sstream> |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 22 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 23 | using namespace art; |
| 24 | |
| 25 | //Simple usage: |
| 26 | // Histogram *hist = new Histogram("SimplePercentiles"); |
| 27 | // Percentile PerValue |
| 28 | // hist->AddValue(121); |
| 29 | // hist->AddValue(132); |
| 30 | // hist->AddValue(140); |
| 31 | // hist->AddValue(145); |
| 32 | // hist->AddValue(155); |
| 33 | // hist->CreateHistogram(); |
| 34 | // PerValue = hist->PercentileVal(0.50); finds the 50th percentile(median). |
| 35 | |
| 36 | TEST(Histtest, MeanTest) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 37 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("MeanTest")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 38 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 39 | double mean; |
| 40 | for (size_t Idx = 0; Idx < 90; Idx++) { |
| 41 | hist->AddValue(static_cast<uint64_t>(50)); |
| 42 | } |
| 43 | mean = hist->Mean(); |
| 44 | EXPECT_EQ(mean, 50); |
| 45 | hist->Reset(); |
| 46 | hist->AddValue(9); |
| 47 | hist->AddValue(17); |
| 48 | hist->AddValue(28); |
| 49 | hist->AddValue(28); |
| 50 | mean = hist->Mean(); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 51 | EXPECT_EQ(20.5, mean); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 52 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 53 | |
| 54 | TEST(Histtest, VarianceTest) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 55 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("VarianceTest")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 56 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 57 | double variance; |
| 58 | hist->AddValue(9); |
| 59 | hist->AddValue(17); |
| 60 | hist->AddValue(28); |
| 61 | hist->AddValue(28); |
| 62 | hist->CreateHistogram(); |
| 63 | variance = hist->Variance(); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 64 | EXPECT_EQ(64.25, variance); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 65 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 66 | |
| 67 | TEST(Histtest, Percentile) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 68 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("Percentile")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 69 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 70 | double PerValue; |
| 71 | |
| 72 | hist->AddValue(20); |
| 73 | hist->AddValue(31); |
| 74 | hist->AddValue(42); |
| 75 | hist->AddValue(50); |
| 76 | hist->AddValue(60); |
| 77 | hist->AddValue(70); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 78 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 79 | hist->AddValue(98); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 80 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 81 | hist->AddValue(110); |
| 82 | hist->AddValue(121); |
| 83 | hist->AddValue(132); |
| 84 | hist->AddValue(140); |
| 85 | hist->AddValue(145); |
| 86 | hist->AddValue(155); |
| 87 | |
| 88 | hist->CreateHistogram(); |
| 89 | PerValue = hist->Percentile(0.50); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 90 | EXPECT_EQ(875, static_cast<int>(PerValue * 10)); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 91 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 92 | |
| 93 | TEST(Histtest, UpdateRange) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 94 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("UpdateRange")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 95 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 96 | double PerValue; |
| 97 | |
| 98 | hist->AddValue(15); |
| 99 | hist->AddValue(17); |
| 100 | hist->AddValue(35); |
| 101 | hist->AddValue(50); |
| 102 | hist->AddValue(68); |
| 103 | hist->AddValue(75); |
| 104 | hist->AddValue(93); |
| 105 | hist->AddValue(110); |
| 106 | hist->AddValue(121); |
| 107 | hist->AddValue(132); |
| 108 | hist->AddValue(140); //Median value |
| 109 | hist->AddValue(145); |
| 110 | hist->AddValue(155); |
| 111 | hist->AddValue(163); |
| 112 | hist->AddValue(168); |
| 113 | hist->AddValue(175); |
| 114 | hist->AddValue(182); |
| 115 | hist->AddValue(193); |
| 116 | hist->AddValue(200); |
| 117 | hist->AddValue(205); |
| 118 | hist->AddValue(212); |
| 119 | hist->CreateHistogram(); |
| 120 | PerValue = hist->Percentile(0.50); |
| 121 | |
| 122 | std::string text; |
| 123 | std::stringstream stream; |
| 124 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 125 | "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] | 126 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 127 | |
| 128 | EXPECT_EQ(expected, stream.str()); |
| 129 | EXPECT_GE(PerValue, 132); |
| 130 | EXPECT_LE(PerValue, 145); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 131 | } |
| 132 | ; |
| 133 | |
| 134 | TEST(Histtest, Reset) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 135 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("Reset")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 136 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 137 | double PerValue; |
| 138 | hist->AddValue(0); |
| 139 | hist->AddValue(189); |
| 140 | hist->AddValue(389); |
| 141 | hist->Reset(); |
| 142 | hist->AddValue(15); |
| 143 | hist->AddValue(17); |
| 144 | hist->AddValue(35); |
| 145 | hist->AddValue(50); |
| 146 | hist->AddValue(68); |
| 147 | hist->AddValue(75); |
| 148 | hist->AddValue(93); |
| 149 | hist->AddValue(110); |
| 150 | hist->AddValue(121); |
| 151 | hist->AddValue(132); |
| 152 | hist->AddValue(140); //Median value |
| 153 | hist->AddValue(145); |
| 154 | hist->AddValue(155); |
| 155 | hist->AddValue(163); |
| 156 | hist->AddValue(168); |
| 157 | hist->AddValue(175); |
| 158 | hist->AddValue(182); |
| 159 | hist->AddValue(193); |
| 160 | hist->AddValue(200); |
| 161 | hist->AddValue(205); |
| 162 | hist->AddValue(212); |
| 163 | hist->CreateHistogram(); |
| 164 | PerValue = hist->Percentile(0.50); |
| 165 | |
| 166 | std::string text; |
| 167 | std::stringstream stream; |
| 168 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 169 | "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] | 170 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 171 | |
| 172 | EXPECT_EQ(expected, stream.str()); |
| 173 | EXPECT_GE(PerValue, 132); |
| 174 | EXPECT_LE(PerValue, 145); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 175 | } |
| 176 | ; |
| 177 | |
| 178 | TEST(Histtest, MultipleCreateHist) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 179 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("MultipleCreateHist")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 180 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 181 | double PerValue; |
| 182 | hist->AddValue(15); |
| 183 | hist->AddValue(17); |
| 184 | hist->AddValue(35); |
| 185 | hist->AddValue(50); |
| 186 | hist->AddValue(68); |
| 187 | hist->AddValue(75); |
| 188 | hist->AddValue(93); |
| 189 | hist->CreateHistogram(); |
| 190 | hist->AddValue(110); |
| 191 | hist->AddValue(121); |
| 192 | hist->AddValue(132); |
| 193 | hist->AddValue(140); //Median value |
| 194 | hist->AddValue(145); |
| 195 | hist->AddValue(155); |
| 196 | hist->AddValue(163); |
| 197 | hist->AddValue(168); |
| 198 | hist->CreateHistogram(); |
| 199 | hist->AddValue(175); |
| 200 | hist->AddValue(182); |
| 201 | hist->AddValue(193); |
| 202 | hist->AddValue(200); |
| 203 | hist->AddValue(205); |
| 204 | hist->AddValue(212); |
| 205 | hist->CreateHistogram(); |
| 206 | PerValue = hist->Percentile(0.50); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 207 | std::stringstream stream; |
| 208 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 209 | "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] | 210 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 211 | |
| 212 | EXPECT_EQ(expected, stream.str()); |
| 213 | EXPECT_GE(PerValue, 132); |
| 214 | EXPECT_LE(PerValue, 145); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 215 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 216 | |
| 217 | TEST(Histtest, SingleValue) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 218 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("SingleValue")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 219 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 220 | hist->AddValue(1); |
| 221 | hist->CreateHistogram(); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 222 | std::stringstream stream; |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 223 | 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] | 224 | hist->PrintConfidenceIntervals(stream, 0.99); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 225 | EXPECT_EQ(expected, stream.str()); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 226 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 227 | |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 228 | TEST(Histtest, CappingPercentiles) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 229 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("CappingPercentiles")); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 230 | |
| 231 | double per_995; |
| 232 | double per_005; |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 233 | // All values are similar. |
| 234 | for (uint64_t idx = 0ull; idx < 150ull; idx++) { |
| 235 | hist->AddValue(0); |
| 236 | } |
| 237 | hist->CreateHistogram(); |
| 238 | per_995 = hist->Percentile(0.995); |
| 239 | EXPECT_EQ(per_995, 0); |
| 240 | hist->Reset(); |
| 241 | for (size_t idx = 0; idx < 200; idx++) { |
| 242 | for (uint64_t val = 1ull; val <= 4ull; val++) { |
| 243 | hist->AddValue(val); |
| 244 | } |
| 245 | } |
| 246 | hist->CreateHistogram(); |
| 247 | per_005 = hist->Percentile(0.005); |
| 248 | per_995 = hist->Percentile(0.995); |
| 249 | EXPECT_EQ(1, per_005); |
| 250 | EXPECT_EQ(4, per_995); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 251 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 252 | |
| 253 | TEST(Histtest, SpikyValues) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame^] | 254 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("SpikyValues")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 255 | |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 256 | for (uint64_t idx = 0ull; idx < 30ull; idx++) { |
| 257 | for (uint64_t idx_inner = 0ull; idx_inner < 5ull; idx_inner++) { |
| 258 | hist->AddValue(idx * idx_inner); |
| 259 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 260 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 261 | hist->AddValue(10000); |
| 262 | hist->CreateHistogram(); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 263 | std::stringstream stream; |
| 264 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 265 | "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] | 266 | hist->PrintConfidenceIntervals(stream, 0.99); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 267 | EXPECT_EQ(expected, stream.str()); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 268 | } |