Chromium Code Reviews| 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/histogram.h" | 5 #include "base/metrics/histogram.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 // Test the AddCount function. | 238 // Test the AddCount function. |
| 239 TEST_F(HistogramTest, AddCountTest) { | 239 TEST_F(HistogramTest, AddCountTest) { |
| 240 const size_t kBucketCount = 50; | 240 const size_t kBucketCount = 50; |
| 241 Histogram* histogram = static_cast<Histogram*>( | 241 Histogram* histogram = static_cast<Histogram*>( |
| 242 Histogram::FactoryGet("AddCountHistogram", 10, 100, kBucketCount, | 242 Histogram::FactoryGet("AddCountHistogram", 10, 100, kBucketCount, |
| 243 HistogramBase::kNoFlags)); | 243 HistogramBase::kNoFlags)); |
| 244 | 244 |
| 245 histogram->AddCount(20, 15); | 245 histogram->AddCount(20, 15); |
| 246 histogram->AddCount(30, 14); | 246 histogram->AddCount(30, 14); |
| 247 | 247 |
| 248 scoped_ptr<SampleVector> samples = histogram->SnapshotSampleVector(); | 248 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
| 249 EXPECT_EQ(29, samples->TotalCount()); | 249 EXPECT_EQ(29, samples->TotalCount()); |
| 250 EXPECT_EQ(15, samples->GetCount(20)); | 250 EXPECT_EQ(15, samples->GetCount(20)); |
| 251 EXPECT_EQ(14, samples->GetCount(30)); | 251 EXPECT_EQ(14, samples->GetCount(30)); |
| 252 | 252 |
| 253 histogram->AddCount(20, 25); | 253 histogram->AddCount(20, 25); |
| 254 histogram->AddCount(30, 24); | 254 histogram->AddCount(30, 24); |
| 255 | 255 |
| 256 scoped_ptr<SampleVector> samples2 = histogram->SnapshotSampleVector(); | 256 scoped_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples(); |
| 257 EXPECT_EQ(78, samples2->TotalCount()); | 257 EXPECT_EQ(78, samples2->TotalCount()); |
| 258 EXPECT_EQ(40, samples2->GetCount(20)); | 258 EXPECT_EQ(40, samples2->GetCount(20)); |
| 259 EXPECT_EQ(38, samples2->GetCount(30)); | 259 EXPECT_EQ(38, samples2->GetCount(30)); |
| 260 } | 260 } |
| 261 | 261 |
| 262 // Test the AddCount function with large sample values. | |
| 263 TEST_F(HistogramTest, AddCountLargeTest) { | |
|
Alexei Svitkine (slow)
2016/01/22 22:33:03
Nit: Maybe AddCount_LargeValuesDontOverflow
And s
bcf
2016/01/22 22:46:14
Done.
| |
| 264 const size_t kBucketCount = 50; | |
| 265 Histogram* histogram = static_cast<Histogram*>( | |
| 266 Histogram::FactoryGet("AddCountHistogram", 10, 1000000000, kBucketCount, | |
| 267 HistogramBase::kNoFlags)); | |
| 268 | |
| 269 histogram->AddCount(200000000, 15); | |
| 270 histogram->AddCount(300000000, 14); | |
| 271 | |
| 272 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | |
| 273 EXPECT_EQ(29, samples->TotalCount()); | |
| 274 EXPECT_EQ(15, samples->GetCount(200000000)); | |
| 275 EXPECT_EQ(14, samples->GetCount(300000000)); | |
| 276 | |
| 277 histogram->AddCount(200000000, 25); | |
| 278 histogram->AddCount(300000000, 24); | |
| 279 | |
| 280 scoped_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples(); | |
| 281 EXPECT_EQ(78, samples2->TotalCount()); | |
| 282 EXPECT_EQ(40, samples2->GetCount(200000000)); | |
| 283 EXPECT_EQ(38, samples2->GetCount(300000000)); | |
| 284 EXPECT_EQ(19400000000LL, samples2->sum()); | |
| 285 } | |
| 286 | |
| 262 // Make sure histogram handles out-of-bounds data gracefully. | 287 // Make sure histogram handles out-of-bounds data gracefully. |
| 263 TEST_F(HistogramTest, BoundsTest) { | 288 TEST_F(HistogramTest, BoundsTest) { |
| 264 const size_t kBucketCount = 50; | 289 const size_t kBucketCount = 50; |
| 265 Histogram* histogram = static_cast<Histogram*>( | 290 Histogram* histogram = static_cast<Histogram*>( |
| 266 Histogram::FactoryGet("Bounded", 10, 100, kBucketCount, | 291 Histogram::FactoryGet("Bounded", 10, 100, kBucketCount, |
| 267 HistogramBase::kNoFlags)); | 292 HistogramBase::kNoFlags)); |
| 268 | 293 |
| 269 // Put two samples "out of bounds" above and below. | 294 // Put two samples "out of bounds" above and below. |
| 270 histogram->Add(5); | 295 histogram->Add(5); |
| 271 histogram->Add(-50); | 296 histogram->Add(-50); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 // But we can't spot a corruption if it is compensated for. | 376 // But we can't spot a corruption if it is compensated for. |
| 352 snapshot->counts_[1] += 100; | 377 snapshot->counts_[1] += 100; |
| 353 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES, | 378 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES, |
| 354 histogram->FindCorruption(*snapshot)); | 379 histogram->FindCorruption(*snapshot)); |
| 355 } | 380 } |
| 356 | 381 |
| 357 TEST_F(HistogramTest, CorruptBucketBounds) { | 382 TEST_F(HistogramTest, CorruptBucketBounds) { |
| 358 Histogram* histogram = static_cast<Histogram*>( | 383 Histogram* histogram = static_cast<Histogram*>( |
| 359 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags)); | 384 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags)); |
| 360 | 385 |
| 361 scoped_ptr<SampleVector> snapshot = histogram->SnapshotSampleVector(); | 386 scoped_ptr<HistogramSamples> snapshot = histogram->SnapshotSamples(); |
| 362 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES, | 387 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES, |
| 363 histogram->FindCorruption(*snapshot)); | 388 histogram->FindCorruption(*snapshot)); |
| 364 | 389 |
| 365 BucketRanges* bucket_ranges = | 390 BucketRanges* bucket_ranges = |
| 366 const_cast<BucketRanges*>(histogram->bucket_ranges()); | 391 const_cast<BucketRanges*>(histogram->bucket_ranges()); |
| 367 HistogramBase::Sample tmp = bucket_ranges->range(1); | 392 HistogramBase::Sample tmp = bucket_ranges->range(1); |
| 368 bucket_ranges->set_range(1, bucket_ranges->range(2)); | 393 bucket_ranges->set_range(1, bucket_ranges->range(2)); |
| 369 bucket_ranges->set_range(2, tmp); | 394 bucket_ranges->set_range(2, tmp); |
| 370 EXPECT_EQ( | 395 EXPECT_EQ( |
| 371 HistogramBase::BUCKET_ORDER_ERROR | HistogramBase::RANGE_CHECKSUM_ERROR, | 396 HistogramBase::BUCKET_ORDER_ERROR | HistogramBase::RANGE_CHECKSUM_ERROR, |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 531 // CustomHistogram needs at least 1 valid range. | 556 // CustomHistogram needs at least 1 valid range. |
| 532 custom_ranges.clear(); | 557 custom_ranges.clear(); |
| 533 custom_ranges.push_back(0); | 558 custom_ranges.push_back(0); |
| 534 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, | 559 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, |
| 535 HistogramBase::kNoFlags), | 560 HistogramBase::kNoFlags), |
| 536 ""); | 561 ""); |
| 537 } | 562 } |
| 538 #endif | 563 #endif |
| 539 | 564 |
| 540 } // namespace base | 565 } // namespace base |
| OLD | NEW |