| 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 |
| 11 #include <climits> | 11 #include <climits> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/metrics/bucket_ranges.h" | 17 #include "base/metrics/bucket_ranges.h" |
| 18 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
| 19 #include "base/metrics/persistent_histogram_allocator.h" | 19 #include "base/metrics/persistent_histogram_allocator.h" |
| 20 #include "base/metrics/persistent_memory_allocator.h" | 20 #include "base/metrics/persistent_memory_allocator.h" |
| 21 #include "base/metrics/sample_vector.h" | 21 #include "base/metrics/sample_vector.h" |
| 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/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
| 25 #include "base/test/gtest_util.h" |
| 25 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 28 namespace base { | 29 namespace base { |
| 29 | 30 |
| 30 // Test parameter indicates if a persistent memory allocator should be used | 31 // Test parameter indicates if a persistent memory allocator should be used |
| 31 // for histogram allocation. False will allocate histograms from the process | 32 // for histogram allocation. False will allocate histograms from the process |
| 32 // heap. | 33 // heap. |
| 33 class HistogramTest : public testing::TestWithParam<bool> { | 34 class HistogramTest : public testing::TestWithParam<bool> { |
| 34 protected: | 35 protected: |
| (...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 histogram->Add(i & 127); | 695 histogram->Add(i & 127); |
| 695 TimeDelta add_ticks = TimeTicks::Now() - add_start; | 696 TimeDelta add_ticks = TimeTicks::Now() - add_start; |
| 696 int64_t add_ms = add_ticks.InMilliseconds(); | 697 int64_t add_ms = add_ticks.InMilliseconds(); |
| 697 | 698 |
| 698 VLOG(1) << kTestAddCount << " histogram adds took " << add_ms | 699 VLOG(1) << kTestAddCount << " histogram adds took " << add_ms |
| 699 << "ms or about " | 700 << "ms or about " |
| 700 << (add_ms * 1000000) / kTestAddCount | 701 << (add_ms * 1000000) / kTestAddCount |
| 701 << "ns each."; | 702 << "ns each."; |
| 702 } | 703 } |
| 703 | 704 |
| 704 #if GTEST_HAS_DEATH_TEST | |
| 705 // For Histogram, LinearHistogram and CustomHistogram, the minimum for a | 705 // For Histogram, LinearHistogram and CustomHistogram, the minimum for a |
| 706 // declared range is 1, while the maximum is (HistogramBase::kSampleType_MAX - | 706 // declared range is 1, while the maximum is (HistogramBase::kSampleType_MAX - |
| 707 // 1). But we accept ranges exceeding those limits, and silently clamped to | 707 // 1). But we accept ranges exceeding those limits, and silently clamped to |
| 708 // those limits. This is for backwards compatibility. | 708 // those limits. This is for backwards compatibility. |
| 709 TEST(HistogramDeathTest, BadRangesTest) { | 709 TEST(HistogramDeathTest, BadRangesTest) { |
| 710 HistogramBase* histogram = Histogram::FactoryGet( | 710 HistogramBase* histogram = Histogram::FactoryGet( |
| 711 "BadRanges", 0, HistogramBase::kSampleType_MAX, 8, | 711 "BadRanges", 0, HistogramBase::kSampleType_MAX, 8, |
| 712 HistogramBase::kNoFlags); | 712 HistogramBase::kNoFlags); |
| 713 EXPECT_TRUE( | 713 EXPECT_TRUE( |
| 714 histogram->HasConstructionArguments( | 714 histogram->HasConstructionArguments( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 728 CustomHistogram::FactoryGet( | 728 CustomHistogram::FactoryGet( |
| 729 "BadRangesCustom", custom_ranges, HistogramBase::kNoFlags)); | 729 "BadRangesCustom", custom_ranges, HistogramBase::kNoFlags)); |
| 730 const BucketRanges* ranges = custom_histogram->bucket_ranges(); | 730 const BucketRanges* ranges = custom_histogram->bucket_ranges(); |
| 731 ASSERT_EQ(3u, ranges->size()); | 731 ASSERT_EQ(3u, ranges->size()); |
| 732 EXPECT_EQ(0, ranges->range(0)); | 732 EXPECT_EQ(0, ranges->range(0)); |
| 733 EXPECT_EQ(5, ranges->range(1)); | 733 EXPECT_EQ(5, ranges->range(1)); |
| 734 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2)); | 734 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2)); |
| 735 | 735 |
| 736 // CustomHistogram does not accepts kSampleType_MAX as range. | 736 // CustomHistogram does not accepts kSampleType_MAX as range. |
| 737 custom_ranges.push_back(HistogramBase::kSampleType_MAX); | 737 custom_ranges.push_back(HistogramBase::kSampleType_MAX); |
| 738 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom2", custom_ranges, | 738 EXPECT_DCHECK_DEATH( |
| 739 HistogramBase::kNoFlags), | 739 CustomHistogram::FactoryGet("BadRangesCustom2", custom_ranges, |
| 740 ""); | 740 HistogramBase::kNoFlags), |
| 741 ""); |
| 741 | 742 |
| 742 // CustomHistogram needs at least 1 valid range. | 743 // CustomHistogram needs at least 1 valid range. |
| 743 custom_ranges.clear(); | 744 custom_ranges.clear(); |
| 744 custom_ranges.push_back(0); | 745 custom_ranges.push_back(0); |
| 745 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, | 746 EXPECT_DCHECK_DEATH( |
| 746 HistogramBase::kNoFlags), | 747 CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, |
| 747 ""); | 748 HistogramBase::kNoFlags), |
| 749 ""); |
| 748 } | 750 } |
| 749 #endif | |
| 750 | 751 |
| 751 } // namespace base | 752 } // namespace base |
| OLD | NEW |