| OLD | NEW |
| (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) { |
| 41 std::string histogram("Testing.UMA.HistogramEnumeration"); |
| 42 HistogramTester tester; |
| 43 UmaHistogramEnumeration( |
| 44 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, |
| 51 static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 10, |
| 52 UMA_HISTOGRAM_TESTING_ENUM_THIRD); |
| 53 tester.ExpectBucketCount( |
| 54 histogram, |
| 55 static_cast<int>(UMA_HISTOGRAM_TESTING_ENUM_THIRD) + 1, 1); |
| 56 tester.ExpectTotalCount(histogram, 2); |
| 57 } |
| 58 |
| 59 TEST(HistogramFunctionsTest, HistogramBoolean) { |
| 60 std::string histogram("Testing.UMA.HistogramBoolean"); |
| 61 HistogramTester tester; |
| 62 UmaHistogramBoolean(histogram, true); |
| 63 tester.ExpectUniqueSample(histogram, 1, 1); |
| 64 UmaHistogramBoolean(histogram, false); |
| 65 tester.ExpectBucketCount(histogram, 0, 1); |
| 66 tester.ExpectTotalCount(histogram, 2); |
| 67 } |
| 68 |
| 69 TEST(HistogramFunctionsTest, HistogramPercentage) { |
| 70 std::string histogram("Testing.UMA.HistogramPercentage"); |
| 71 HistogramTester tester; |
| 72 UmaHistogramPercentage(histogram, 50); |
| 73 tester.ExpectUniqueSample(histogram, 50, 1); |
| 74 // Test overflows. |
| 75 UmaHistogramPercentage(histogram, 110); |
| 76 tester.ExpectBucketCount(histogram, 101, 1); |
| 77 tester.ExpectTotalCount(histogram, 2); |
| 78 } |
| 79 |
| 80 TEST(HistogramFunctionsTest, HistogramCounts) { |
| 81 std::string histogram("Testing.UMA.HistogramCount.Custom"); |
| 82 HistogramTester tester; |
| 83 UmaHistogramCustomCounts(histogram, 10, 1, 100, 10); |
| 84 tester.ExpectUniqueSample(histogram, 10, 1); |
| 85 UmaHistogramCustomCounts(histogram, 20, 1, 100, 10); |
| 86 UmaHistogramCustomCounts(histogram, 20, 1, 100, 10); |
| 87 UmaHistogramCustomCounts(histogram, 20, 1, 100, 10); |
| 88 tester.ExpectBucketCount(histogram, 20, 3); |
| 89 tester.ExpectTotalCount(histogram, 4); |
| 90 UmaHistogramCustomCounts(histogram, 110, 1, 100, 10); |
| 91 tester.ExpectBucketCount(histogram, 101, 1); |
| 92 tester.ExpectTotalCount(histogram, 5); |
| 93 } |
| 94 |
| 95 TEST(HistogramFunctionsTest, HistogramTimes) { |
| 96 std::string histogram("Testing.UMA.HistogramTimes"); |
| 97 HistogramTester tester; |
| 98 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(1)); |
| 99 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(1), 1); |
| 100 tester.ExpectTotalCount(histogram, 1); |
| 101 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(9)); |
| 102 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(9), 1); |
| 103 tester.ExpectTotalCount(histogram, 2); |
| 104 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(10)); // Overflows |
| 105 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(10), 1); |
| 106 UmaHistogramTimes(histogram, TimeDelta::FromSeconds(20)); // Overflows. |
| 107 // Check the value by picking any overflow time. |
| 108 tester.ExpectTimeBucketCount(histogram, TimeDelta::FromSeconds(11), 2); |
| 109 tester.ExpectTotalCount(histogram, 4); |
| 110 } |
| 111 |
| 112 } // namespace base. |
| OLD | NEW |