| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/histogram.h" | 7 #include "base/histogram.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 using base::TimeDelta; | 12 using base::TimeDelta; |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 class HistogramTest : public testing::Test { | 16 class HistogramTest : public testing::Test { |
| 17 }; | 17 }; |
| 18 | 18 |
| 19 // Check for basic syntax and use. | 19 // Check for basic syntax and use. |
| 20 TEST(HistogramTest, StartupShutdownTest) { | 20 TEST(HistogramTest, StartupShutdownTest) { |
| 21 // Try basic construction | 21 // Try basic construction |
| 22 scoped_refptr<Histogram> histogram = | 22 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( |
| 23 Histogram::HistogramFactoryGet("TestHistogram", 1, 1000, 10); | 23 "TestHistogram", 1, 1000, 10, Histogram::kNoFlags); |
| 24 scoped_refptr<Histogram> histogram1 = | 24 scoped_refptr<Histogram> histogram1 = Histogram::FactoryGet( |
| 25 Histogram::HistogramFactoryGet("Test1Histogram", 1, 1000, 10); | 25 "Test1Histogram", 1, 1000, 10, Histogram::kNoFlags); |
| 26 | 26 |
| 27 scoped_refptr<Histogram> linear_histogram = | 27 scoped_refptr<Histogram> linear_histogram = LinearHistogram::FactoryGet( |
| 28 LinearHistogram::LinearHistogramFactoryGet("TestLinearHistogram", 1, 1000, | 28 "TestLinearHistogram", 1, 1000, 10, Histogram::kNoFlags); |
| 29 10); | 29 scoped_refptr<Histogram> linear_histogram1 = LinearHistogram::FactoryGet( |
| 30 scoped_refptr<Histogram> linear_histogram1 = | 30 "Test1LinearHistogram", 1, 1000, 10, Histogram::kNoFlags); |
| 31 LinearHistogram::LinearHistogramFactoryGet("Test1LinearHistogram", 1, | |
| 32 1000, 10); | |
| 33 | 31 |
| 34 // Use standard macros (but with fixed samples) | 32 // Use standard macros (but with fixed samples) |
| 35 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); | 33 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); |
| 36 HISTOGRAM_COUNTS("Test3Histogram", 30); | 34 HISTOGRAM_COUNTS("Test3Histogram", 30); |
| 37 | 35 |
| 38 DHISTOGRAM_TIMES("Test4Histogram", TimeDelta::FromDays(1)); | 36 DHISTOGRAM_TIMES("Test4Histogram", TimeDelta::FromDays(1)); |
| 39 DHISTOGRAM_COUNTS("Test5Histogram", 30); | 37 DHISTOGRAM_COUNTS("Test5Histogram", 30); |
| 40 | 38 |
| 41 ASSET_HISTOGRAM_COUNTS("Test6Histogram", 129); | 39 HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130); |
| 42 | 40 |
| 43 // Try to construct samples. | 41 // Try to construct samples. |
| 44 Histogram::SampleSet sample1; | 42 Histogram::SampleSet sample1; |
| 45 Histogram::SampleSet sample2; | 43 Histogram::SampleSet sample2; |
| 46 | 44 |
| 47 // Use copy constructor of SampleSet | 45 // Use copy constructor of SampleSet |
| 48 sample1 = sample2; | 46 sample1 = sample2; |
| 49 Histogram::SampleSet sample3(sample1); | 47 Histogram::SampleSet sample3(sample1); |
| 50 | 48 |
| 51 // Finally test a statistics recorder, without really using it. | 49 // Finally test a statistics recorder, without really using it. |
| 52 StatisticsRecorder recorder; | 50 StatisticsRecorder recorder; |
| 53 } | 51 } |
| 54 | 52 |
| 55 // Repeat with a recorder present to register with. | 53 // Repeat with a recorder present to register with. |
| 56 TEST(HistogramTest, RecordedStartupTest) { | 54 TEST(HistogramTest, RecordedStartupTest) { |
| 57 // Test a statistics recorder, by letting histograms register. | 55 // Test a statistics recorder, by letting histograms register. |
| 58 StatisticsRecorder recorder; // This initializes the global state. | 56 StatisticsRecorder recorder; // This initializes the global state. |
| 59 | 57 |
| 60 StatisticsRecorder::Histograms histograms; | 58 StatisticsRecorder::Histograms histograms; |
| 61 EXPECT_EQ(0U, histograms.size()); | 59 EXPECT_EQ(0U, histograms.size()); |
| 62 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 60 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 63 EXPECT_EQ(0U, histograms.size()); | 61 EXPECT_EQ(0U, histograms.size()); |
| 64 | 62 |
| 65 // Try basic construction | 63 // Try basic construction |
| 66 scoped_refptr<Histogram> histogram = | 64 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( |
| 67 Histogram::HistogramFactoryGet("TestHistogram", 1, 1000, 10); | 65 "TestHistogram", 1, 1000, 10, Histogram::kNoFlags); |
| 68 histograms.clear(); | 66 histograms.clear(); |
| 69 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 67 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 70 EXPECT_EQ(1U, histograms.size()); | 68 EXPECT_EQ(1U, histograms.size()); |
| 71 scoped_refptr<Histogram> histogram1 = | 69 scoped_refptr<Histogram> histogram1 = Histogram::FactoryGet( |
| 72 Histogram::HistogramFactoryGet("Test1Histogram", 1, 1000, 10); | 70 "Test1Histogram", 1, 1000, 10, Histogram::kNoFlags); |
| 73 histograms.clear(); | 71 histograms.clear(); |
| 74 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 72 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 75 EXPECT_EQ(2U, histograms.size()); | 73 EXPECT_EQ(2U, histograms.size()); |
| 76 | 74 |
| 77 scoped_refptr<Histogram> linear_histogram = | 75 scoped_refptr<Histogram> linear_histogram = LinearHistogram::FactoryGet( |
| 78 LinearHistogram::LinearHistogramFactoryGet( | 76 "TestLinearHistogram", 1, 1000, 10, Histogram::kNoFlags); |
| 79 "TestLinearHistogram", 1, 1000, 10); | 77 scoped_refptr<Histogram> linear_histogram1 = LinearHistogram::FactoryGet( |
| 80 scoped_refptr<Histogram> linear_histogram1 = | 78 "Test1LinearHistogram", 1, 1000, 10, Histogram::kNoFlags); |
| 81 LinearHistogram::LinearHistogramFactoryGet( | |
| 82 "Test1LinearHistogram", 1, 1000, 10); | |
| 83 histograms.clear(); | 79 histograms.clear(); |
| 84 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 80 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 85 EXPECT_EQ(4U, histograms.size()); | 81 EXPECT_EQ(4U, histograms.size()); |
| 86 | 82 |
| 87 // Use standard macros (but with fixed samples) | 83 // Use standard macros (but with fixed samples) |
| 88 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); | 84 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); |
| 89 HISTOGRAM_COUNTS("Test3Histogram", 30); | 85 HISTOGRAM_COUNTS("Test3Histogram", 30); |
| 90 histograms.clear(); | 86 histograms.clear(); |
| 91 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 87 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 92 EXPECT_EQ(6U, histograms.size()); | 88 EXPECT_EQ(6U, histograms.size()); |
| 93 | 89 |
| 94 ASSET_HISTOGRAM_COUNTS("TestAssetHistogram", 1000); | 90 HISTOGRAM_ENUMERATION("TestEnumerationHistogram", 20, 200); |
| 95 histograms.clear(); | 91 histograms.clear(); |
| 96 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 92 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 97 EXPECT_EQ(7U, histograms.size()); | 93 EXPECT_EQ(7U, histograms.size()); |
| 98 | 94 |
| 99 DHISTOGRAM_TIMES("Test4Histogram", TimeDelta::FromDays(1)); | 95 DHISTOGRAM_TIMES("Test4Histogram", TimeDelta::FromDays(1)); |
| 100 DHISTOGRAM_COUNTS("Test5Histogram", 30); | 96 DHISTOGRAM_COUNTS("Test5Histogram", 30); |
| 101 histograms.clear(); | 97 histograms.clear(); |
| 102 StatisticsRecorder::GetHistograms(&histograms); // Load up lists | 98 StatisticsRecorder::GetHistograms(&histograms); // Load up lists |
| 103 #ifndef NDEBUG | 99 #ifndef NDEBUG |
| 104 EXPECT_EQ(9U, histograms.size()); | 100 EXPECT_EQ(9U, histograms.size()); |
| 105 #else | 101 #else |
| 106 EXPECT_EQ(7U, histograms.size()); | 102 EXPECT_EQ(7U, histograms.size()); |
| 107 #endif | 103 #endif |
| 108 } | 104 } |
| 109 | 105 |
| 110 TEST(HistogramTest, RangeTest) { | 106 TEST(HistogramTest, RangeTest) { |
| 111 StatisticsRecorder recorder; | 107 StatisticsRecorder recorder; |
| 112 StatisticsRecorder::Histograms histograms; | 108 StatisticsRecorder::Histograms histograms; |
| 113 | 109 |
| 114 recorder.GetHistograms(&histograms); | 110 recorder.GetHistograms(&histograms); |
| 115 EXPECT_EQ(0U, histograms.size()); | 111 EXPECT_EQ(0U, histograms.size()); |
| 116 | 112 |
| 117 scoped_refptr<Histogram> histogram = Histogram::HistogramFactoryGet( | 113 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( |
| 118 "Histogram", 1, 64, 8); // As mentioned in header file. | 114 "Histogram", 1, 64, 8, Histogram::kNoFlags); // As per header file. |
| 119 // Check that we got a nice exponential when there was enough rooom. | 115 // Check that we got a nice exponential when there was enough rooom. |
| 120 EXPECT_EQ(0, histogram->ranges(0)); | 116 EXPECT_EQ(0, histogram->ranges(0)); |
| 121 int power_of_2 = 1; | 117 int power_of_2 = 1; |
| 122 for (int i = 1; i < 8; i++) { | 118 for (int i = 1; i < 8; i++) { |
| 123 EXPECT_EQ(power_of_2, histogram->ranges(i)); | 119 EXPECT_EQ(power_of_2, histogram->ranges(i)); |
| 124 power_of_2 *= 2; | 120 power_of_2 *= 2; |
| 125 } | 121 } |
| 126 EXPECT_EQ(INT_MAX, histogram->ranges(8)); | 122 EXPECT_EQ(INT_MAX, histogram->ranges(8)); |
| 127 | 123 |
| 128 scoped_refptr<Histogram> short_histogram = | 124 scoped_refptr<Histogram> short_histogram = Histogram::FactoryGet( |
| 129 Histogram::HistogramFactoryGet("Histogram Shortened", 1, 7, 8); | 125 "Histogram Shortened", 1, 7, 8, Histogram::kNoFlags); |
| 130 // Check that when the number of buckets is short, we get a linear histogram | 126 // Check that when the number of buckets is short, we get a linear histogram |
| 131 // for lack of space to do otherwise. | 127 // for lack of space to do otherwise. |
| 132 for (int i = 0; i < 8; i++) | 128 for (int i = 0; i < 8; i++) |
| 133 EXPECT_EQ(i, short_histogram->ranges(i)); | 129 EXPECT_EQ(i, short_histogram->ranges(i)); |
| 134 EXPECT_EQ(INT_MAX, short_histogram->ranges(8)); | 130 EXPECT_EQ(INT_MAX, short_histogram->ranges(8)); |
| 135 | 131 |
| 136 scoped_refptr<Histogram> linear_histogram = | 132 scoped_refptr<Histogram> linear_histogram = LinearHistogram::FactoryGet( |
| 137 LinearHistogram::LinearHistogramFactoryGet("Linear", 1, 7, 8); | 133 "Linear", 1, 7, 8, Histogram::kNoFlags); |
| 138 // We also get a nice linear set of bucket ranges when we ask for it | 134 // We also get a nice linear set of bucket ranges when we ask for it |
| 139 for (int i = 0; i < 8; i++) | 135 for (int i = 0; i < 8; i++) |
| 140 EXPECT_EQ(i, linear_histogram->ranges(i)); | 136 EXPECT_EQ(i, linear_histogram->ranges(i)); |
| 141 EXPECT_EQ(INT_MAX, linear_histogram->ranges(8)); | 137 EXPECT_EQ(INT_MAX, linear_histogram->ranges(8)); |
| 142 | 138 |
| 143 scoped_refptr<Histogram> linear_broad_histogram = | 139 scoped_refptr<Histogram> linear_broad_histogram = LinearHistogram::FactoryGet( |
| 144 LinearHistogram::LinearHistogramFactoryGet( | 140 "Linear widened", 2, 14, 8, Histogram::kNoFlags); |
| 145 "Linear widened", 2, 14, 8); | |
| 146 // ...but when the list has more space, then the ranges naturally spread out. | 141 // ...but when the list has more space, then the ranges naturally spread out. |
| 147 for (int i = 0; i < 8; i++) | 142 for (int i = 0; i < 8; i++) |
| 148 EXPECT_EQ(2 * i, linear_broad_histogram->ranges(i)); | 143 EXPECT_EQ(2 * i, linear_broad_histogram->ranges(i)); |
| 149 EXPECT_EQ(INT_MAX, linear_broad_histogram->ranges(8)); | 144 EXPECT_EQ(INT_MAX, linear_broad_histogram->ranges(8)); |
| 150 | 145 |
| 151 scoped_refptr<Histogram> threadsafe_histogram = | 146 scoped_refptr<Histogram> transitioning_histogram = |
| 152 ThreadSafeHistogram::ThreadSafeHistogramFactoryGet("ThreadSafe", 1, 32, | 147 Histogram::FactoryGet("LinearAndExponential", 1, 32, 15, |
| 153 15); | 148 Histogram::kNoFlags); |
| 154 // When space is a little tight, we transition from linear to exponential. | 149 // When space is a little tight, we transition from linear to exponential. |
| 155 // This is what happens in both the basic histogram, and the threadsafe | 150 EXPECT_EQ(0, transitioning_histogram->ranges(0)); |
| 156 // variant (which is derived). | 151 EXPECT_EQ(1, transitioning_histogram->ranges(1)); |
| 157 EXPECT_EQ(0, threadsafe_histogram->ranges(0)); | 152 EXPECT_EQ(2, transitioning_histogram->ranges(2)); |
| 158 EXPECT_EQ(1, threadsafe_histogram->ranges(1)); | 153 EXPECT_EQ(3, transitioning_histogram->ranges(3)); |
| 159 EXPECT_EQ(2, threadsafe_histogram->ranges(2)); | 154 EXPECT_EQ(4, transitioning_histogram->ranges(4)); |
| 160 EXPECT_EQ(3, threadsafe_histogram->ranges(3)); | 155 EXPECT_EQ(5, transitioning_histogram->ranges(5)); |
| 161 EXPECT_EQ(4, threadsafe_histogram->ranges(4)); | 156 EXPECT_EQ(6, transitioning_histogram->ranges(6)); |
| 162 EXPECT_EQ(5, threadsafe_histogram->ranges(5)); | 157 EXPECT_EQ(7, transitioning_histogram->ranges(7)); |
| 163 EXPECT_EQ(6, threadsafe_histogram->ranges(6)); | 158 EXPECT_EQ(9, transitioning_histogram->ranges(8)); |
| 164 EXPECT_EQ(7, threadsafe_histogram->ranges(7)); | 159 EXPECT_EQ(11, transitioning_histogram->ranges(9)); |
| 165 EXPECT_EQ(9, threadsafe_histogram->ranges(8)); | 160 EXPECT_EQ(14, transitioning_histogram->ranges(10)); |
| 166 EXPECT_EQ(11, threadsafe_histogram->ranges(9)); | 161 EXPECT_EQ(17, transitioning_histogram->ranges(11)); |
| 167 EXPECT_EQ(14, threadsafe_histogram->ranges(10)); | 162 EXPECT_EQ(21, transitioning_histogram->ranges(12)); |
| 168 EXPECT_EQ(17, threadsafe_histogram->ranges(11)); | 163 EXPECT_EQ(26, transitioning_histogram->ranges(13)); |
| 169 EXPECT_EQ(21, threadsafe_histogram->ranges(12)); | 164 EXPECT_EQ(32, transitioning_histogram->ranges(14)); |
| 170 EXPECT_EQ(26, threadsafe_histogram->ranges(13)); | 165 EXPECT_EQ(INT_MAX, transitioning_histogram->ranges(15)); |
| 171 EXPECT_EQ(32, threadsafe_histogram->ranges(14)); | |
| 172 EXPECT_EQ(INT_MAX, threadsafe_histogram->ranges(15)); | |
| 173 | 166 |
| 174 recorder.GetHistograms(&histograms); | 167 recorder.GetHistograms(&histograms); |
| 175 EXPECT_EQ(5U, histograms.size()); | 168 EXPECT_EQ(5U, histograms.size()); |
| 176 } | 169 } |
| 177 | 170 |
| 178 // Make sure histogram handles out-of-bounds data gracefully. | 171 // Make sure histogram handles out-of-bounds data gracefully. |
| 179 TEST(HistogramTest, BoundsTest) { | 172 TEST(HistogramTest, BoundsTest) { |
| 180 const size_t kBucketCount = 50; | 173 const size_t kBucketCount = 50; |
| 181 scoped_refptr<Histogram> histogram = Histogram::HistogramFactoryGet("Bounded", | 174 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( |
| 182 10, 100, kBucketCount); | 175 "Bounded", 10, 100, kBucketCount, Histogram::kNoFlags); |
| 183 | 176 |
| 184 // Put two samples "out of bounds" above and below. | 177 // Put two samples "out of bounds" above and below. |
| 185 histogram->Add(5); | 178 histogram->Add(5); |
| 186 histogram->Add(-50); | 179 histogram->Add(-50); |
| 187 | 180 |
| 188 histogram->Add(100); | 181 histogram->Add(100); |
| 189 histogram->Add(10000); | 182 histogram->Add(10000); |
| 190 | 183 |
| 191 // Verify they landed in the underflow, and overflow buckets. | 184 // Verify they landed in the underflow, and overflow buckets. |
| 192 Histogram::SampleSet sample; | 185 Histogram::SampleSet sample; |
| 193 histogram->SnapshotSample(&sample); | 186 histogram->SnapshotSample(&sample); |
| 194 EXPECT_EQ(2, sample.counts(0)); | 187 EXPECT_EQ(2, sample.counts(0)); |
| 195 EXPECT_EQ(0, sample.counts(1)); | 188 EXPECT_EQ(0, sample.counts(1)); |
| 196 size_t array_size = histogram->bucket_count(); | 189 size_t array_size = histogram->bucket_count(); |
| 197 EXPECT_EQ(kBucketCount, array_size); | 190 EXPECT_EQ(kBucketCount, array_size); |
| 198 EXPECT_EQ(0, sample.counts(array_size - 2)); | 191 EXPECT_EQ(0, sample.counts(array_size - 2)); |
| 199 EXPECT_EQ(2, sample.counts(array_size - 1)); | 192 EXPECT_EQ(2, sample.counts(array_size - 1)); |
| 200 } | 193 } |
| 201 | 194 |
| 202 // Check to be sure samples land as expected is "correct" buckets. | 195 // Check to be sure samples land as expected is "correct" buckets. |
| 203 TEST(HistogramTest, BucketPlacementTest) { | 196 TEST(HistogramTest, BucketPlacementTest) { |
| 204 scoped_refptr<Histogram> histogram = Histogram::HistogramFactoryGet( | 197 scoped_refptr<Histogram> histogram = Histogram::FactoryGet( |
| 205 "Histogram", 1, 64, 8); // As mentioned in header file. | 198 "Histogram", 1, 64, 8, Histogram::kNoFlags); // As per header file. |
| 206 | 199 |
| 207 // Check that we got a nice exponential since there was enough rooom. | 200 // Check that we got a nice exponential since there was enough rooom. |
| 208 EXPECT_EQ(0, histogram->ranges(0)); | 201 EXPECT_EQ(0, histogram->ranges(0)); |
| 209 int power_of_2 = 1; | 202 int power_of_2 = 1; |
| 210 for (int i = 1; i < 8; i++) { | 203 for (int i = 1; i < 8; i++) { |
| 211 EXPECT_EQ(power_of_2, histogram->ranges(i)); | 204 EXPECT_EQ(power_of_2, histogram->ranges(i)); |
| 212 power_of_2 *= 2; | 205 power_of_2 *= 2; |
| 213 } | 206 } |
| 214 EXPECT_EQ(INT_MAX, histogram->ranges(8)); | 207 EXPECT_EQ(INT_MAX, histogram->ranges(8)); |
| 215 | 208 |
| 216 // Add i+1 samples to the i'th bucket. | 209 // Add i+1 samples to the i'th bucket. |
| 217 histogram->Add(0); | 210 histogram->Add(0); |
| 218 power_of_2 = 1; | 211 power_of_2 = 1; |
| 219 for (int i = 1; i < 8; i++) { | 212 for (int i = 1; i < 8; i++) { |
| 220 for (int j = 0; j <= i; j++) | 213 for (int j = 0; j <= i; j++) |
| 221 histogram->Add(power_of_2); | 214 histogram->Add(power_of_2); |
| 222 power_of_2 *= 2; | 215 power_of_2 *= 2; |
| 223 } | 216 } |
| 224 // Leave overflow bucket empty. | 217 // Leave overflow bucket empty. |
| 225 | 218 |
| 226 // Check to see that the bucket counts reflect our additions. | 219 // Check to see that the bucket counts reflect our additions. |
| 227 Histogram::SampleSet sample; | 220 Histogram::SampleSet sample; |
| 228 histogram->SnapshotSample(&sample); | 221 histogram->SnapshotSample(&sample); |
| 229 EXPECT_EQ(INT_MAX, histogram->ranges(8)); | 222 EXPECT_EQ(INT_MAX, histogram->ranges(8)); |
| 230 for (int i = 0; i < 8; i++) | 223 for (int i = 0; i < 8; i++) |
| 231 EXPECT_EQ(i + 1, sample.counts(i)); | 224 EXPECT_EQ(i + 1, sample.counts(i)); |
| 232 } | 225 } |
| 233 | 226 |
| 234 static const char kAssetTestHistogramName[] = "AssetCountTest"; | |
| 235 static const char kAssetTestDebugHistogramName[] = "DAssetCountTest"; | |
| 236 void AssetCountFunction(int sample) { | |
| 237 ASSET_HISTOGRAM_COUNTS(kAssetTestHistogramName, sample); | |
| 238 DASSET_HISTOGRAM_COUNTS(kAssetTestDebugHistogramName, sample); | |
| 239 } | |
| 240 // Check that asset can be added and removed from buckets. | |
| 241 TEST(HistogramTest, AssetCountTest) { | |
| 242 // Start up a recorder system to identify all histograms. | |
| 243 StatisticsRecorder recorder; | |
| 244 | |
| 245 // Call through the macro to instantiate the static variables. | |
| 246 AssetCountFunction(100); // Put a sample in the bucket for 100. | |
| 247 | |
| 248 // Find the histogram. | |
| 249 StatisticsRecorder::Histograms histogram_list; | |
| 250 StatisticsRecorder::GetHistograms(&histogram_list); | |
| 251 ASSERT_NE(0U, histogram_list.size()); | |
| 252 const Histogram* our_histogram = NULL; | |
| 253 const Histogram* our_debug_histogram = NULL; | |
| 254 for (StatisticsRecorder::Histograms::iterator it = histogram_list.begin(); | |
| 255 it != histogram_list.end(); | |
| 256 ++it) { | |
| 257 if (!(*it)->histogram_name().compare(kAssetTestHistogramName)) | |
| 258 our_histogram = *it; | |
| 259 else if (!(*it)->histogram_name().compare(kAssetTestDebugHistogramName)) { | |
| 260 our_debug_histogram = *it; | |
| 261 } | |
| 262 } | |
| 263 ASSERT_TRUE(our_histogram); | |
| 264 #ifndef NDEBUG | |
| 265 EXPECT_TRUE(our_debug_histogram); | |
| 266 #else | |
| 267 EXPECT_FALSE(our_debug_histogram); | |
| 268 #endif | |
| 269 // Verify it has a 1 in exactly one bucket (where we put the sample). | |
| 270 Histogram::SampleSet sample; | |
| 271 our_histogram->SnapshotSample(&sample); | |
| 272 int match_count = 0; | |
| 273 for (size_t i = 0; i < our_histogram->bucket_count(); ++i) { | |
| 274 if (sample.counts(i) > 0) { | |
| 275 EXPECT_LT(++match_count, 2) << "extra count in bucket " << i; | |
| 276 } | |
| 277 } | |
| 278 EXPECT_EQ(1, match_count); | |
| 279 | |
| 280 // Remove our sample. | |
| 281 AssetCountFunction(-100); // Remove a sample from the bucket for 100. | |
| 282 our_histogram->SnapshotSample(&sample); // Extract data set. | |
| 283 | |
| 284 // Verify that the bucket is now empty, as are all the other buckets. | |
| 285 for (size_t i = 0; i < our_histogram->bucket_count(); ++i) { | |
| 286 EXPECT_EQ(0, sample.counts(i)) << "extra count in bucket " << i; | |
| 287 } | |
| 288 | |
| 289 if (!our_debug_histogram) | |
| 290 return; // This is a production build. | |
| 291 | |
| 292 // Repeat test with debug histogram. Note that insertion and deletion above | |
| 293 // should have cancelled each other out. | |
| 294 AssetCountFunction(100); // Add a sample into the bucket for 100. | |
| 295 our_debug_histogram->SnapshotSample(&sample); | |
| 296 match_count = 0; | |
| 297 for (size_t i = 0; i < our_debug_histogram->bucket_count(); ++i) { | |
| 298 if (sample.counts(i) > 0) { | |
| 299 EXPECT_LT(++match_count, 2) << "extra count in bucket " << i; | |
| 300 } | |
| 301 } | |
| 302 EXPECT_EQ(1, match_count); | |
| 303 | |
| 304 // Remove our sample. | |
| 305 AssetCountFunction(-100); // Remove a sample from the bucket for 100. | |
| 306 our_debug_histogram->SnapshotSample(&sample); // Extract data set. | |
| 307 | |
| 308 // Verify that the bucket is now empty, as are all the other buckets. | |
| 309 for (size_t i = 0; i < our_debug_histogram->bucket_count(); ++i) { | |
| 310 EXPECT_EQ(0, sample.counts(i)) << "extra count in bucket " << i; | |
| 311 } | |
| 312 } | |
| 313 | 227 |
| 314 } // namespace | 228 } // namespace |
| OLD | NEW |