Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/metrics/histogram_functions.h" |
| 6 | |
| 7 | #include "base/metrics/histogram_macros.h" |
Devlin Cronin | 15291e9c | 2018-06-07 21:37:48 | [diff] [blame] | 8 | #include "base/test/metrics/histogram_tester.h" |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 9 | #include "base/time/time.h" |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 10 | #include "testing/gmock/include/gmock/gmock.h" |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace base { |
| 14 | |
| 15 | enum UmaHistogramTestingEnum { |
| 16 | UMA_HISTOGRAM_TESTING_ENUM_FIRST, |
| 17 | UMA_HISTOGRAM_TESTING_ENUM_SECOND, |
| 18 | UMA_HISTOGRAM_TESTING_ENUM_THIRD |
| 19 | }; |
| 20 | |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 21 | TEST(HistogramFunctionsTest, ExactLinear) { |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 22 | std::string histogram("Testing.UMA.HistogramExactLinear"); |
| 23 | HistogramTester tester; |
| 24 | UmaHistogramExactLinear(histogram, 10, 100); |
| 25 | tester.ExpectUniqueSample(histogram, 10, 1); |
| 26 | UmaHistogramExactLinear(histogram, 20, 100); |
| 27 | UmaHistogramExactLinear(histogram, 10, 100); |
| 28 | tester.ExpectBucketCount(histogram, 10, 2); |
| 29 | tester.ExpectBucketCount(histogram, 20, 1); |
| 30 | tester.ExpectTotalCount(histogram, 3); |
| 31 | // Test linear buckets overflow. |
| 32 | UmaHistogramExactLinear(histogram, 200, 100); |
| 33 | tester.ExpectBucketCount(histogram, 101, 1); |
| 34 | tester.ExpectTotalCount(histogram, 4); |
| 35 | // Test linear buckets underflow. |
| 36 | UmaHistogramExactLinear(histogram, 0, 100); |
| 37 | tester.ExpectBucketCount(histogram, 0, 1); |
| 38 | tester.ExpectTotalCount(histogram, 5); |
| 39 | } |
| 40 | |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 41 | TEST(HistogramFunctionsTest, Enumeration) { |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 42 | std::string histogram("Testing.UMA.HistogramEnumeration"); |
| 43 | HistogramTester tester; |
| 44 | UmaHistogramEnumeration(histogram, UMA_HISTOGRAM_TESTING_ENUM_FIRST, |
| 45 | UMA_HISTOGRAM_TESTING_ENUM_THIRD); |
| 46 | tester.ExpectUniqueSample(histogram, UMA_HISTOGRAM_TESTING_ENUM_FIRST, 1); |
| 47 | |
| 48 | // Verify the overflow & underflow bucket exists. |
| 49 | UMA_HISTOGRAM_ENUMERATION( |
| 50 | histogram, static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 10, |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 51 | static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD)); |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 52 | tester.ExpectBucketCount( |
| 53 | histogram, static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 1, 1); |
| 54 | tester.ExpectTotalCount(histogram, 2); |
| 55 | } |
| 56 | |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 57 | TEST(HistogramFunctionsTest, Boolean) { |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 58 | std::string histogram("Testing.UMA.HistogramBoolean"); |
| 59 | HistogramTester tester; |
| 60 | UmaHistogramBoolean(histogram, true); |
| 61 | tester.ExpectUniqueSample(histogram, 1, 1); |
| 62 | UmaHistogramBoolean(histogram, false); |
| 63 | tester.ExpectBucketCount(histogram, 0, 1); |
| 64 | tester.ExpectTotalCount(histogram, 2); |
| 65 | } |
| 66 | |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 67 | TEST(HistogramFunctionsTest, Percentage) { |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 68 | std::string histogram("Testing.UMA.HistogramPercentage"); |
| 69 | HistogramTester tester; |
Yue Ru Sun | 702f7460 | 2020-10-16 00:05:11 | [diff] [blame] | 70 | UmaHistogramPercentage(histogram, 1); |
| 71 | tester.ExpectBucketCount(histogram, 1, 1); |
| 72 | tester.ExpectTotalCount(histogram, 1); |
| 73 | |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 74 | UmaHistogramPercentage(histogram, 50); |
Yue Ru Sun | 702f7460 | 2020-10-16 00:05:11 | [diff] [blame] | 75 | tester.ExpectBucketCount(histogram, 50, 1); |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 76 | tester.ExpectTotalCount(histogram, 2); |
Yue Ru Sun | 702f7460 | 2020-10-16 00:05:11 | [diff] [blame] | 77 | |
| 78 | UmaHistogramPercentage(histogram, 100); |
| 79 | tester.ExpectBucketCount(histogram, 100, 1); |
| 80 | tester.ExpectTotalCount(histogram, 3); |
| 81 | // Test overflows. |
| 82 | UmaHistogramPercentage(histogram, 101); |
| 83 | tester.ExpectBucketCount(histogram, 101, 1); |
| 84 | tester.ExpectTotalCount(histogram, 4); |
| 85 | |
| 86 | UmaHistogramPercentage(histogram, 500); |
| 87 | tester.ExpectBucketCount(histogram, 101, 2); |
| 88 | tester.ExpectTotalCount(histogram, 5); |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 89 | } |
| 90 | |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 91 | TEST(HistogramFunctionsTest, Counts) { |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 92 | std::string histogram("Testing.UMA.HistogramCount.Custom"); |
| 93 | HistogramTester tester; |
Caitlin Fischer | fc138c8 | 2021-11-04 21:31:19 | [diff] [blame] | 94 | |
| 95 | // Add a sample that should go into the underflow bucket. |
| 96 | UmaHistogramCustomCounts(histogram, 0, 1, 100, 10); |
| 97 | |
| 98 | // Add a sample that should go into the first bucket. |
| 99 | UmaHistogramCustomCounts(histogram, 1, 1, 100, 10); |
| 100 | |
| 101 | // Add multiple samples that should go into the same bucket. |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 102 | UmaHistogramCustomCounts(histogram, 20, 1, 100, 10); |
| 103 | UmaHistogramCustomCounts(histogram, 20, 1, 100, 10); |
Caitlin Fischer | fc138c8 | 2021-11-04 21:31:19 | [diff] [blame] | 104 | UmaHistogramCustomCounts(histogram, 21, 1, 100, 10); |
| 105 | |
| 106 | // Add a sample that should go into the last bucket. |
| 107 | UmaHistogramCustomCounts(histogram, 99, 1, 100, 10); |
| 108 | |
| 109 | // Add some samples that should go into the overflow bucket. |
| 110 | UmaHistogramCustomCounts(histogram, 100, 1, 100, 10); |
| 111 | UmaHistogramCustomCounts(histogram, 101, 1, 100, 10); |
| 112 | |
| 113 | // Verify the number of samples. |
| 114 | tester.ExpectTotalCount(histogram, 8); |
| 115 | |
| 116 | // Verify the following: |
| 117 | // (a) The underflow bucket [0, 1) contains one sample. |
| 118 | // (b) The first and last buckets each contain one sample. |
| 119 | // (c) The bucket for values in [16, 29) contains three samples. |
| 120 | // (d) The overflow bucket contains two samples. |
| 121 | EXPECT_THAT(tester.GetAllSamples(histogram), |
| 122 | testing::ElementsAre(Bucket(0, 1), Bucket(1, 1), Bucket(16, 3), |
| 123 | Bucket(54, 1), Bucket(100, 2))); |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 124 | } |
| 125 | |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 126 | TEST(HistogramFunctionsTest, Times) { |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 127 | std::string histogram("Testing.UMA.HistogramTimes"); |
| 128 | HistogramTester tester; |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 129 | UmaHistogramTimes(histogram, Seconds(1)); |
| 130 | tester.ExpectTimeBucketCount(histogram, Seconds(1), 1); |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 131 | tester.ExpectTotalCount(histogram, 1); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 132 | UmaHistogramTimes(histogram, Seconds(9)); |
| 133 | tester.ExpectTimeBucketCount(histogram, Seconds(9), 1); |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 134 | tester.ExpectTotalCount(histogram, 2); |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 135 | UmaHistogramTimes(histogram, Seconds(10)); // Overflows |
| 136 | tester.ExpectTimeBucketCount(histogram, Seconds(10), 1); |
| 137 | UmaHistogramTimes(histogram, Seconds(20)); // Overflows. |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 138 | // Check the value by picking any overflow time. |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 139 | tester.ExpectTimeBucketCount(histogram, Seconds(11), 2); |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 140 | tester.ExpectTotalCount(histogram, 4); |
| 141 | } |
| 142 | |
Tanushree Bisht | ce31a6f9 | 2024-05-30 19:57:11 | [diff] [blame] | 143 | TEST(HistogramFunctionsTest, ScopedTimes) { |
| 144 | std::string histogram("Testing.UMA.HistogramScopedTimes"); |
| 145 | HistogramTester tester; |
| 146 | { ScopedUmaHistogramTimer scoped_uma_histogram_timer(histogram); } |
| 147 | tester.ExpectTotalCount(histogram, 1); |
| 148 | { ScopedUmaHistogramTimer scoped_uma_histogram_timer(histogram); } |
| 149 | tester.ExpectTotalCount(histogram, 2); |
| 150 | } |
| 151 | |
Ilya Sherman | 16d5d5f4 | 2017-12-08 00:32:44 | [diff] [blame] | 152 | TEST(HistogramFunctionsTest, Sparse_SupportsLargeRange) { |
| 153 | std::string histogram("Testing.UMA.HistogramSparse"); |
| 154 | HistogramTester tester; |
| 155 | UmaHistogramSparse(histogram, 0); |
| 156 | UmaHistogramSparse(histogram, 123456789); |
| 157 | UmaHistogramSparse(histogram, 123456789); |
| 158 | EXPECT_THAT(tester.GetAllSamples(histogram), |
| 159 | testing::ElementsAre(Bucket(0, 1), Bucket(123456789, 2))); |
| 160 | } |
| 161 | |
| 162 | TEST(HistogramFunctionsTest, Sparse_SupportsNegativeValues) { |
| 163 | std::string histogram("Testing.UMA.HistogramSparse"); |
| 164 | HistogramTester tester; |
| 165 | UmaHistogramSparse(histogram, -1); |
| 166 | tester.ExpectUniqueSample(histogram, -1, 1); |
| 167 | } |
| 168 | |
nikunjb | 09d9450 | 2016-11-14 22:06:25 | [diff] [blame] | 169 | } // namespace base. |