OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/sample_map.h" | 5 #include "base/metrics/sample_map.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace base { | 10 namespace base { |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 EXPECT_DEATH(it->Next(), ""); | 160 EXPECT_DEATH(it->Next(), ""); |
161 | 161 |
162 samples.Accumulate(1, 100); | 162 samples.Accumulate(1, 100); |
163 it = samples.Iterator(); | 163 it = samples.Iterator(); |
164 EXPECT_FALSE(it->Done()); | 164 EXPECT_FALSE(it->Done()); |
165 } | 165 } |
166 | 166 |
167 #endif | 167 #endif |
168 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST | 168 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST |
169 | 169 |
| 170 |
| 171 // ----- PersistentSampleMap --------------------------------------------------- |
| 172 |
| 173 TEST(PersistentSampleMapTest, AccumulateTest) { |
| 174 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 175 |
| 176 HistogramSamples::Metadata* meta = |
| 177 allocator.GetAsObject<HistogramSamples::Metadata>( |
| 178 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); |
| 179 PersistentSampleMap samples(1, &allocator, meta); |
| 180 |
| 181 samples.Accumulate(1, 100); |
| 182 samples.Accumulate(2, 200); |
| 183 samples.Accumulate(1, -200); |
| 184 EXPECT_EQ(-100, samples.GetCount(1)); |
| 185 EXPECT_EQ(200, samples.GetCount(2)); |
| 186 |
| 187 EXPECT_EQ(300, samples.sum()); |
| 188 EXPECT_EQ(100, samples.TotalCount()); |
| 189 EXPECT_EQ(samples.redundant_count(), samples.TotalCount()); |
| 190 } |
| 191 |
| 192 TEST(PersistentSampleMapTest, Accumulate_LargeValuesDontOverflow) { |
| 193 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 194 |
| 195 HistogramSamples::Metadata* meta = |
| 196 allocator.GetAsObject<HistogramSamples::Metadata>( |
| 197 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); |
| 198 PersistentSampleMap samples(1, &allocator, meta); |
| 199 |
| 200 samples.Accumulate(250000000, 100); |
| 201 samples.Accumulate(500000000, 200); |
| 202 samples.Accumulate(250000000, -200); |
| 203 EXPECT_EQ(-100, samples.GetCount(250000000)); |
| 204 EXPECT_EQ(200, samples.GetCount(500000000)); |
| 205 |
| 206 EXPECT_EQ(75000000000LL, samples.sum()); |
| 207 EXPECT_EQ(100, samples.TotalCount()); |
| 208 EXPECT_EQ(samples.redundant_count(), samples.TotalCount()); |
| 209 } |
| 210 |
| 211 TEST(PersistentSampleMapTest, AddSubtractTest) { |
| 212 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 213 |
| 214 HistogramSamples::Metadata* meta1 = |
| 215 allocator.GetAsObject<HistogramSamples::Metadata>( |
| 216 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); |
| 217 HistogramSamples::Metadata* meta2 = |
| 218 allocator.GetAsObject<HistogramSamples::Metadata>( |
| 219 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); |
| 220 PersistentSampleMap samples1(1, &allocator, meta1); |
| 221 PersistentSampleMap samples2(2, &allocator, meta2); |
| 222 |
| 223 samples1.Accumulate(1, 100); |
| 224 samples1.Accumulate(2, 100); |
| 225 samples1.Accumulate(3, 100); |
| 226 |
| 227 samples2.Accumulate(1, 200); |
| 228 samples2.Accumulate(2, 200); |
| 229 samples2.Accumulate(4, 200); |
| 230 |
| 231 samples1.Add(samples2); |
| 232 EXPECT_EQ(300, samples1.GetCount(1)); |
| 233 EXPECT_EQ(300, samples1.GetCount(2)); |
| 234 EXPECT_EQ(100, samples1.GetCount(3)); |
| 235 EXPECT_EQ(200, samples1.GetCount(4)); |
| 236 EXPECT_EQ(2000, samples1.sum()); |
| 237 EXPECT_EQ(900, samples1.TotalCount()); |
| 238 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
| 239 |
| 240 samples1.Subtract(samples2); |
| 241 EXPECT_EQ(100, samples1.GetCount(1)); |
| 242 EXPECT_EQ(100, samples1.GetCount(2)); |
| 243 EXPECT_EQ(100, samples1.GetCount(3)); |
| 244 EXPECT_EQ(0, samples1.GetCount(4)); |
| 245 EXPECT_EQ(600, samples1.sum()); |
| 246 EXPECT_EQ(300, samples1.TotalCount()); |
| 247 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
| 248 } |
| 249 |
| 250 TEST(PersistentSampleMapTest, PersistenceTest) { |
| 251 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 252 |
| 253 HistogramSamples::Metadata* meta12 = |
| 254 allocator.GetAsObject<HistogramSamples::Metadata>( |
| 255 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); |
| 256 PersistentSampleMap samples1(12, &allocator, meta12); |
| 257 samples1.Accumulate(1, 100); |
| 258 samples1.Accumulate(2, 200); |
| 259 samples1.Accumulate(1, -200); |
| 260 EXPECT_EQ(-100, samples1.GetCount(1)); |
| 261 EXPECT_EQ(200, samples1.GetCount(2)); |
| 262 EXPECT_EQ(300, samples1.sum()); |
| 263 EXPECT_EQ(100, samples1.TotalCount()); |
| 264 EXPECT_EQ(samples1.redundant_count(), samples1.TotalCount()); |
| 265 |
| 266 PersistentSampleMap samples2(12, &allocator, meta12); |
| 267 EXPECT_EQ(samples1.id(), samples2.id()); |
| 268 EXPECT_EQ(samples1.sum(), samples2.sum()); |
| 269 EXPECT_EQ(samples1.redundant_count(), samples2.redundant_count()); |
| 270 EXPECT_EQ(samples1.TotalCount(), samples2.TotalCount()); |
| 271 EXPECT_EQ(-100, samples2.GetCount(1)); |
| 272 EXPECT_EQ(200, samples2.GetCount(2)); |
| 273 EXPECT_EQ(300, samples2.sum()); |
| 274 EXPECT_EQ(100, samples2.TotalCount()); |
| 275 EXPECT_EQ(samples2.redundant_count(), samples2.TotalCount()); |
| 276 |
| 277 EXPECT_EQ(0, samples2.GetCount(3)); |
| 278 EXPECT_EQ(0, samples1.GetCount(3)); |
| 279 samples2.Accumulate(3, 300); |
| 280 EXPECT_EQ(300, samples2.GetCount(3)); |
| 281 EXPECT_EQ(300, samples1.GetCount(3)); |
| 282 EXPECT_EQ(samples1.sum(), samples2.sum()); |
| 283 EXPECT_EQ(samples1.redundant_count(), samples2.redundant_count()); |
| 284 EXPECT_EQ(samples1.TotalCount(), samples2.TotalCount()); |
| 285 } |
| 286 |
| 287 TEST(PersistentSampleMapIteratorTest, IterateTest) { |
| 288 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 289 |
| 290 HistogramSamples::Metadata* meta = |
| 291 allocator.GetAsObject<HistogramSamples::Metadata>( |
| 292 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); |
| 293 PersistentSampleMap samples(1, &allocator, meta); |
| 294 samples.Accumulate(1, 100); |
| 295 samples.Accumulate(2, 200); |
| 296 samples.Accumulate(4, -300); |
| 297 samples.Accumulate(5, 0); |
| 298 |
| 299 scoped_ptr<SampleCountIterator> it = samples.Iterator(); |
| 300 |
| 301 HistogramBase::Sample min; |
| 302 HistogramBase::Sample max; |
| 303 HistogramBase::Count count; |
| 304 |
| 305 it->Get(&min, &max, &count); |
| 306 EXPECT_EQ(1, min); |
| 307 EXPECT_EQ(2, max); |
| 308 EXPECT_EQ(100, count); |
| 309 EXPECT_FALSE(it->GetBucketIndex(NULL)); |
| 310 |
| 311 it->Next(); |
| 312 it->Get(&min, &max, &count); |
| 313 EXPECT_EQ(2, min); |
| 314 EXPECT_EQ(3, max); |
| 315 EXPECT_EQ(200, count); |
| 316 |
| 317 it->Next(); |
| 318 it->Get(&min, &max, &count); |
| 319 EXPECT_EQ(4, min); |
| 320 EXPECT_EQ(5, max); |
| 321 EXPECT_EQ(-300, count); |
| 322 |
| 323 it->Next(); |
| 324 EXPECT_TRUE(it->Done()); |
| 325 } |
| 326 |
| 327 TEST(PersistentSampleMapIteratorTest, SkipEmptyRanges) { |
| 328 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 329 |
| 330 HistogramSamples::Metadata* meta = |
| 331 allocator.GetAsObject<HistogramSamples::Metadata>( |
| 332 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); |
| 333 PersistentSampleMap samples(1, &allocator, meta); |
| 334 samples.Accumulate(5, 1); |
| 335 samples.Accumulate(10, 2); |
| 336 samples.Accumulate(15, 3); |
| 337 samples.Accumulate(20, 4); |
| 338 samples.Accumulate(25, 5); |
| 339 |
| 340 HistogramSamples::Metadata* meta2 = |
| 341 allocator.GetAsObject<HistogramSamples::Metadata>( |
| 342 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); |
| 343 PersistentSampleMap samples2(2, &allocator, meta2); |
| 344 samples2.Accumulate(5, 1); |
| 345 samples2.Accumulate(20, 4); |
| 346 samples2.Accumulate(25, 5); |
| 347 |
| 348 samples.Subtract(samples2); |
| 349 |
| 350 scoped_ptr<SampleCountIterator> it = samples.Iterator(); |
| 351 EXPECT_FALSE(it->Done()); |
| 352 |
| 353 HistogramBase::Sample min; |
| 354 HistogramBase::Sample max; |
| 355 HistogramBase::Count count; |
| 356 |
| 357 it->Get(&min, &max, &count); |
| 358 EXPECT_EQ(10, min); |
| 359 EXPECT_EQ(11, max); |
| 360 EXPECT_EQ(2, count); |
| 361 |
| 362 it->Next(); |
| 363 EXPECT_FALSE(it->Done()); |
| 364 |
| 365 it->Get(&min, &max, &count); |
| 366 EXPECT_EQ(15, min); |
| 367 EXPECT_EQ(16, max); |
| 368 EXPECT_EQ(3, count); |
| 369 |
| 370 it->Next(); |
| 371 EXPECT_TRUE(it->Done()); |
| 372 } |
| 373 |
| 374 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST |
| 375 |
| 376 TEST(PersistentSampleMapIteratorDeathTest, IterateDoneTest) { |
| 377 LocalPersistentMemoryAllocator allocator(64 << 10, 0, ""); // 64 KiB |
| 378 |
| 379 HistogramSamples::Metadata* meta = |
| 380 allocator.GetAsObject<HistogramSamples::Metadata>( |
| 381 allocator.Allocate(sizeof(HistogramSamples::Metadata), 0), 0); |
| 382 PersistentSampleMap samples(1, &allocator, meta); |
| 383 |
| 384 scoped_ptr<SampleCountIterator> it = samples.Iterator(); |
| 385 |
| 386 EXPECT_TRUE(it->Done()); |
| 387 |
| 388 HistogramBase::Sample min; |
| 389 HistogramBase::Sample max; |
| 390 HistogramBase::Count count; |
| 391 EXPECT_DEATH(it->Get(&min, &max, &count), ""); |
| 392 |
| 393 EXPECT_DEATH(it->Next(), ""); |
| 394 |
| 395 samples.Accumulate(1, 100); |
| 396 it = samples.Iterator(); |
| 397 EXPECT_FALSE(it->Done()); |
| 398 } |
| 399 |
| 400 #endif |
| 401 // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && GTEST_HAS_DEATH_TEST |
| 402 |
170 } // namespace | 403 } // namespace |
171 } // namespace base | 404 } // namespace base |
OLD | NEW |