Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Side by Side Diff: base/metrics/histogram_unittest.cc

Issue 2867303004: [histogram] Make histograms more resistant to overflows. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« base/metrics/histogram.cc ('K') | « base/metrics/histogram.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 383 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 histogram->AddCount(200000000, 25); 394 histogram->AddCount(200000000, 25);
395 histogram->AddCount(300000000, 24); 395 histogram->AddCount(300000000, 24);
396 396
397 std::unique_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples(); 397 std::unique_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples();
398 EXPECT_EQ(78, samples2->TotalCount()); 398 EXPECT_EQ(78, samples2->TotalCount());
399 EXPECT_EQ(40, samples2->GetCount(200000000)); 399 EXPECT_EQ(40, samples2->GetCount(200000000));
400 EXPECT_EQ(38, samples2->GetCount(300000000)); 400 EXPECT_EQ(38, samples2->GetCount(300000000));
401 EXPECT_EQ(19400000000LL, samples2->sum()); 401 EXPECT_EQ(19400000000LL, samples2->sum());
402 } 402 }
403 403
404 TEST_P(HistogramTest, AddCount_LargeCountsDontOverflow) {
Ilya Sherman 2017/05/09 22:15:32 Please add a comment documenting what this is inte
altimin 2017/05/10 11:50:38 Done.
405 const size_t kBucketCount = 50;
406 Histogram* histogram = static_cast<Histogram*>(
407 Histogram::FactoryGet("AddCountHistogram", 10, 1000000000, kBucketCount,
Ilya Sherman 2017/05/09 22:15:32 nit: Do you need such a large max bucket value? I
altimin 2017/05/10 11:50:38 Done.
408 HistogramBase::kNoFlags));
409
410 // Repeat N times to make sure that there is no internal value overflow.
411 for (int i = 0; i < 10; ++i) {
412 histogram->AddCount(42, 1 << 30);
413 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotDelta();
414 EXPECT_EQ(1 << 30, samples->TotalCount());
415 EXPECT_EQ(1 << 30, samples->GetCount(42));
416 }
417 }
418
404 // Make sure histogram handles out-of-bounds data gracefully. 419 // Make sure histogram handles out-of-bounds data gracefully.
405 TEST_P(HistogramTest, BoundsTest) { 420 TEST_P(HistogramTest, BoundsTest) {
406 const size_t kBucketCount = 50; 421 const size_t kBucketCount = 50;
407 Histogram* histogram = static_cast<Histogram*>( 422 Histogram* histogram = static_cast<Histogram*>(
408 Histogram::FactoryGet("Bounded", 10, 100, kBucketCount, 423 Histogram::FactoryGet("Bounded", 10, 100, kBucketCount,
409 HistogramBase::kNoFlags)); 424 HistogramBase::kNoFlags));
410 425
411 // Put two samples "out of bounds" above and below. 426 // Put two samples "out of bounds" above and below.
412 histogram->Add(5); 427 histogram->Add(5);
413 histogram->Add(-50); 428 histogram->Add(-50);
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
743 // CustomHistogram needs at least 1 valid range. 758 // CustomHistogram needs at least 1 valid range.
744 custom_ranges.clear(); 759 custom_ranges.clear();
745 custom_ranges.push_back(0); 760 custom_ranges.push_back(0);
746 EXPECT_DEATH_IF_SUPPORTED( 761 EXPECT_DEATH_IF_SUPPORTED(
747 CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, 762 CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges,
748 HistogramBase::kNoFlags), 763 HistogramBase::kNoFlags),
749 ""); 764 "");
750 } 765 }
751 766
752 } // namespace base 767 } // namespace base
OLDNEW
« base/metrics/histogram.cc ('K') | « base/metrics/histogram.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698