xref: /aosp_15_r20/external/cronet/base/metrics/histogram_functions_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
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"
8 #include "base/test/metrics/histogram_tester.h"
9 #include "base/time/time.h"
10 #include "testing/gmock/include/gmock/gmock.h"
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 
TEST(HistogramFunctionsTest,ExactLinear)21 TEST(HistogramFunctionsTest, ExactLinear) {
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 
TEST(HistogramFunctionsTest,Enumeration)41 TEST(HistogramFunctionsTest, Enumeration) {
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,
51       static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD));
52   tester.ExpectBucketCount(
53       histogram, static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 1, 1);
54   tester.ExpectTotalCount(histogram, 2);
55 }
56 
TEST(HistogramFunctionsTest,Boolean)57 TEST(HistogramFunctionsTest, Boolean) {
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 
TEST(HistogramFunctionsTest,Percentage)67 TEST(HistogramFunctionsTest, Percentage) {
68   std::string histogram("Testing.UMA.HistogramPercentage");
69   HistogramTester tester;
70   UmaHistogramPercentage(histogram, 1);
71   tester.ExpectBucketCount(histogram, 1, 1);
72   tester.ExpectTotalCount(histogram, 1);
73 
74   UmaHistogramPercentage(histogram, 50);
75   tester.ExpectBucketCount(histogram, 50, 1);
76   tester.ExpectTotalCount(histogram, 2);
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);
89 }
90 
TEST(HistogramFunctionsTest,Counts)91 TEST(HistogramFunctionsTest, Counts) {
92   std::string histogram("Testing.UMA.HistogramCount.Custom");
93   HistogramTester tester;
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.
102   UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
103   UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
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)));
124 }
125 
TEST(HistogramFunctionsTest,Times)126 TEST(HistogramFunctionsTest, Times) {
127   std::string histogram("Testing.UMA.HistogramTimes");
128   HistogramTester tester;
129   UmaHistogramTimes(histogram, Seconds(1));
130   tester.ExpectTimeBucketCount(histogram, Seconds(1), 1);
131   tester.ExpectTotalCount(histogram, 1);
132   UmaHistogramTimes(histogram, Seconds(9));
133   tester.ExpectTimeBucketCount(histogram, Seconds(9), 1);
134   tester.ExpectTotalCount(histogram, 2);
135   UmaHistogramTimes(histogram, Seconds(10));  // Overflows
136   tester.ExpectTimeBucketCount(histogram, Seconds(10), 1);
137   UmaHistogramTimes(histogram, Seconds(20));  // Overflows.
138   // Check the value by picking any overflow time.
139   tester.ExpectTimeBucketCount(histogram, Seconds(11), 2);
140   tester.ExpectTotalCount(histogram, 4);
141 }
142 
TEST(HistogramFunctionsTest,Sparse_SupportsLargeRange)143 TEST(HistogramFunctionsTest, Sparse_SupportsLargeRange) {
144   std::string histogram("Testing.UMA.HistogramSparse");
145   HistogramTester tester;
146   UmaHistogramSparse(histogram, 0);
147   UmaHistogramSparse(histogram, 123456789);
148   UmaHistogramSparse(histogram, 123456789);
149   EXPECT_THAT(tester.GetAllSamples(histogram),
150               testing::ElementsAre(Bucket(0, 1), Bucket(123456789, 2)));
151 }
152 
TEST(HistogramFunctionsTest,Sparse_SupportsNegativeValues)153 TEST(HistogramFunctionsTest, Sparse_SupportsNegativeValues) {
154   std::string histogram("Testing.UMA.HistogramSparse");
155   HistogramTester tester;
156   UmaHistogramSparse(histogram, -1);
157   tester.ExpectUniqueSample(histogram, -1, 1);
158 }
159 
160 }  // namespace base.
161