| 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 11 matching lines...) Expand all Loading... |
| 22 #include "base/metrics/statistics_recorder.h" | 22 #include "base/metrics/statistics_recorder.h" |
| 23 #include "base/pickle.h" | 23 #include "base/pickle.h" |
| 24 #include "base/rand_util.h" | 24 #include "base/rand_util.h" |
| 25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
| 26 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 28 |
| 29 namespace base { | 29 namespace base { |
| 30 | 30 |
| 31 class HistogramTest : public testing::Test { | 31 class HistogramTest : public testing::Test { |
| 32 public: |
| 33 HistogramTest() {} |
| 34 ~HistogramTest() override {} |
| 35 |
| 32 protected: | 36 protected: |
| 33 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB | 37 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB |
| 34 | 38 |
| 35 HistogramTest() {} | |
| 36 | |
| 37 void SetUp() override { | 39 void SetUp() override { |
| 38 // Each test will have a clean state (no Histogram / BucketRanges | 40 // Each test will have a clean state (no Histogram / BucketRanges |
| 39 // registered). | 41 // registered). |
| 40 InitializeStatisticsRecorder(); | 42 InitializeStatisticsRecorder(); |
| 41 // By getting the results-histogram before any persistent allocator | 43 // By getting the results-histogram before any persistent allocator |
| 42 // is attached, that histogram is guaranteed not to be stored in | 44 // is attached, that histogram is guaranteed not to be stored in |
| 43 // any persistent memory segment (which simplifies some tests). | 45 // any persistent memory segment (which simplifies some tests). |
| 44 GetCreateHistogramResultHistogram(); | 46 GetCreateHistogramResultHistogram(); |
| 45 } | 47 } |
| 46 | 48 |
| 47 void TearDown() override { | 49 void TearDown() override { |
| 48 UninitializeStatisticsRecorder(); | 50 UninitializeStatisticsRecorder(); |
| 49 DestroyPersistentMemoryAllocator(); | 51 DestroyPersistentMemoryAllocator(); |
| 50 } | 52 } |
| 51 | 53 |
| 52 void InitializeStatisticsRecorder() { | 54 void InitializeStatisticsRecorder() { |
| 55 StatisticsRecorder::ResetForTesting(); |
| 53 statistics_recorder_ = new StatisticsRecorder(); | 56 statistics_recorder_ = new StatisticsRecorder(); |
| 54 } | 57 } |
| 55 | 58 |
| 56 void UninitializeStatisticsRecorder() { | 59 void UninitializeStatisticsRecorder() { |
| 57 delete statistics_recorder_; | 60 delete statistics_recorder_; |
| 58 statistics_recorder_ = NULL; | 61 statistics_recorder_ = NULL; |
| 59 } | 62 } |
| 60 | 63 |
| 61 void CreatePersistentMemoryAllocator() { | 64 void CreatePersistentMemoryAllocator() { |
| 62 if (!allocator_memory_) | 65 if (!allocator_memory_) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 77 } | 80 } |
| 78 | 81 |
| 79 StatisticsRecorder* statistics_recorder_; | 82 StatisticsRecorder* statistics_recorder_; |
| 80 scoped_ptr<char[]> allocator_memory_; | 83 scoped_ptr<char[]> allocator_memory_; |
| 81 PersistentMemoryAllocator* allocator_; | 84 PersistentMemoryAllocator* allocator_; |
| 82 | 85 |
| 83 private: | 86 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(HistogramTest); | 87 DISALLOW_COPY_AND_ASSIGN(HistogramTest); |
| 85 }; | 88 }; |
| 86 | 89 |
| 90 class AllocatorHistogramTest : public HistogramTest, |
| 91 public testing::WithParamInterface<bool> { |
| 92 public: |
| 93 AllocatorHistogramTest() { |
| 94 if (GetParam()) |
| 95 CreatePersistentMemoryAllocator(); |
| 96 } |
| 97 ~AllocatorHistogramTest() override {} |
| 98 }; |
| 99 |
| 100 // Run all AllocatorHistogramTest cases with both heap and persistent memory. |
| 101 INSTANTIATE_TEST_CASE_P(HeapAndPersistent, AllocatorHistogramTest, |
| 102 testing::Values(false, true)); |
| 103 |
| 104 |
| 87 // Check for basic syntax and use. | 105 // Check for basic syntax and use. |
| 88 TEST_F(HistogramTest, BasicTest) { | 106 TEST_F(HistogramTest, BasicTest) { |
| 89 // Try basic construction | 107 // Try basic construction |
| 90 HistogramBase* histogram = Histogram::FactoryGet( | 108 HistogramBase* histogram = Histogram::FactoryGet( |
| 91 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); | 109 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); |
| 92 EXPECT_TRUE(histogram); | 110 EXPECT_TRUE(histogram); |
| 93 | 111 |
| 94 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( | 112 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( |
| 95 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags); | 113 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags); |
| 96 EXPECT_TRUE(linear_histogram); | 114 EXPECT_TRUE(linear_histogram); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); | 219 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); |
| 202 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); | 220 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); |
| 203 HistogramBase* histogram = LinearHistogram::FactoryGet( | 221 HistogramBase* histogram = LinearHistogram::FactoryGet( |
| 204 "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags); | 222 "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags); |
| 205 | 223 |
| 206 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | 224 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
| 207 EXPECT_EQ(2, samples->TotalCount()); | 225 EXPECT_EQ(2, samples->TotalCount()); |
| 208 EXPECT_EQ(2, samples->GetCount(10)); | 226 EXPECT_EQ(2, samples->GetCount(10)); |
| 209 } | 227 } |
| 210 | 228 |
| 229 // Check that delta calculations work correct. |
| 230 TEST_P(AllocatorHistogramTest, DeltaTest) { |
| 231 HistogramBase* histogram = |
| 232 Histogram::FactoryGet("DeltaHistogram", 1, 64, 8, |
| 233 HistogramBase::kNoFlags); |
| 234 histogram->Add(1); |
| 235 histogram->Add(10); |
| 236 histogram->Add(50); |
| 237 |
| 238 scoped_ptr<HistogramSamples> samples = histogram->SnapshotDelta(); |
| 239 EXPECT_EQ(3, samples->TotalCount()); |
| 240 EXPECT_EQ(1, samples->GetCount(1)); |
| 241 EXPECT_EQ(1, samples->GetCount(10)); |
| 242 EXPECT_EQ(1, samples->GetCount(50)); |
| 243 EXPECT_EQ(samples->TotalCount(), samples->redundant_count()); |
| 244 |
| 245 samples = histogram->SnapshotDelta(); |
| 246 EXPECT_EQ(0, samples->TotalCount()); |
| 247 |
| 248 histogram->Add(10); |
| 249 histogram->Add(10); |
| 250 samples = histogram->SnapshotDelta(); |
| 251 EXPECT_EQ(2, samples->TotalCount()); |
| 252 EXPECT_EQ(2, samples->GetCount(10)); |
| 253 |
| 254 samples = histogram->SnapshotDelta(); |
| 255 EXPECT_EQ(0, samples->TotalCount()); |
| 256 } |
| 257 |
| 211 TEST_F(HistogramTest, ExponentialRangesTest) { | 258 TEST_F(HistogramTest, ExponentialRangesTest) { |
| 212 // Check that we got a nice exponential when there was enough room. | 259 // Check that we got a nice exponential when there was enough room. |
| 213 BucketRanges ranges(9); | 260 BucketRanges ranges(9); |
| 214 Histogram::InitializeBucketRanges(1, 64, &ranges); | 261 Histogram::InitializeBucketRanges(1, 64, &ranges); |
| 215 EXPECT_EQ(0, ranges.range(0)); | 262 EXPECT_EQ(0, ranges.range(0)); |
| 216 int power_of_2 = 1; | 263 int power_of_2 = 1; |
| 217 for (int i = 1; i < 8; i++) { | 264 for (int i = 1; i < 8; i++) { |
| 218 EXPECT_EQ(power_of_2, ranges.range(i)); | 265 EXPECT_EQ(power_of_2, ranges.range(i)); |
| 219 power_of_2 *= 2; | 266 power_of_2 *= 2; |
| 220 } | 267 } |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 const_cast<BucketRanges*>(histogram->bucket_ranges()); | 560 const_cast<BucketRanges*>(histogram->bucket_ranges()); |
| 514 HistogramBase::Sample tmp = bucket_ranges->range(1); | 561 HistogramBase::Sample tmp = bucket_ranges->range(1); |
| 515 bucket_ranges->set_range(1, bucket_ranges->range(2)); | 562 bucket_ranges->set_range(1, bucket_ranges->range(2)); |
| 516 bucket_ranges->set_range(2, tmp); | 563 bucket_ranges->set_range(2, tmp); |
| 517 EXPECT_EQ( | 564 EXPECT_EQ( |
| 518 HistogramBase::BUCKET_ORDER_ERROR | HistogramBase::RANGE_CHECKSUM_ERROR, | 565 HistogramBase::BUCKET_ORDER_ERROR | HistogramBase::RANGE_CHECKSUM_ERROR, |
| 519 histogram->FindCorruption(*snapshot)); | 566 histogram->FindCorruption(*snapshot)); |
| 520 | 567 |
| 521 bucket_ranges->set_range(2, bucket_ranges->range(1)); | 568 bucket_ranges->set_range(2, bucket_ranges->range(1)); |
| 522 bucket_ranges->set_range(1, tmp); | 569 bucket_ranges->set_range(1, tmp); |
| 523 EXPECT_EQ(0, histogram->FindCorruption(*snapshot)); | 570 EXPECT_EQ(0U, histogram->FindCorruption(*snapshot)); |
| 524 | 571 |
| 525 // Show that two simple changes don't offset each other | 572 // Show that two simple changes don't offset each other |
| 526 bucket_ranges->set_range(3, bucket_ranges->range(3) + 1); | 573 bucket_ranges->set_range(3, bucket_ranges->range(3) + 1); |
| 527 EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR, | 574 EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR, |
| 528 histogram->FindCorruption(*snapshot)); | 575 histogram->FindCorruption(*snapshot)); |
| 529 | 576 |
| 530 bucket_ranges->set_range(4, bucket_ranges->range(4) - 1); | 577 bucket_ranges->set_range(4, bucket_ranges->range(4) - 1); |
| 531 EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR, | 578 EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR, |
| 532 histogram->FindCorruption(*snapshot)); | 579 histogram->FindCorruption(*snapshot)); |
| 533 | 580 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 // CustomHistogram needs at least 1 valid range. | 771 // CustomHistogram needs at least 1 valid range. |
| 725 custom_ranges.clear(); | 772 custom_ranges.clear(); |
| 726 custom_ranges.push_back(0); | 773 custom_ranges.push_back(0); |
| 727 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, | 774 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, |
| 728 HistogramBase::kNoFlags), | 775 HistogramBase::kNoFlags), |
| 729 ""); | 776 ""); |
| 730 } | 777 } |
| 731 #endif | 778 #endif |
| 732 | 779 |
| 733 } // namespace base | 780 } // namespace base |
| OLD | NEW |