Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: base/metrics/histogram_functions_unittest.cc

Issue 2393493002: Expose macros from base/metrics/histogram_macros.h as functions (Closed)
Patch Set: Review comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
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/histogram_tester.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace base {
13
14 enum UmaHistogramTestingEnum {
15 UMA_HISTOGRAM_TESTING_ENUM_FIRST,
16 UMA_HISTOGRAM_TESTING_ENUM_SECOND,
17 UMA_HISTOGRAM_TESTING_ENUM_THIRD
18 };
19
20 TEST(HistogramFunctionsTest, HistogramExactLinear) {
21 std::string histogram("Testing.UMA.HistogramExactLinear");
22 HistogramTester tester;
23 UmaHistogramExactLinear(histogram, 10, 100);
24 tester.ExpectUniqueSample(histogram, 10, 1);
25 UmaHistogramExactLinear(histogram, 20, 100);
26 UmaHistogramExactLinear(histogram, 10, 100);
27 tester.ExpectBucketCount(histogram, 10, 2);
28 tester.ExpectBucketCount(histogram, 20, 1);
29 tester.ExpectTotalCount(histogram, 3);
30 // Test linear buckets overflow.
31 UmaHistogramExactLinear(histogram, 200, 100);
32 tester.ExpectBucketCount(histogram, 101, 1);
33 tester.ExpectTotalCount(histogram, 4);
34 // Test linear buckets underflow.
35 UmaHistogramExactLinear(histogram, 0, 100);
36 tester.ExpectBucketCount(histogram, 0, 1);
37 tester.ExpectTotalCount(histogram, 5);
38 }
39
40 TEST(HistogramFunctionsTest, HistogramEnumeration) {
wychen 2016/11/14 23:56:11 Might worth adding some no-compile tests. Alas it
41 std::string histogram("Testing.UMA.HistogramEnumeration");
42 HistogramTester tester;
43 UmaHistogramEnumeration(histogram, UMA_HISTOGRAM_TESTING_ENUM_FIRST,
44 UMA_HISTOGRAM_TESTING_ENUM_THIRD);
45 tester.ExpectUniqueSample(histogram, UMA_HISTOGRAM_TESTING_ENUM_FIRST, 1);
46
47 // Verify the overflow & underflow bucket exists.
48 UMA_HISTOGRAM_ENUMERATION(
49 histogram, static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 10,
50 UMA_HISTOGRAM_TESTING_ENUM_THIRD);
51 tester.ExpectBucketCount(
52 histogram, static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 1, 1);
53 tester.ExpectTotalCount(histogram, 2);
54 }
55
56 TEST(HistogramFunctionsTest, HistogramBoolean) {
57 std::string histogram("Testing.UMA.HistogramBoolean");
58 HistogramTester tester;
59 UmaHistogramBoolean(histogram, true);
60 tester.ExpectUniqueSample(histogram, 1, 1);
61 UmaHistogramBoolean(histogram, false);
62 tester.ExpectBucketCount(histogram, 0, 1);
63 tester.ExpectTotalCount(histogram, 2);
64 }
65
66 TEST(HistogramFunctionsTest, HistogramPercentage) {
67 std::string histogram("Testing.UMA.HistogramPercentage");
68 HistogramTester tester;
69 UmaHistogramPercentage(histogram, 50);
70 tester.ExpectUniqueSample(histogram, 50, 1);
71 // Test overflows.
72 UmaHistogramPercentage(histogram, 110);
73 tester.ExpectBucketCount(histogram, 101, 1);
74 tester.ExpectTotalCount(histogram, 2);
75 }
76
77 TEST(HistogramFunctionsTest, HistogramCounts) {
78 std::string histogram("Testing.UMA.HistogramCount.Custom");
79 HistogramTester tester;
80 UmaHistogramCustomCounts(histogram, 10, 1, 100, 10);
81 tester.ExpectUniqueSample(histogram, 10, 1);
82 UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
83 UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
84 UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
85 tester.ExpectBucketCount(histogram, 20, 3);
86 tester.ExpectTotalCount(histogram, 4);
87 UmaHistogramCustomCounts(histogram, 110, 1, 100, 10);
88 tester.ExpectBucketCount(histogram, 101, 1);
89 tester.ExpectTotalCount(histogram, 5);
90 }
91
92 TEST(HistogramFunctionsTest, HistogramTimes) {
93 std::string histogram("Testing.UMA.HistogramTimes");
94 HistogramTester tester;
95 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(1));
96 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(1), 1);
97 tester.ExpectTotalCount(histogram, 1);
98 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(9));
99 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(9), 1);
100 tester.ExpectTotalCount(histogram, 2);
101 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(10)); // Overflows
102 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(10), 1);
103 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(20)); // Overflows.
104 // Check the value by picking any overflow time.
105 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(11), 2);
106 tester.ExpectTotalCount(histogram, 4);
107 }
108
109 } // namespace base.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698