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 14 matching lines...) Expand all Loading... |
25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
27 | 27 |
28 namespace base { | 28 namespace base { |
29 | 29 |
30 // Test parameter indicates if a persistent memory allocator should be used | 30 // Test parameter indicates if a persistent memory allocator should be used |
31 // for histogram allocation. False will allocate histograms from the process | 31 // for histogram allocation. False will allocate histograms from the process |
32 // heap. | 32 // heap. |
33 class HistogramTest : public testing::TestWithParam<bool> { | 33 class HistogramTest : public testing::TestWithParam<bool> { |
34 protected: | 34 protected: |
35 const int32_t kAllocatorMemorySize = 4 << 20; // 4 MiB | 35 const int32_t kAllocatorMemorySize = 8 << 20; // 8 MiB |
36 | 36 |
37 HistogramTest() : use_persistent_histogram_allocator_(GetParam()) {} | 37 HistogramTest() : use_persistent_histogram_allocator_(GetParam()) {} |
38 | 38 |
39 void SetUp() override { | 39 void SetUp() override { |
40 // Each test will have a clean state (no Histogram / BucketRanges | 40 // Each test will have a clean state (no Histogram / BucketRanges |
41 // registered). | 41 // registered). |
42 InitializeStatisticsRecorder(); | 42 InitializeStatisticsRecorder(); |
43 if (use_persistent_histogram_allocator_) | 43 if (use_persistent_histogram_allocator_) |
44 CreatePersistentMemoryAllocator(); | 44 CreatePersistentMemoryAllocator(); |
45 } | 45 } |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); | 228 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); |
229 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); | 229 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); |
230 HistogramBase* histogram = LinearHistogram::FactoryGet( | 230 HistogramBase* histogram = LinearHistogram::FactoryGet( |
231 "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags); | 231 "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags); |
232 | 232 |
233 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | 233 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); |
234 EXPECT_EQ(2, samples->TotalCount()); | 234 EXPECT_EQ(2, samples->TotalCount()); |
235 EXPECT_EQ(2, samples->GetCount(10)); | 235 EXPECT_EQ(2, samples->GetCount(10)); |
236 } | 236 } |
237 | 237 |
| 238 // Check that delta calculations work correct. |
| 239 TEST_P(HistogramTest, DeltaTest) { |
| 240 HistogramBase* histogram = |
| 241 Histogram::FactoryGet("DeltaHistogram", 1, 64, 8, |
| 242 HistogramBase::kNoFlags); |
| 243 histogram->Add(1); |
| 244 histogram->Add(10); |
| 245 histogram->Add(50); |
| 246 |
| 247 scoped_ptr<HistogramSamples> samples = histogram->SnapshotDelta(); |
| 248 EXPECT_EQ(3, samples->TotalCount()); |
| 249 EXPECT_EQ(1, samples->GetCount(1)); |
| 250 EXPECT_EQ(1, samples->GetCount(10)); |
| 251 EXPECT_EQ(1, samples->GetCount(50)); |
| 252 EXPECT_EQ(samples->TotalCount(), samples->redundant_count()); |
| 253 |
| 254 samples = histogram->SnapshotDelta(); |
| 255 EXPECT_EQ(0, samples->TotalCount()); |
| 256 |
| 257 histogram->Add(10); |
| 258 histogram->Add(10); |
| 259 samples = histogram->SnapshotDelta(); |
| 260 EXPECT_EQ(2, samples->TotalCount()); |
| 261 EXPECT_EQ(2, samples->GetCount(10)); |
| 262 |
| 263 samples = histogram->SnapshotDelta(); |
| 264 EXPECT_EQ(0, samples->TotalCount()); |
| 265 } |
| 266 |
238 TEST_P(HistogramTest, ExponentialRangesTest) { | 267 TEST_P(HistogramTest, ExponentialRangesTest) { |
239 // Check that we got a nice exponential when there was enough room. | 268 // Check that we got a nice exponential when there was enough room. |
240 BucketRanges ranges(9); | 269 BucketRanges ranges(9); |
241 Histogram::InitializeBucketRanges(1, 64, &ranges); | 270 Histogram::InitializeBucketRanges(1, 64, &ranges); |
242 EXPECT_EQ(0, ranges.range(0)); | 271 EXPECT_EQ(0, ranges.range(0)); |
243 int power_of_2 = 1; | 272 int power_of_2 = 1; |
244 for (int i = 1; i < 8; i++) { | 273 for (int i = 1; i < 8; i++) { |
245 EXPECT_EQ(power_of_2, ranges.range(i)); | 274 EXPECT_EQ(power_of_2, ranges.range(i)); |
246 power_of_2 *= 2; | 275 power_of_2 *= 2; |
247 } | 276 } |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 const_cast<BucketRanges*>(histogram->bucket_ranges()); | 569 const_cast<BucketRanges*>(histogram->bucket_ranges()); |
541 HistogramBase::Sample tmp = bucket_ranges->range(1); | 570 HistogramBase::Sample tmp = bucket_ranges->range(1); |
542 bucket_ranges->set_range(1, bucket_ranges->range(2)); | 571 bucket_ranges->set_range(1, bucket_ranges->range(2)); |
543 bucket_ranges->set_range(2, tmp); | 572 bucket_ranges->set_range(2, tmp); |
544 EXPECT_EQ( | 573 EXPECT_EQ( |
545 HistogramBase::BUCKET_ORDER_ERROR | HistogramBase::RANGE_CHECKSUM_ERROR, | 574 HistogramBase::BUCKET_ORDER_ERROR | HistogramBase::RANGE_CHECKSUM_ERROR, |
546 histogram->FindCorruption(*snapshot)); | 575 histogram->FindCorruption(*snapshot)); |
547 | 576 |
548 bucket_ranges->set_range(2, bucket_ranges->range(1)); | 577 bucket_ranges->set_range(2, bucket_ranges->range(1)); |
549 bucket_ranges->set_range(1, tmp); | 578 bucket_ranges->set_range(1, tmp); |
550 EXPECT_EQ(0, histogram->FindCorruption(*snapshot)); | 579 EXPECT_EQ(0U, histogram->FindCorruption(*snapshot)); |
551 | 580 |
552 // Show that two simple changes don't offset each other | 581 // Show that two simple changes don't offset each other |
553 bucket_ranges->set_range(3, bucket_ranges->range(3) + 1); | 582 bucket_ranges->set_range(3, bucket_ranges->range(3) + 1); |
554 EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR, | 583 EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR, |
555 histogram->FindCorruption(*snapshot)); | 584 histogram->FindCorruption(*snapshot)); |
556 | 585 |
557 bucket_ranges->set_range(4, bucket_ranges->range(4) - 1); | 586 bucket_ranges->set_range(4, bucket_ranges->range(4) - 1); |
558 EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR, | 587 EXPECT_EQ(HistogramBase::RANGE_CHECKSUM_ERROR, |
559 histogram->FindCorruption(*snapshot)); | 588 histogram->FindCorruption(*snapshot)); |
560 | 589 |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 // CustomHistogram needs at least 1 valid range. | 797 // CustomHistogram needs at least 1 valid range. |
769 custom_ranges.clear(); | 798 custom_ranges.clear(); |
770 custom_ranges.push_back(0); | 799 custom_ranges.push_back(0); |
771 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, | 800 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, |
772 HistogramBase::kNoFlags), | 801 HistogramBase::kNoFlags), |
773 ""); | 802 ""); |
774 } | 803 } |
775 #endif | 804 #endif |
776 | 805 |
777 } // namespace base | 806 } // namespace base |
OLD | NEW |