| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Test of Histogram class | 5 // Test of Histogram class |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/scoped_ptr.h" |
| 8 #include "base/time.h" | 9 #include "base/time.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 11 |
| 11 namespace base { | 12 namespace base { |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 class HistogramTest : public testing::Test { | 15 class HistogramTest : public testing::Test { |
| 15 }; | 16 }; |
| 16 | 17 |
| 17 // Check for basic syntax and use. | 18 // Check for basic syntax and use. |
| 18 TEST(HistogramTest, StartupShutdownTest) { | 19 TEST(HistogramTest, StartupShutdownTest) { |
| 19 // Try basic construction | 20 // Try basic construction |
| 20 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( | 21 Histogram* histogram(Histogram::FactoryGet( |
| 21 "TestHistogram", 1, 1000, 10, Histogram::kNoFlags); | 22 "TestHistogram", 1, 1000, 10, Histogram::kNoFlags)); |
| 22 scoped_refptr<Histogram> histogram1 = Histogram::FactoryGet( | 23 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), histogram); |
| 23 "Test1Histogram", 1, 1000, 10, Histogram::kNoFlags); | 24 Histogram* histogram1(Histogram::FactoryGet( |
| 25 "Test1Histogram", 1, 1000, 10, Histogram::kNoFlags)); |
| 26 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), histogram1); |
| 27 EXPECT_NE(histogram, histogram1); |
| 24 | 28 |
| 25 scoped_refptr<Histogram> linear_histogram = LinearHistogram::FactoryGet( | 29 |
| 26 "TestLinearHistogram", 1, 1000, 10, Histogram::kNoFlags); | 30 Histogram* linear_histogram(LinearHistogram::FactoryGet( |
| 27 scoped_refptr<Histogram> linear_histogram1 = LinearHistogram::FactoryGet( | 31 "TestLinearHistogram", 1, 1000, 10, Histogram::kNoFlags)); |
| 28 "Test1LinearHistogram", 1, 1000, 10, Histogram::kNoFlags); | 32 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), linear_histogram); |
| 33 Histogram* linear_histogram1(LinearHistogram::FactoryGet( |
| 34 "Test1LinearHistogram", 1, 1000, 10, Histogram::kNoFlags)); |
| 35 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), linear_histogram1); |
| 36 EXPECT_NE(linear_histogram, linear_histogram1); |
| 29 | 37 |
| 30 std::vector<int> custom_ranges; | 38 std::vector<int> custom_ranges; |
| 31 custom_ranges.push_back(1); | 39 custom_ranges.push_back(1); |
| 32 custom_ranges.push_back(5); | 40 custom_ranges.push_back(5); |
| 33 custom_ranges.push_back(10); | 41 custom_ranges.push_back(10); |
| 34 custom_ranges.push_back(20); | 42 custom_ranges.push_back(20); |
| 35 custom_ranges.push_back(30); | 43 custom_ranges.push_back(30); |
| 36 scoped_refptr<Histogram> custom_histogram = CustomHistogram::FactoryGet( | 44 Histogram* custom_histogram(CustomHistogram::FactoryGet( |
| 37 "TestCustomHistogram", custom_ranges, Histogram::kNoFlags); | 45 "TestCustomHistogram", custom_ranges, Histogram::kNoFlags)); |
| 38 scoped_refptr<Histogram> custom_histogram1 = CustomHistogram::FactoryGet( | 46 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), custom_histogram); |
| 39 "Test1CustomHistogram", custom_ranges, Histogram::kNoFlags); | 47 Histogram* custom_histogram1(CustomHistogram::FactoryGet( |
| 48 "Test1CustomHistogram", custom_ranges, Histogram::kNoFlags)); |
| 49 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), custom_histogram1); |
| 40 | 50 |
| 41 // Use standard macros (but with fixed samples) | 51 // Use standard macros (but with fixed samples) |
| 42 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); | 52 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); |
| 43 HISTOGRAM_COUNTS("Test3Histogram", 30); | 53 HISTOGRAM_COUNTS("Test3Histogram", 30); |
| 44 | 54 |
| 45 DHISTOGRAM_TIMES("Test4Histogram", TimeDelta::FromDays(1)); | 55 DHISTOGRAM_TIMES("Test4Histogram", TimeDelta::FromDays(1)); |
| 46 DHISTOGRAM_COUNTS("Test5Histogram", 30); | 56 DHISTOGRAM_COUNTS("Test5Histogram", 30); |
| 47 | 57 |
| 48 HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130); | 58 HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130); |
| 49 | 59 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 63 TEST(HistogramTest, RecordedStartupTest) { | 73 TEST(HistogramTest, RecordedStartupTest) { |
| 64 // Test a statistics recorder, by letting histograms register. | 74 // Test a statistics recorder, by letting histograms register. |
| 65 StatisticsRecorder recorder; // This initializes the global state. | 75 StatisticsRecorder recorder; // This initializes the global state. |
| 66 | 76 |
| 67 StatisticsRecorder::Histograms histograms; | 77 StatisticsRecorder::Histograms histograms; |
| 68 EXPECT_EQ(0U, histograms.size()); | 78 EXPECT_EQ(0U, histograms.size()); |
| 69 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 79 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 70 EXPECT_EQ(0U, histograms.size()); | 80 EXPECT_EQ(0U, histograms.size()); |
| 71 | 81 |
| 72 // Try basic construction | 82 // Try basic construction |
| 73 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( | 83 Histogram* histogram(Histogram::FactoryGet( |
| 74 "TestHistogram", 1, 1000, 10, Histogram::kNoFlags); | 84 "TestHistogram", 1, 1000, 10, Histogram::kNoFlags)); |
| 85 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), histogram); |
| 75 histograms.clear(); | 86 histograms.clear(); |
| 76 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 87 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 77 EXPECT_EQ(1U, histograms.size()); | 88 EXPECT_EQ(1U, histograms.size()); |
| 78 scoped_refptr<Histogram> histogram1 = Histogram::FactoryGet( | 89 Histogram* histogram1(Histogram::FactoryGet( |
| 79 "Test1Histogram", 1, 1000, 10, Histogram::kNoFlags); | 90 "Test1Histogram", 1, 1000, 10, Histogram::kNoFlags)); |
| 91 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), histogram1); |
| 80 histograms.clear(); | 92 histograms.clear(); |
| 81 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 93 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 82 EXPECT_EQ(2U, histograms.size()); | 94 EXPECT_EQ(2U, histograms.size()); |
| 83 | 95 |
| 84 scoped_refptr<Histogram> linear_histogram = LinearHistogram::FactoryGet( | 96 Histogram* linear_histogram(LinearHistogram::FactoryGet( |
| 85 "TestLinearHistogram", 1, 1000, 10, Histogram::kNoFlags); | 97 "TestLinearHistogram", 1, 1000, 10, Histogram::kNoFlags)); |
| 98 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), linear_histogram); |
| 86 histograms.clear(); | 99 histograms.clear(); |
| 87 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 100 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 88 EXPECT_EQ(3U, histograms.size()); | 101 EXPECT_EQ(3U, histograms.size()); |
| 89 | 102 |
| 90 scoped_refptr<Histogram> linear_histogram1 = LinearHistogram::FactoryGet( | 103 Histogram* linear_histogram1(LinearHistogram::FactoryGet( |
| 91 "Test1LinearHistogram", 1, 1000, 10, Histogram::kNoFlags); | 104 "Test1LinearHistogram", 1, 1000, 10, Histogram::kNoFlags)); |
| 105 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), linear_histogram1); |
| 92 histograms.clear(); | 106 histograms.clear(); |
| 93 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 107 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 94 EXPECT_EQ(4U, histograms.size()); | 108 EXPECT_EQ(4U, histograms.size()); |
| 95 | 109 |
| 96 std::vector<int> custom_ranges; | 110 std::vector<int> custom_ranges; |
| 97 custom_ranges.push_back(1); | 111 custom_ranges.push_back(1); |
| 98 custom_ranges.push_back(5); | 112 custom_ranges.push_back(5); |
| 99 custom_ranges.push_back(10); | 113 custom_ranges.push_back(10); |
| 100 custom_ranges.push_back(20); | 114 custom_ranges.push_back(20); |
| 101 custom_ranges.push_back(30); | 115 custom_ranges.push_back(30); |
| 102 scoped_refptr<Histogram> custom_histogram = CustomHistogram::FactoryGet( | 116 Histogram* custom_histogram(CustomHistogram::FactoryGet( |
| 103 "TestCustomHistogram", custom_ranges, Histogram::kNoFlags); | 117 "TestCustomHistogram", custom_ranges, Histogram::kNoFlags)); |
| 104 scoped_refptr<Histogram> custom_histogram1 = CustomHistogram::FactoryGet( | 118 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), custom_histogram); |
| 105 "TestCustomHistogram", custom_ranges, Histogram::kNoFlags); | 119 Histogram* custom_histogram1(CustomHistogram::FactoryGet( |
| 120 "TestCustomHistogram", custom_ranges, Histogram::kNoFlags)); |
| 121 EXPECT_NE(reinterpret_cast<Histogram*>(NULL), custom_histogram1); |
| 106 | 122 |
| 107 histograms.clear(); | 123 histograms.clear(); |
| 108 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 124 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 109 EXPECT_EQ(5U, histograms.size()); | 125 EXPECT_EQ(5U, histograms.size()); |
| 110 | 126 |
| 111 // Use standard macros (but with fixed samples) | 127 // Use standard macros (but with fixed samples) |
| 112 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); | 128 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); |
| 113 HISTOGRAM_COUNTS("Test3Histogram", 30); | 129 HISTOGRAM_COUNTS("Test3Histogram", 30); |
| 114 histograms.clear(); | 130 histograms.clear(); |
| 115 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 131 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| (...skipping 15 matching lines...) Expand all Loading... |
| 131 #endif | 147 #endif |
| 132 } | 148 } |
| 133 | 149 |
| 134 TEST(HistogramTest, RangeTest) { | 150 TEST(HistogramTest, RangeTest) { |
| 135 StatisticsRecorder recorder; | 151 StatisticsRecorder recorder; |
| 136 StatisticsRecorder::Histograms histograms; | 152 StatisticsRecorder::Histograms histograms; |
| 137 | 153 |
| 138 recorder.GetHistograms(&histograms); | 154 recorder.GetHistograms(&histograms); |
| 139 EXPECT_EQ(0U, histograms.size()); | 155 EXPECT_EQ(0U, histograms.size()); |
| 140 | 156 |
| 141 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( | 157 Histogram* histogram(Histogram::FactoryGet( |
| 142 "Histogram", 1, 64, 8, Histogram::kNoFlags); // As per header file. | 158 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. |
| 143 // Check that we got a nice exponential when there was enough rooom. | 159 // Check that we got a nice exponential when there was enough rooom. |
| 144 EXPECT_EQ(0, histogram->ranges(0)); | 160 EXPECT_EQ(0, histogram->ranges(0)); |
| 145 int power_of_2 = 1; | 161 int power_of_2 = 1; |
| 146 for (int i = 1; i < 8; i++) { | 162 for (int i = 1; i < 8; i++) { |
| 147 EXPECT_EQ(power_of_2, histogram->ranges(i)); | 163 EXPECT_EQ(power_of_2, histogram->ranges(i)); |
| 148 power_of_2 *= 2; | 164 power_of_2 *= 2; |
| 149 } | 165 } |
| 150 EXPECT_EQ(INT_MAX, histogram->ranges(8)); | 166 EXPECT_EQ(INT_MAX, histogram->ranges(8)); |
| 151 | 167 |
| 152 scoped_refptr<Histogram> short_histogram = Histogram::FactoryGet( | 168 Histogram* short_histogram(Histogram::FactoryGet( |
| 153 "Histogram Shortened", 1, 7, 8, Histogram::kNoFlags); | 169 "Histogram Shortened", 1, 7, 8, Histogram::kNoFlags)); |
| 154 // Check that when the number of buckets is short, we get a linear histogram | 170 // Check that when the number of buckets is short, we get a linear histogram |
| 155 // for lack of space to do otherwise. | 171 // for lack of space to do otherwise. |
| 156 for (int i = 0; i < 8; i++) | 172 for (int i = 0; i < 8; i++) |
| 157 EXPECT_EQ(i, short_histogram->ranges(i)); | 173 EXPECT_EQ(i, short_histogram->ranges(i)); |
| 158 EXPECT_EQ(INT_MAX, short_histogram->ranges(8)); | 174 EXPECT_EQ(INT_MAX, short_histogram->ranges(8)); |
| 159 | 175 |
| 160 scoped_refptr<Histogram> linear_histogram = LinearHistogram::FactoryGet( | 176 Histogram* linear_histogram(LinearHistogram::FactoryGet( |
| 161 "Linear", 1, 7, 8, Histogram::kNoFlags); | 177 "Linear", 1, 7, 8, Histogram::kNoFlags)); |
| 162 // We also get a nice linear set of bucket ranges when we ask for it | 178 // We also get a nice linear set of bucket ranges when we ask for it |
| 163 for (int i = 0; i < 8; i++) | 179 for (int i = 0; i < 8; i++) |
| 164 EXPECT_EQ(i, linear_histogram->ranges(i)); | 180 EXPECT_EQ(i, linear_histogram->ranges(i)); |
| 165 EXPECT_EQ(INT_MAX, linear_histogram->ranges(8)); | 181 EXPECT_EQ(INT_MAX, linear_histogram->ranges(8)); |
| 166 | 182 |
| 167 scoped_refptr<Histogram> linear_broad_histogram = LinearHistogram::FactoryGet( | 183 Histogram* linear_broad_histogram(LinearHistogram::FactoryGet( |
| 168 "Linear widened", 2, 14, 8, Histogram::kNoFlags); | 184 "Linear widened", 2, 14, 8, Histogram::kNoFlags)); |
| 169 // ...but when the list has more space, then the ranges naturally spread out. | 185 // ...but when the list has more space, then the ranges naturally spread out. |
| 170 for (int i = 0; i < 8; i++) | 186 for (int i = 0; i < 8; i++) |
| 171 EXPECT_EQ(2 * i, linear_broad_histogram->ranges(i)); | 187 EXPECT_EQ(2 * i, linear_broad_histogram->ranges(i)); |
| 172 EXPECT_EQ(INT_MAX, linear_broad_histogram->ranges(8)); | 188 EXPECT_EQ(INT_MAX, linear_broad_histogram->ranges(8)); |
| 173 | 189 |
| 174 scoped_refptr<Histogram> transitioning_histogram = | 190 Histogram* transitioning_histogram(Histogram::FactoryGet( |
| 175 Histogram::FactoryGet("LinearAndExponential", 1, 32, 15, | 191 "LinearAndExponential", 1, 32, 15, Histogram::kNoFlags)); |
| 176 Histogram::kNoFlags); | |
| 177 // When space is a little tight, we transition from linear to exponential. | 192 // When space is a little tight, we transition from linear to exponential. |
| 178 EXPECT_EQ(0, transitioning_histogram->ranges(0)); | 193 EXPECT_EQ(0, transitioning_histogram->ranges(0)); |
| 179 EXPECT_EQ(1, transitioning_histogram->ranges(1)); | 194 EXPECT_EQ(1, transitioning_histogram->ranges(1)); |
| 180 EXPECT_EQ(2, transitioning_histogram->ranges(2)); | 195 EXPECT_EQ(2, transitioning_histogram->ranges(2)); |
| 181 EXPECT_EQ(3, transitioning_histogram->ranges(3)); | 196 EXPECT_EQ(3, transitioning_histogram->ranges(3)); |
| 182 EXPECT_EQ(4, transitioning_histogram->ranges(4)); | 197 EXPECT_EQ(4, transitioning_histogram->ranges(4)); |
| 183 EXPECT_EQ(5, transitioning_histogram->ranges(5)); | 198 EXPECT_EQ(5, transitioning_histogram->ranges(5)); |
| 184 EXPECT_EQ(6, transitioning_histogram->ranges(6)); | 199 EXPECT_EQ(6, transitioning_histogram->ranges(6)); |
| 185 EXPECT_EQ(7, transitioning_histogram->ranges(7)); | 200 EXPECT_EQ(7, transitioning_histogram->ranges(7)); |
| 186 EXPECT_EQ(9, transitioning_histogram->ranges(8)); | 201 EXPECT_EQ(9, transitioning_histogram->ranges(8)); |
| 187 EXPECT_EQ(11, transitioning_histogram->ranges(9)); | 202 EXPECT_EQ(11, transitioning_histogram->ranges(9)); |
| 188 EXPECT_EQ(14, transitioning_histogram->ranges(10)); | 203 EXPECT_EQ(14, transitioning_histogram->ranges(10)); |
| 189 EXPECT_EQ(17, transitioning_histogram->ranges(11)); | 204 EXPECT_EQ(17, transitioning_histogram->ranges(11)); |
| 190 EXPECT_EQ(21, transitioning_histogram->ranges(12)); | 205 EXPECT_EQ(21, transitioning_histogram->ranges(12)); |
| 191 EXPECT_EQ(26, transitioning_histogram->ranges(13)); | 206 EXPECT_EQ(26, transitioning_histogram->ranges(13)); |
| 192 EXPECT_EQ(32, transitioning_histogram->ranges(14)); | 207 EXPECT_EQ(32, transitioning_histogram->ranges(14)); |
| 193 EXPECT_EQ(INT_MAX, transitioning_histogram->ranges(15)); | 208 EXPECT_EQ(INT_MAX, transitioning_histogram->ranges(15)); |
| 194 | 209 |
| 195 std::vector<int> custom_ranges; | 210 std::vector<int> custom_ranges; |
| 196 custom_ranges.push_back(0); | 211 custom_ranges.push_back(0); |
| 197 custom_ranges.push_back(9); | 212 custom_ranges.push_back(9); |
| 198 custom_ranges.push_back(10); | 213 custom_ranges.push_back(10); |
| 199 custom_ranges.push_back(11); | 214 custom_ranges.push_back(11); |
| 200 custom_ranges.push_back(300); | 215 custom_ranges.push_back(300); |
| 201 scoped_refptr<Histogram> test_custom_histogram = CustomHistogram::FactoryGet( | 216 Histogram* test_custom_histogram(CustomHistogram::FactoryGet( |
| 202 "TestCustomRangeHistogram", custom_ranges, Histogram::kNoFlags); | 217 "TestCustomRangeHistogram", custom_ranges, Histogram::kNoFlags)); |
| 203 | 218 |
| 204 EXPECT_EQ(custom_ranges[0], test_custom_histogram->ranges(0)); | 219 EXPECT_EQ(custom_ranges[0], test_custom_histogram->ranges(0)); |
| 205 EXPECT_EQ(custom_ranges[1], test_custom_histogram->ranges(1)); | 220 EXPECT_EQ(custom_ranges[1], test_custom_histogram->ranges(1)); |
| 206 EXPECT_EQ(custom_ranges[2], test_custom_histogram->ranges(2)); | 221 EXPECT_EQ(custom_ranges[2], test_custom_histogram->ranges(2)); |
| 207 EXPECT_EQ(custom_ranges[3], test_custom_histogram->ranges(3)); | 222 EXPECT_EQ(custom_ranges[3], test_custom_histogram->ranges(3)); |
| 208 EXPECT_EQ(custom_ranges[4], test_custom_histogram->ranges(4)); | 223 EXPECT_EQ(custom_ranges[4], test_custom_histogram->ranges(4)); |
| 209 | 224 |
| 210 recorder.GetHistograms(&histograms); | 225 recorder.GetHistograms(&histograms); |
| 211 EXPECT_EQ(6U, histograms.size()); | 226 EXPECT_EQ(6U, histograms.size()); |
| 212 } | 227 } |
| 213 | 228 |
| 214 TEST(HistogramTest, CustomRangeTest) { | 229 TEST(HistogramTest, CustomRangeTest) { |
| 215 StatisticsRecorder recorder; | 230 StatisticsRecorder recorder; |
| 216 StatisticsRecorder::Histograms histograms; | 231 StatisticsRecorder::Histograms histograms; |
| 217 | 232 |
| 218 // Check that missing leading zero is handled by an auto-insertion. | 233 // Check that missing leading zero is handled by an auto-insertion. |
| 219 std::vector<int> custom_ranges; | 234 std::vector<int> custom_ranges; |
| 220 // Don't include a zero. | 235 // Don't include a zero. |
| 221 custom_ranges.push_back(9); | 236 custom_ranges.push_back(9); |
| 222 custom_ranges.push_back(10); | 237 custom_ranges.push_back(10); |
| 223 custom_ranges.push_back(11); | 238 custom_ranges.push_back(11); |
| 224 scoped_refptr<Histogram> test_custom_histogram = CustomHistogram::FactoryGet( | 239 Histogram* test_custom_histogram(CustomHistogram::FactoryGet( |
| 225 "TestCustomRangeHistogram", custom_ranges, Histogram::kNoFlags); | 240 "TestCustomRangeHistogram", custom_ranges, Histogram::kNoFlags)); |
| 226 | 241 |
| 227 EXPECT_EQ(0, test_custom_histogram->ranges(0)); // Auto added | 242 EXPECT_EQ(0, test_custom_histogram->ranges(0)); // Auto added |
| 228 EXPECT_EQ(custom_ranges[0], test_custom_histogram->ranges(1)); | 243 EXPECT_EQ(custom_ranges[0], test_custom_histogram->ranges(1)); |
| 229 EXPECT_EQ(custom_ranges[1], test_custom_histogram->ranges(2)); | 244 EXPECT_EQ(custom_ranges[1], test_custom_histogram->ranges(2)); |
| 230 EXPECT_EQ(custom_ranges[2], test_custom_histogram->ranges(3)); | 245 EXPECT_EQ(custom_ranges[2], test_custom_histogram->ranges(3)); |
| 231 | 246 |
| 232 // Check that unsorted data with dups is handled gracefully. | 247 // Check that unsorted data with dups is handled gracefully. |
| 233 const int kSmall = 7; | 248 const int kSmall = 7; |
| 234 const int kMid = 8; | 249 const int kMid = 8; |
| 235 const int kBig = 9; | 250 const int kBig = 9; |
| 236 custom_ranges.clear(); | 251 custom_ranges.clear(); |
| 237 custom_ranges.push_back(kBig); | 252 custom_ranges.push_back(kBig); |
| 238 custom_ranges.push_back(kMid); | 253 custom_ranges.push_back(kMid); |
| 239 custom_ranges.push_back(kSmall); | 254 custom_ranges.push_back(kSmall); |
| 240 custom_ranges.push_back(kSmall); | 255 custom_ranges.push_back(kSmall); |
| 241 custom_ranges.push_back(kMid); | 256 custom_ranges.push_back(kMid); |
| 242 custom_ranges.push_back(0); // Push an explicit zero. | 257 custom_ranges.push_back(0); // Push an explicit zero. |
| 243 custom_ranges.push_back(kBig); | 258 custom_ranges.push_back(kBig); |
| 244 | 259 |
| 245 scoped_refptr<Histogram> unsorted_histogram = CustomHistogram::FactoryGet( | 260 Histogram* unsorted_histogram(CustomHistogram::FactoryGet( |
| 246 "TestCustomUnsortedDupedHistogram", custom_ranges, Histogram::kNoFlags); | 261 "TestCustomUnsortedDupedHistogram", custom_ranges, Histogram::kNoFlags)); |
| 247 EXPECT_EQ(0, unsorted_histogram->ranges(0)); | 262 EXPECT_EQ(0, unsorted_histogram->ranges(0)); |
| 248 EXPECT_EQ(kSmall, unsorted_histogram->ranges(1)); | 263 EXPECT_EQ(kSmall, unsorted_histogram->ranges(1)); |
| 249 EXPECT_EQ(kMid, unsorted_histogram->ranges(2)); | 264 EXPECT_EQ(kMid, unsorted_histogram->ranges(2)); |
| 250 EXPECT_EQ(kBig, unsorted_histogram->ranges(3)); | 265 EXPECT_EQ(kBig, unsorted_histogram->ranges(3)); |
| 251 } | 266 } |
| 252 | 267 |
| 253 | 268 |
| 254 // Make sure histogram handles out-of-bounds data gracefully. | 269 // Make sure histogram handles out-of-bounds data gracefully. |
| 255 TEST(HistogramTest, BoundsTest) { | 270 TEST(HistogramTest, BoundsTest) { |
| 256 const size_t kBucketCount = 50; | 271 const size_t kBucketCount = 50; |
| 257 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( | 272 Histogram* histogram(Histogram::FactoryGet( |
| 258 "Bounded", 10, 100, kBucketCount, Histogram::kNoFlags); | 273 "Bounded", 10, 100, kBucketCount, Histogram::kNoFlags)); |
| 259 | 274 |
| 260 // Put two samples "out of bounds" above and below. | 275 // Put two samples "out of bounds" above and below. |
| 261 histogram->Add(5); | 276 histogram->Add(5); |
| 262 histogram->Add(-50); | 277 histogram->Add(-50); |
| 263 | 278 |
| 264 histogram->Add(100); | 279 histogram->Add(100); |
| 265 histogram->Add(10000); | 280 histogram->Add(10000); |
| 266 | 281 |
| 267 // Verify they landed in the underflow, and overflow buckets. | 282 // Verify they landed in the underflow, and overflow buckets. |
| 268 Histogram::SampleSet sample; | 283 Histogram::SampleSet sample; |
| 269 histogram->SnapshotSample(&sample); | 284 histogram->SnapshotSample(&sample); |
| 270 EXPECT_EQ(2, sample.counts(0)); | 285 EXPECT_EQ(2, sample.counts(0)); |
| 271 EXPECT_EQ(0, sample.counts(1)); | 286 EXPECT_EQ(0, sample.counts(1)); |
| 272 size_t array_size = histogram->bucket_count(); | 287 size_t array_size = histogram->bucket_count(); |
| 273 EXPECT_EQ(kBucketCount, array_size); | 288 EXPECT_EQ(kBucketCount, array_size); |
| 274 EXPECT_EQ(0, sample.counts(array_size - 2)); | 289 EXPECT_EQ(0, sample.counts(array_size - 2)); |
| 275 EXPECT_EQ(2, sample.counts(array_size - 1)); | 290 EXPECT_EQ(2, sample.counts(array_size - 1)); |
| 276 } | 291 } |
| 277 | 292 |
| 278 // Check to be sure samples land as expected is "correct" buckets. | 293 // Check to be sure samples land as expected is "correct" buckets. |
| 279 TEST(HistogramTest, BucketPlacementTest) { | 294 TEST(HistogramTest, BucketPlacementTest) { |
| 280 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( | 295 Histogram* histogram(Histogram::FactoryGet( |
| 281 "Histogram", 1, 64, 8, Histogram::kNoFlags); // As per header file. | 296 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. |
| 282 | 297 |
| 283 // Check that we got a nice exponential since there was enough rooom. | 298 // Check that we got a nice exponential since there was enough rooom. |
| 284 EXPECT_EQ(0, histogram->ranges(0)); | 299 EXPECT_EQ(0, histogram->ranges(0)); |
| 285 int power_of_2 = 1; | 300 int power_of_2 = 1; |
| 286 for (int i = 1; i < 8; i++) { | 301 for (int i = 1; i < 8; i++) { |
| 287 EXPECT_EQ(power_of_2, histogram->ranges(i)); | 302 EXPECT_EQ(power_of_2, histogram->ranges(i)); |
| 288 power_of_2 *= 2; | 303 power_of_2 *= 2; |
| 289 } | 304 } |
| 290 EXPECT_EQ(INT_MAX, histogram->ranges(8)); | 305 EXPECT_EQ(INT_MAX, histogram->ranges(8)); |
| 291 | 306 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 307 EXPECT_EQ(i + 1, sample.counts(i)); | 322 EXPECT_EQ(i + 1, sample.counts(i)); |
| 308 } | 323 } |
| 309 | 324 |
| 310 } // namespace | 325 } // namespace |
| 311 | 326 |
| 312 //------------------------------------------------------------------------------ | 327 //------------------------------------------------------------------------------ |
| 313 // We can't be an an anonymous namespace while being friends, so we pop back | 328 // We can't be an an anonymous namespace while being friends, so we pop back |
| 314 // out to the base namespace here. We need to be friends to corrupt the | 329 // out to the base namespace here. We need to be friends to corrupt the |
| 315 // internals of the histogram and/or sampleset. | 330 // internals of the histogram and/or sampleset. |
| 316 TEST(HistogramTest, CorruptSampleCounts) { | 331 TEST(HistogramTest, CorruptSampleCounts) { |
| 317 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( | 332 Histogram* histogram(Histogram::FactoryGet( |
| 318 "Histogram", 1, 64, 8, Histogram::kNoFlags); // As per header file. | 333 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. |
| 319 | 334 |
| 320 EXPECT_EQ(0, histogram->sample_.redundant_count()); | 335 EXPECT_EQ(0, histogram->sample_.redundant_count()); |
| 321 histogram->Add(20); // Add some samples. | 336 histogram->Add(20); // Add some samples. |
| 322 histogram->Add(40); | 337 histogram->Add(40); |
| 323 EXPECT_EQ(2, histogram->sample_.redundant_count()); | 338 EXPECT_EQ(2, histogram->sample_.redundant_count()); |
| 324 | 339 |
| 325 Histogram::SampleSet snapshot; | 340 Histogram::SampleSet snapshot; |
| 326 histogram->SnapshotSample(&snapshot); | 341 histogram->SnapshotSample(&snapshot); |
| 327 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); | 342 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); |
| 328 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. | 343 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. |
| 329 EXPECT_EQ(2, snapshot.redundant_count()); | 344 EXPECT_EQ(2, snapshot.redundant_count()); |
| 330 | 345 |
| 331 snapshot.counts_[3] += 100; // Sample count won't match redundant count. | 346 snapshot.counts_[3] += 100; // Sample count won't match redundant count. |
| 332 EXPECT_EQ(Histogram::COUNT_LOW_ERROR, histogram->FindCorruption(snapshot)); | 347 EXPECT_EQ(Histogram::COUNT_LOW_ERROR, histogram->FindCorruption(snapshot)); |
| 333 snapshot.counts_[2] -= 200; | 348 snapshot.counts_[2] -= 200; |
| 334 EXPECT_EQ(Histogram::COUNT_HIGH_ERROR, histogram->FindCorruption(snapshot)); | 349 EXPECT_EQ(Histogram::COUNT_HIGH_ERROR, histogram->FindCorruption(snapshot)); |
| 335 | 350 |
| 336 // But we can't spot a corruption if it is compensated for. | 351 // But we can't spot a corruption if it is compensated for. |
| 337 snapshot.counts_[1] += 100; | 352 snapshot.counts_[1] += 100; |
| 338 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); | 353 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); |
| 339 } | 354 } |
| 340 | 355 |
| 341 TEST(HistogramTest, CorruptBucketBounds) { | 356 TEST(HistogramTest, CorruptBucketBounds) { |
| 342 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( | 357 Histogram* histogram(Histogram::FactoryGet( |
| 343 "Histogram", 1, 64, 8, Histogram::kNoFlags); // As per header file. | 358 "Histogram", 1, 64, 8, Histogram::kNoFlags)); // As per header file. |
| 344 | 359 |
| 345 Histogram::SampleSet snapshot; | 360 Histogram::SampleSet snapshot; |
| 346 histogram->SnapshotSample(&snapshot); | 361 histogram->SnapshotSample(&snapshot); |
| 347 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); | 362 EXPECT_EQ(Histogram::NO_INCONSISTENCIES, 0); |
| 348 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. | 363 EXPECT_EQ(0, histogram->FindCorruption(snapshot)); // No default corruption. |
| 349 | 364 |
| 350 std::swap(histogram->ranges_[1], histogram->ranges_[2]); | 365 std::swap(histogram->ranges_[1], histogram->ranges_[2]); |
| 351 EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR, | 366 EXPECT_EQ(Histogram::BUCKET_ORDER_ERROR | Histogram::RANGE_CHECKSUM_ERROR, |
| 352 histogram->FindCorruption(snapshot)); | 367 histogram->FindCorruption(snapshot)); |
| 353 | 368 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 378 if (checksum & 1) | 393 if (checksum & 1) |
| 379 checksum = kReversedPolynomial ^ (checksum >> 1); | 394 checksum = kReversedPolynomial ^ (checksum >> 1); |
| 380 else | 395 else |
| 381 checksum >>= 1; | 396 checksum >>= 1; |
| 382 } | 397 } |
| 383 EXPECT_EQ(Histogram::kCrcTable[i], checksum); | 398 EXPECT_EQ(Histogram::kCrcTable[i], checksum); |
| 384 } | 399 } |
| 385 } | 400 } |
| 386 | 401 |
| 387 } // namespace base | 402 } // namespace base |
| OLD | NEW |