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

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

Issue 1618423004: Fix overflow bug in histogram sample data structures (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Rename tests, remove redundant comments. Created 4 years, 11 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
« no previous file with comments | « base/metrics/histogram.h ('k') | base/metrics/sample_map.cc » ('j') | 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 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 Histogram* histogram = static_cast<Histogram*>( 228 Histogram* histogram = static_cast<Histogram*>(
229 CustomHistogram::FactoryGet("2BucketsCustomHistogram", custom_ranges, 229 CustomHistogram::FactoryGet("2BucketsCustomHistogram", custom_ranges,
230 HistogramBase::kNoFlags)); 230 HistogramBase::kNoFlags));
231 const BucketRanges* ranges = histogram->bucket_ranges(); 231 const BucketRanges* ranges = histogram->bucket_ranges();
232 ASSERT_EQ(3u, ranges->size()); 232 ASSERT_EQ(3u, ranges->size());
233 EXPECT_EQ(0, ranges->range(0)); 233 EXPECT_EQ(0, ranges->range(0));
234 EXPECT_EQ(4, ranges->range(1)); 234 EXPECT_EQ(4, ranges->range(1));
235 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2)); 235 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2));
236 } 236 }
237 237
238 // Test the AddCount function.
239 TEST_F(HistogramTest, AddCountTest) { 238 TEST_F(HistogramTest, AddCountTest) {
240 const size_t kBucketCount = 50; 239 const size_t kBucketCount = 50;
241 Histogram* histogram = static_cast<Histogram*>( 240 Histogram* histogram = static_cast<Histogram*>(
242 Histogram::FactoryGet("AddCountHistogram", 10, 100, kBucketCount, 241 Histogram::FactoryGet("AddCountHistogram", 10, 100, kBucketCount,
243 HistogramBase::kNoFlags)); 242 HistogramBase::kNoFlags));
244 243
245 histogram->AddCount(20, 15); 244 histogram->AddCount(20, 15);
246 histogram->AddCount(30, 14); 245 histogram->AddCount(30, 14);
247 246
248 scoped_ptr<SampleVector> samples = histogram->SnapshotSampleVector(); 247 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
249 EXPECT_EQ(29, samples->TotalCount()); 248 EXPECT_EQ(29, samples->TotalCount());
250 EXPECT_EQ(15, samples->GetCount(20)); 249 EXPECT_EQ(15, samples->GetCount(20));
251 EXPECT_EQ(14, samples->GetCount(30)); 250 EXPECT_EQ(14, samples->GetCount(30));
252 251
253 histogram->AddCount(20, 25); 252 histogram->AddCount(20, 25);
254 histogram->AddCount(30, 24); 253 histogram->AddCount(30, 24);
255 254
256 scoped_ptr<SampleVector> samples2 = histogram->SnapshotSampleVector(); 255 scoped_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples();
257 EXPECT_EQ(78, samples2->TotalCount()); 256 EXPECT_EQ(78, samples2->TotalCount());
258 EXPECT_EQ(40, samples2->GetCount(20)); 257 EXPECT_EQ(40, samples2->GetCount(20));
259 EXPECT_EQ(38, samples2->GetCount(30)); 258 EXPECT_EQ(38, samples2->GetCount(30));
260 } 259 }
261 260
261 TEST_F(HistogramTest, AddCount_LargeValuesDontOverflow) {
262 const size_t kBucketCount = 50;
263 Histogram* histogram = static_cast<Histogram*>(
264 Histogram::FactoryGet("AddCountHistogram", 10, 1000000000, kBucketCount,
265 HistogramBase::kNoFlags));
266
267 histogram->AddCount(200000000, 15);
268 histogram->AddCount(300000000, 14);
269
270 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
271 EXPECT_EQ(29, samples->TotalCount());
272 EXPECT_EQ(15, samples->GetCount(200000000));
273 EXPECT_EQ(14, samples->GetCount(300000000));
274
275 histogram->AddCount(200000000, 25);
276 histogram->AddCount(300000000, 24);
277
278 scoped_ptr<HistogramSamples> samples2 = histogram->SnapshotSamples();
279 EXPECT_EQ(78, samples2->TotalCount());
280 EXPECT_EQ(40, samples2->GetCount(200000000));
281 EXPECT_EQ(38, samples2->GetCount(300000000));
282 EXPECT_EQ(19400000000LL, samples2->sum());
283 }
284
262 // Make sure histogram handles out-of-bounds data gracefully. 285 // Make sure histogram handles out-of-bounds data gracefully.
263 TEST_F(HistogramTest, BoundsTest) { 286 TEST_F(HistogramTest, BoundsTest) {
264 const size_t kBucketCount = 50; 287 const size_t kBucketCount = 50;
265 Histogram* histogram = static_cast<Histogram*>( 288 Histogram* histogram = static_cast<Histogram*>(
266 Histogram::FactoryGet("Bounded", 10, 100, kBucketCount, 289 Histogram::FactoryGet("Bounded", 10, 100, kBucketCount,
267 HistogramBase::kNoFlags)); 290 HistogramBase::kNoFlags));
268 291
269 // Put two samples "out of bounds" above and below. 292 // Put two samples "out of bounds" above and below.
270 histogram->Add(5); 293 histogram->Add(5);
271 histogram->Add(-50); 294 histogram->Add(-50);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 // But we can't spot a corruption if it is compensated for. 374 // But we can't spot a corruption if it is compensated for.
352 snapshot->counts_[1] += 100; 375 snapshot->counts_[1] += 100;
353 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES, 376 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES,
354 histogram->FindCorruption(*snapshot)); 377 histogram->FindCorruption(*snapshot));
355 } 378 }
356 379
357 TEST_F(HistogramTest, CorruptBucketBounds) { 380 TEST_F(HistogramTest, CorruptBucketBounds) {
358 Histogram* histogram = static_cast<Histogram*>( 381 Histogram* histogram = static_cast<Histogram*>(
359 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags)); 382 Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
360 383
361 scoped_ptr<SampleVector> snapshot = histogram->SnapshotSampleVector(); 384 scoped_ptr<HistogramSamples> snapshot = histogram->SnapshotSamples();
362 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES, 385 EXPECT_EQ(HistogramBase::NO_INCONSISTENCIES,
363 histogram->FindCorruption(*snapshot)); 386 histogram->FindCorruption(*snapshot));
364 387
365 BucketRanges* bucket_ranges = 388 BucketRanges* bucket_ranges =
366 const_cast<BucketRanges*>(histogram->bucket_ranges()); 389 const_cast<BucketRanges*>(histogram->bucket_ranges());
367 HistogramBase::Sample tmp = bucket_ranges->range(1); 390 HistogramBase::Sample tmp = bucket_ranges->range(1);
368 bucket_ranges->set_range(1, bucket_ranges->range(2)); 391 bucket_ranges->set_range(1, bucket_ranges->range(2));
369 bucket_ranges->set_range(2, tmp); 392 bucket_ranges->set_range(2, tmp);
370 EXPECT_EQ( 393 EXPECT_EQ(
371 HistogramBase::BUCKET_ORDER_ERROR | HistogramBase::RANGE_CHECKSUM_ERROR, 394 HistogramBase::BUCKET_ORDER_ERROR | HistogramBase::RANGE_CHECKSUM_ERROR,
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 // CustomHistogram needs at least 1 valid range. 554 // CustomHistogram needs at least 1 valid range.
532 custom_ranges.clear(); 555 custom_ranges.clear();
533 custom_ranges.push_back(0); 556 custom_ranges.push_back(0);
534 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, 557 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges,
535 HistogramBase::kNoFlags), 558 HistogramBase::kNoFlags),
536 ""); 559 "");
537 } 560 }
538 #endif 561 #endif
539 562
540 } // namespace base 563 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/histogram.h ('k') | base/metrics/sample_map.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698