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

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: Add Histogram functions API implementation and tests 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 (c) 2012 The Chromium Authors. All rights reserved.
Alexei Svitkine (slow) 2016/11/12 00:51:14 2016, no (c)
nikunjb 2016/11/12 01:35:15 Done.
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"
Alexei Svitkine (slow) 2016/11/12 00:51:14 Nit: Add an empty line after this.
nikunjb 2016/11/12 01:35:15 Done.
6 #include "base/metrics/histogram_macros.h"
7 #include "base/test/histogram_tester.h"
8 #include "base/time/time.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace base {
12
13 enum UmaHistogramTestingEnum {
14 UMA_HISTOGRAM_TESTING_ENUM_FIRST,
15 UMA_HISTOGRAM_TESTING_ENUM_SECOND,
16 UMA_HISTOGRAM_TESTING_ENUM_THIRD
17 };
18
19 TEST(HistogramFunctionsTest, HistogramExactLinear) {
20 std::string histogram("Testing.UMA.HistogramExactLinear");
21 HistogramTester tester;
22 UmaHistogramExactLinear(histogram, 10, 100);
23 tester.ExpectUniqueSample(histogram, 10, 1);
24 UmaHistogramExactLinear(histogram, 20, 100);
25 UmaHistogramExactLinear(histogram, 10, 100);
26 tester.ExpectBucketCount(histogram, 10, 2);
27 tester.ExpectBucketCount(histogram, 20, 1);
28 tester.ExpectTotalCount(histogram, 3);
29 // Test linear buckets overflow.
30 UmaHistogramExactLinear(histogram, 200, 100);
31 tester.ExpectBucketCount(histogram, 101, 1);
32 tester.ExpectTotalCount(histogram, 4);
33 // Test linear buckets underflow.
34 UmaHistogramExactLinear(histogram, 0, 100);
35 tester.ExpectBucketCount(histogram, 0, 1);
36 tester.ExpectTotalCount(histogram, 5);
37 }
38
39 TEST(HistogramFunctionsTest, HistogramEnumeration) {
40 std::string histogram("Testing.UMA.HistogramEnumeration");
41 HistogramTester tester;
42 UmaHistogramEnumeration(
43 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,
50 static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 10,
51 UMA_HISTOGRAM_TESTING_ENUM_THIRD);
52 tester.ExpectBucketCount(
53 histogram,
54 static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 1, 1);
55 tester.ExpectTotalCount(histogram, 2);
56 }
57
58 TEST(HistogramFunctionsTest, HistogramBoolean) {
59 std::string histogram("Testing.UMA.HistogramBoolean");
60 HistogramTester tester;
61 UmaHistogramBoolean(histogram, true);
62 tester.ExpectUniqueSample(histogram, 1, 1);
63 UmaHistogramBoolean(histogram, false);
64 tester.ExpectBucketCount(histogram, 0, 1);
65 tester.ExpectTotalCount(histogram, 2);
66 }
67
68 TEST(HistogramFunctionsTest, HistogramPercentage) {
69 std::string histogram("Testing.UMA.HistogramPercentage");
70 HistogramTester tester;
71 UmaHistogramPercentage(histogram, 50);
72 tester.ExpectUniqueSample(histogram, 50, 1);
73 // Test overflows.
74 UmaHistogramPercentage(histogram, 110);
75 tester.ExpectBucketCount(histogram, 101, 1);
76 tester.ExpectTotalCount(histogram, 2);
77 }
78
79 TEST(HistogramFunctionsTest, HistogramCounts) {
80 std::string histogram("Testing.UMA.HistogramCount.Custom");
81 HistogramTester tester;
82 UmaHistogramCustomCounts(histogram, 10, 1, 100, 10);
83 tester.ExpectUniqueSample(histogram, 10, 1);
84 UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
85 UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
86 UmaHistogramCustomCounts(histogram, 20, 1, 100, 10);
87 tester.ExpectBucketCount(histogram, 20, 3);
88 tester.ExpectTotalCount(histogram, 4);
89 UmaHistogramCustomCounts(histogram, 110, 1, 100, 10);
90 tester.ExpectBucketCount(histogram, 101, 1);
91 tester.ExpectTotalCount(histogram, 5);
92 }
93
94 TEST(HistogramFunctionsTest, HistogramTimes) {
95 std::string histogram("Testing.UMA.HistogramTimes");
96 HistogramTester tester;
97 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(1));
98 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(1), 1);
99 tester.ExpectTotalCount(histogram, 1);
100 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(9));
101 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(9), 1);
102 tester.ExpectTotalCount(histogram, 2);
103 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(10)); // Overflows
104 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(10), 1);
105 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(20)); // Overflows.
106 // Check the value by picking any overflow time.
107 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(11), 2);
108 tester.ExpectTotalCount(histogram, 4);
109 }
110
111 } // namespace base.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698