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 | |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 23 | namespace art { |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 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 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 132 | |
| 133 | TEST(Histtest, Reset) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 134 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("Reset")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 135 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 136 | double PerValue; |
| 137 | hist->AddValue(0); |
| 138 | hist->AddValue(189); |
| 139 | hist->AddValue(389); |
| 140 | hist->Reset(); |
| 141 | hist->AddValue(15); |
| 142 | hist->AddValue(17); |
| 143 | hist->AddValue(35); |
| 144 | hist->AddValue(50); |
| 145 | hist->AddValue(68); |
| 146 | hist->AddValue(75); |
| 147 | hist->AddValue(93); |
| 148 | hist->AddValue(110); |
| 149 | hist->AddValue(121); |
| 150 | hist->AddValue(132); |
| 151 | hist->AddValue(140); //Median value |
| 152 | hist->AddValue(145); |
| 153 | hist->AddValue(155); |
| 154 | hist->AddValue(163); |
| 155 | hist->AddValue(168); |
| 156 | hist->AddValue(175); |
| 157 | hist->AddValue(182); |
| 158 | hist->AddValue(193); |
| 159 | hist->AddValue(200); |
| 160 | hist->AddValue(205); |
| 161 | hist->AddValue(212); |
| 162 | hist->CreateHistogram(); |
| 163 | PerValue = hist->Percentile(0.50); |
| 164 | |
| 165 | std::string text; |
| 166 | std::stringstream stream; |
| 167 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 168 | "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] | 169 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 170 | |
| 171 | EXPECT_EQ(expected, stream.str()); |
| 172 | EXPECT_GE(PerValue, 132); |
| 173 | EXPECT_LE(PerValue, 145); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 174 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 175 | |
| 176 | TEST(Histtest, MultipleCreateHist) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 177 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("MultipleCreateHist")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 178 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 179 | double PerValue; |
| 180 | hist->AddValue(15); |
| 181 | hist->AddValue(17); |
| 182 | hist->AddValue(35); |
| 183 | hist->AddValue(50); |
| 184 | hist->AddValue(68); |
| 185 | hist->AddValue(75); |
| 186 | hist->AddValue(93); |
| 187 | hist->CreateHistogram(); |
| 188 | hist->AddValue(110); |
| 189 | hist->AddValue(121); |
| 190 | hist->AddValue(132); |
| 191 | hist->AddValue(140); //Median value |
| 192 | hist->AddValue(145); |
| 193 | hist->AddValue(155); |
| 194 | hist->AddValue(163); |
| 195 | hist->AddValue(168); |
| 196 | hist->CreateHistogram(); |
| 197 | hist->AddValue(175); |
| 198 | hist->AddValue(182); |
| 199 | hist->AddValue(193); |
| 200 | hist->AddValue(200); |
| 201 | hist->AddValue(205); |
| 202 | hist->AddValue(212); |
| 203 | hist->CreateHistogram(); |
| 204 | PerValue = hist->Percentile(0.50); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 205 | std::stringstream stream; |
| 206 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 207 | "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] | 208 | hist->PrintConfidenceIntervals(stream, 0.99); |
| 209 | |
| 210 | EXPECT_EQ(expected, stream.str()); |
| 211 | EXPECT_GE(PerValue, 132); |
| 212 | EXPECT_LE(PerValue, 145); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 213 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 214 | |
| 215 | TEST(Histtest, SingleValue) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 216 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("SingleValue")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 217 | |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 218 | hist->AddValue(1); |
| 219 | hist->CreateHistogram(); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 220 | std::stringstream stream; |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 221 | 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] | 222 | hist->PrintConfidenceIntervals(stream, 0.99); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 223 | EXPECT_EQ(expected, stream.str()); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 224 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 225 | |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 226 | TEST(Histtest, CappingPercentiles) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 227 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("CappingPercentiles")); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 228 | |
| 229 | double per_995; |
| 230 | double per_005; |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 231 | // All values are similar. |
| 232 | for (uint64_t idx = 0ull; idx < 150ull; idx++) { |
| 233 | hist->AddValue(0); |
| 234 | } |
| 235 | hist->CreateHistogram(); |
| 236 | per_995 = hist->Percentile(0.995); |
| 237 | EXPECT_EQ(per_995, 0); |
| 238 | hist->Reset(); |
| 239 | for (size_t idx = 0; idx < 200; idx++) { |
| 240 | for (uint64_t val = 1ull; val <= 4ull; val++) { |
| 241 | hist->AddValue(val); |
| 242 | } |
| 243 | } |
| 244 | hist->CreateHistogram(); |
| 245 | per_005 = hist->Percentile(0.005); |
| 246 | per_995 = hist->Percentile(0.995); |
| 247 | EXPECT_EQ(1, per_005); |
| 248 | EXPECT_EQ(4, per_995); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 249 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 250 | |
| 251 | TEST(Histtest, SpikyValues) { |
Ian Rogers | 4535705 | 2013-04-18 20:49:43 -0700 | [diff] [blame] | 252 | UniquePtr<Histogram<uint64_t> > hist(new Histogram<uint64_t>("SpikyValues")); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 253 | |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 254 | for (uint64_t idx = 0ull; idx < 30ull; idx++) { |
| 255 | for (uint64_t idx_inner = 0ull; idx_inner < 5ull; idx_inner++) { |
| 256 | hist->AddValue(idx * idx_inner); |
| 257 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 258 | } |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 259 | hist->AddValue(10000); |
| 260 | hist->CreateHistogram(); |
Sameer Abu Asal | c081e36 | 2013-02-20 16:45:38 -0800 | [diff] [blame] | 261 | std::stringstream stream; |
| 262 | std::string expected = |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 263 | "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] | 264 | hist->PrintConfidenceIntervals(stream, 0.99); |
Sameer Abu Asal | 857a078 | 2013-02-21 11:00:20 -0800 | [diff] [blame] | 265 | EXPECT_EQ(expected, stream.str()); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 266 | } |
Brian Carlstrom | 3e3d591 | 2013-07-18 00:19:45 -0700 | [diff] [blame] | 267 | |
| 268 | } // namespace art |