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

Side by Side Diff: base/histogram_unittest.cc

Issue 462027: Use factory to create histograms, and refcounts to track lifetimes... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years 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 | Annotate | Revision Log
« no previous file with comments | « base/histogram.cc ('k') | base/message_loop.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 Histogram histogram("TestHistogram", 1, 1000, 10); 22 scoped_refptr<Histogram> histogram =
23 Histogram histogram1("Test1Histogram", 1, 1000, 10); 23 Histogram::HistogramFactoryGet("TestHistogram", 1, 1000, 10);
24 scoped_refptr<Histogram> histogram1 =
25 Histogram::HistogramFactoryGet("Test1Histogram", 1, 1000, 10);
24 26
25 LinearHistogram linear_histogram("TestLinearHistogram", 1, 1000, 10); 27 scoped_refptr<Histogram> linear_histogram =
26 LinearHistogram linear_histogram1("Test1LinearHistogram", 1, 1000, 10); 28 LinearHistogram::LinearHistogramFactoryGet("TestLinearHistogram", 1, 1000,
29 10);
30 scoped_refptr<Histogram> linear_histogram1 =
31 LinearHistogram::LinearHistogramFactoryGet("Test1LinearHistogram", 1,
32 1000, 10);
27 33
28 // Use standard macros (but with fixed samples) 34 // Use standard macros (but with fixed samples)
29 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); 35 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1));
30 HISTOGRAM_COUNTS("Test3Histogram", 30); 36 HISTOGRAM_COUNTS("Test3Histogram", 30);
31 37
32 DHISTOGRAM_TIMES("Test4Histogram", TimeDelta::FromDays(1)); 38 DHISTOGRAM_TIMES("Test4Histogram", TimeDelta::FromDays(1));
33 DHISTOGRAM_COUNTS("Test5Histogram", 30); 39 DHISTOGRAM_COUNTS("Test5Histogram", 30);
34 40
35 ASSET_HISTOGRAM_COUNTS("Test6Histogram", 129); 41 ASSET_HISTOGRAM_COUNTS("Test6Histogram", 129);
36 42
(...skipping 13 matching lines...) Expand all
50 TEST(HistogramTest, RecordedStartupTest) { 56 TEST(HistogramTest, RecordedStartupTest) {
51 // Test a statistics recorder, by letting histograms register. 57 // Test a statistics recorder, by letting histograms register.
52 StatisticsRecorder recorder; // This initializes the global state. 58 StatisticsRecorder recorder; // This initializes the global state.
53 59
54 StatisticsRecorder::Histograms histograms; 60 StatisticsRecorder::Histograms histograms;
55 EXPECT_EQ(0U, histograms.size()); 61 EXPECT_EQ(0U, histograms.size());
56 StatisticsRecorder::GetHistograms(&histograms); // Load up lists 62 StatisticsRecorder::GetHistograms(&histograms); // Load up lists
57 EXPECT_EQ(0U, histograms.size()); 63 EXPECT_EQ(0U, histograms.size());
58 64
59 // Try basic construction 65 // Try basic construction
60 Histogram histogram("TestHistogram", 1, 1000, 10); 66 scoped_refptr<Histogram> histogram =
67 Histogram::HistogramFactoryGet("TestHistogram", 1, 1000, 10);
61 histograms.clear(); 68 histograms.clear();
62 StatisticsRecorder::GetHistograms(&histograms); // Load up lists 69 StatisticsRecorder::GetHistograms(&histograms); // Load up lists
63 EXPECT_EQ(1U, histograms.size()); 70 EXPECT_EQ(1U, histograms.size());
64 Histogram histogram1("Test1Histogram", 1, 1000, 10); 71 scoped_refptr<Histogram> histogram1 =
72 Histogram::HistogramFactoryGet("Test1Histogram", 1, 1000, 10);
65 histograms.clear(); 73 histograms.clear();
66 StatisticsRecorder::GetHistograms(&histograms); // Load up lists 74 StatisticsRecorder::GetHistograms(&histograms); // Load up lists
67 EXPECT_EQ(2U, histograms.size()); 75 EXPECT_EQ(2U, histograms.size());
68 76
69 LinearHistogram linear_histogram("TestLinearHistogram", 1, 1000, 10); 77 scoped_refptr<Histogram> linear_histogram =
70 LinearHistogram linear_histogram1("Test1LinearHistogram", 1, 1000, 10); 78 LinearHistogram::LinearHistogramFactoryGet(
79 "TestLinearHistogram", 1, 1000, 10);
80 scoped_refptr<Histogram> linear_histogram1 =
81 LinearHistogram::LinearHistogramFactoryGet(
82 "Test1LinearHistogram", 1, 1000, 10);
71 histograms.clear(); 83 histograms.clear();
72 StatisticsRecorder::GetHistograms(&histograms); // Load up lists 84 StatisticsRecorder::GetHistograms(&histograms); // Load up lists
73 EXPECT_EQ(4U, histograms.size()); 85 EXPECT_EQ(4U, histograms.size());
74 86
75 // Use standard macros (but with fixed samples) 87 // Use standard macros (but with fixed samples)
76 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); 88 HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1));
77 HISTOGRAM_COUNTS("Test3Histogram", 30); 89 HISTOGRAM_COUNTS("Test3Histogram", 30);
78 histograms.clear(); 90 histograms.clear();
79 StatisticsRecorder::GetHistograms(&histograms); // Load up lists 91 StatisticsRecorder::GetHistograms(&histograms); // Load up lists
80 EXPECT_EQ(6U, histograms.size()); 92 EXPECT_EQ(6U, histograms.size());
(...skipping 14 matching lines...) Expand all
95 #endif 107 #endif
96 } 108 }
97 109
98 TEST(HistogramTest, RangeTest) { 110 TEST(HistogramTest, RangeTest) {
99 StatisticsRecorder recorder; 111 StatisticsRecorder recorder;
100 StatisticsRecorder::Histograms histograms; 112 StatisticsRecorder::Histograms histograms;
101 113
102 recorder.GetHistograms(&histograms); 114 recorder.GetHistograms(&histograms);
103 EXPECT_EQ(0U, histograms.size()); 115 EXPECT_EQ(0U, histograms.size());
104 116
105 Histogram histogram("Histogram", 1, 64, 8); // As mentioned in header file. 117 scoped_refptr<Histogram> histogram = Histogram::HistogramFactoryGet(
118 "Histogram", 1, 64, 8); // As mentioned in header file.
106 // Check that we got a nice exponential when there was enough rooom. 119 // Check that we got a nice exponential when there was enough rooom.
107 EXPECT_EQ(0, histogram.ranges(0)); 120 EXPECT_EQ(0, histogram->ranges(0));
108 int power_of_2 = 1; 121 int power_of_2 = 1;
109 for (int i = 1; i < 8; i++) { 122 for (int i = 1; i < 8; i++) {
110 EXPECT_EQ(power_of_2, histogram.ranges(i)); 123 EXPECT_EQ(power_of_2, histogram->ranges(i));
111 power_of_2 *= 2; 124 power_of_2 *= 2;
112 } 125 }
113 EXPECT_EQ(INT_MAX, histogram.ranges(8)); 126 EXPECT_EQ(INT_MAX, histogram->ranges(8));
114 127
115 Histogram short_histogram("Histogram Shortened", 1, 7, 8); 128 scoped_refptr<Histogram> short_histogram =
129 Histogram::HistogramFactoryGet("Histogram Shortened", 1, 7, 8);
116 // Check that when the number of buckets is short, we get a linear histogram 130 // Check that when the number of buckets is short, we get a linear histogram
117 // for lack of space to do otherwise. 131 // for lack of space to do otherwise.
118 for (int i = 0; i < 8; i++) 132 for (int i = 0; i < 8; i++)
119 EXPECT_EQ(i, short_histogram.ranges(i)); 133 EXPECT_EQ(i, short_histogram->ranges(i));
120 EXPECT_EQ(INT_MAX, short_histogram.ranges(8)); 134 EXPECT_EQ(INT_MAX, short_histogram->ranges(8));
121 135
122 LinearHistogram linear_histogram("Linear", 1, 7, 8); 136 scoped_refptr<Histogram> linear_histogram =
137 LinearHistogram::LinearHistogramFactoryGet("Linear", 1, 7, 8);
123 // We also get a nice linear set of bucket ranges when we ask for it 138 // We also get a nice linear set of bucket ranges when we ask for it
124 for (int i = 0; i < 8; i++) 139 for (int i = 0; i < 8; i++)
125 EXPECT_EQ(i, linear_histogram.ranges(i)); 140 EXPECT_EQ(i, linear_histogram->ranges(i));
126 EXPECT_EQ(INT_MAX, linear_histogram.ranges(8)); 141 EXPECT_EQ(INT_MAX, linear_histogram->ranges(8));
127 142
128 LinearHistogram linear_broad_histogram("Linear widened", 2, 14, 8); 143 scoped_refptr<Histogram> linear_broad_histogram =
144 LinearHistogram::LinearHistogramFactoryGet(
145 "Linear widened", 2, 14, 8);
129 // ...but when the list has more space, then the ranges naturally spread out. 146 // ...but when the list has more space, then the ranges naturally spread out.
130 for (int i = 0; i < 8; i++) 147 for (int i = 0; i < 8; i++)
131 EXPECT_EQ(2 * i, linear_broad_histogram.ranges(i)); 148 EXPECT_EQ(2 * i, linear_broad_histogram->ranges(i));
132 EXPECT_EQ(INT_MAX, linear_broad_histogram.ranges(8)); 149 EXPECT_EQ(INT_MAX, linear_broad_histogram->ranges(8));
133 150
134 ThreadSafeHistogram threadsafe_histogram("ThreadSafe", 1, 32, 15); 151 scoped_refptr<Histogram> threadsafe_histogram =
152 ThreadSafeHistogram::ThreadSafeHistogramFactoryGet("ThreadSafe", 1, 32,
153 15);
135 // When space is a little tight, we transition from linear to exponential. 154 // When space is a little tight, we transition from linear to exponential.
136 // This is what happens in both the basic histogram, and the threadsafe 155 // This is what happens in both the basic histogram, and the threadsafe
137 // variant (which is derived). 156 // variant (which is derived).
138 EXPECT_EQ(0, threadsafe_histogram.ranges(0)); 157 EXPECT_EQ(0, threadsafe_histogram->ranges(0));
139 EXPECT_EQ(1, threadsafe_histogram.ranges(1)); 158 EXPECT_EQ(1, threadsafe_histogram->ranges(1));
140 EXPECT_EQ(2, threadsafe_histogram.ranges(2)); 159 EXPECT_EQ(2, threadsafe_histogram->ranges(2));
141 EXPECT_EQ(3, threadsafe_histogram.ranges(3)); 160 EXPECT_EQ(3, threadsafe_histogram->ranges(3));
142 EXPECT_EQ(4, threadsafe_histogram.ranges(4)); 161 EXPECT_EQ(4, threadsafe_histogram->ranges(4));
143 EXPECT_EQ(5, threadsafe_histogram.ranges(5)); 162 EXPECT_EQ(5, threadsafe_histogram->ranges(5));
144 EXPECT_EQ(6, threadsafe_histogram.ranges(6)); 163 EXPECT_EQ(6, threadsafe_histogram->ranges(6));
145 EXPECT_EQ(7, threadsafe_histogram.ranges(7)); 164 EXPECT_EQ(7, threadsafe_histogram->ranges(7));
146 EXPECT_EQ(9, threadsafe_histogram.ranges(8)); 165 EXPECT_EQ(9, threadsafe_histogram->ranges(8));
147 EXPECT_EQ(11, threadsafe_histogram.ranges(9)); 166 EXPECT_EQ(11, threadsafe_histogram->ranges(9));
148 EXPECT_EQ(14, threadsafe_histogram.ranges(10)); 167 EXPECT_EQ(14, threadsafe_histogram->ranges(10));
149 EXPECT_EQ(17, threadsafe_histogram.ranges(11)); 168 EXPECT_EQ(17, threadsafe_histogram->ranges(11));
150 EXPECT_EQ(21, threadsafe_histogram.ranges(12)); 169 EXPECT_EQ(21, threadsafe_histogram->ranges(12));
151 EXPECT_EQ(26, threadsafe_histogram.ranges(13)); 170 EXPECT_EQ(26, threadsafe_histogram->ranges(13));
152 EXPECT_EQ(32, threadsafe_histogram.ranges(14)); 171 EXPECT_EQ(32, threadsafe_histogram->ranges(14));
153 EXPECT_EQ(INT_MAX, threadsafe_histogram.ranges(15)); 172 EXPECT_EQ(INT_MAX, threadsafe_histogram->ranges(15));
154 173
155 recorder.GetHistograms(&histograms); 174 recorder.GetHistograms(&histograms);
156 EXPECT_EQ(5U, histograms.size()); 175 EXPECT_EQ(5U, histograms.size());
157 } 176 }
158 177
159 // Make sure histogram handles out-of-bounds data gracefully. 178 // Make sure histogram handles out-of-bounds data gracefully.
160 TEST(HistogramTest, BoundsTest) { 179 TEST(HistogramTest, BoundsTest) {
161 const size_t kBucketCount = 50; 180 const size_t kBucketCount = 50;
162 Histogram histogram("Bounded", 10, 100, kBucketCount); 181 scoped_refptr<Histogram> histogram = Histogram::HistogramFactoryGet("Bounded",
182 10, 100, kBucketCount);
163 183
164 // Put two samples "out of bounds" above and below. 184 // Put two samples "out of bounds" above and below.
165 histogram.Add(5); 185 histogram->Add(5);
166 histogram.Add(-50); 186 histogram->Add(-50);
167 187
168 histogram.Add(100); 188 histogram->Add(100);
169 histogram.Add(10000); 189 histogram->Add(10000);
170 190
171 // Verify they landed in the underflow, and overflow buckets. 191 // Verify they landed in the underflow, and overflow buckets.
172 Histogram::SampleSet sample; 192 Histogram::SampleSet sample;
173 histogram.SnapshotSample(&sample); 193 histogram->SnapshotSample(&sample);
174 EXPECT_EQ(2, sample.counts(0)); 194 EXPECT_EQ(2, sample.counts(0));
175 EXPECT_EQ(0, sample.counts(1)); 195 EXPECT_EQ(0, sample.counts(1));
176 size_t array_size = histogram.bucket_count(); 196 size_t array_size = histogram->bucket_count();
177 EXPECT_EQ(kBucketCount, array_size); 197 EXPECT_EQ(kBucketCount, array_size);
178 EXPECT_EQ(0, sample.counts(array_size - 2)); 198 EXPECT_EQ(0, sample.counts(array_size - 2));
179 EXPECT_EQ(2, sample.counts(array_size - 1)); 199 EXPECT_EQ(2, sample.counts(array_size - 1));
180 } 200 }
181 201
182 // Check to be sure samples land as expected is "correct" buckets. 202 // Check to be sure samples land as expected is "correct" buckets.
183 TEST(HistogramTest, BucketPlacementTest) { 203 TEST(HistogramTest, BucketPlacementTest) {
184 Histogram histogram("Histogram", 1, 64, 8); // As mentioned in header file. 204 scoped_refptr<Histogram> histogram = Histogram::HistogramFactoryGet(
205 "Histogram", 1, 64, 8); // As mentioned in header file.
185 206
186 // Check that we got a nice exponential since there was enough rooom. 207 // Check that we got a nice exponential since there was enough rooom.
187 EXPECT_EQ(0, histogram.ranges(0)); 208 EXPECT_EQ(0, histogram->ranges(0));
188 int power_of_2 = 1; 209 int power_of_2 = 1;
189 for (int i = 1; i < 8; i++) { 210 for (int i = 1; i < 8; i++) {
190 EXPECT_EQ(power_of_2, histogram.ranges(i)); 211 EXPECT_EQ(power_of_2, histogram->ranges(i));
191 power_of_2 *= 2; 212 power_of_2 *= 2;
192 } 213 }
193 EXPECT_EQ(INT_MAX, histogram.ranges(8)); 214 EXPECT_EQ(INT_MAX, histogram->ranges(8));
194 215
195 // Add i+1 samples to the i'th bucket. 216 // Add i+1 samples to the i'th bucket.
196 histogram.Add(0); 217 histogram->Add(0);
197 power_of_2 = 1; 218 power_of_2 = 1;
198 for (int i = 1; i < 8; i++) { 219 for (int i = 1; i < 8; i++) {
199 for (int j = 0; j <= i; j++) 220 for (int j = 0; j <= i; j++)
200 histogram.Add(power_of_2); 221 histogram->Add(power_of_2);
201 power_of_2 *= 2; 222 power_of_2 *= 2;
202 } 223 }
203 // Leave overflow bucket empty. 224 // Leave overflow bucket empty.
204 225
205 // Check to see that the bucket counts reflect our additions. 226 // Check to see that the bucket counts reflect our additions.
206 Histogram::SampleSet sample; 227 Histogram::SampleSet sample;
207 histogram.SnapshotSample(&sample); 228 histogram->SnapshotSample(&sample);
208 EXPECT_EQ(INT_MAX, histogram.ranges(8)); 229 EXPECT_EQ(INT_MAX, histogram->ranges(8));
209 for (int i = 0; i < 8; i++) 230 for (int i = 0; i < 8; i++)
210 EXPECT_EQ(i + 1, sample.counts(i)); 231 EXPECT_EQ(i + 1, sample.counts(i));
211 } 232 }
212 233
213 static const char kAssetTestHistogramName[] = "AssetCountTest"; 234 static const char kAssetTestHistogramName[] = "AssetCountTest";
214 static const char kAssetTestDebugHistogramName[] = "DAssetCountTest"; 235 static const char kAssetTestDebugHistogramName[] = "DAssetCountTest";
215 void AssetCountFunction(int sample) { 236 void AssetCountFunction(int sample) {
216 ASSET_HISTOGRAM_COUNTS(kAssetTestHistogramName, sample); 237 ASSET_HISTOGRAM_COUNTS(kAssetTestHistogramName, sample);
217 DASSET_HISTOGRAM_COUNTS(kAssetTestDebugHistogramName, sample); 238 DASSET_HISTOGRAM_COUNTS(kAssetTestDebugHistogramName, sample);
218 } 239 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 AssetCountFunction(-100); // Remove a sample from the bucket for 100. 305 AssetCountFunction(-100); // Remove a sample from the bucket for 100.
285 our_debug_histogram->SnapshotSample(&sample); // Extract data set. 306 our_debug_histogram->SnapshotSample(&sample); // Extract data set.
286 307
287 // Verify that the bucket is now empty, as are all the other buckets. 308 // Verify that the bucket is now empty, as are all the other buckets.
288 for (size_t i = 0; i < our_debug_histogram->bucket_count(); ++i) { 309 for (size_t i = 0; i < our_debug_histogram->bucket_count(); ++i) {
289 EXPECT_EQ(0, sample.counts(i)) << "extra count in bucket " << i; 310 EXPECT_EQ(0, sample.counts(i)) << "extra count in bucket " << i;
290 } 311 }
291 } 312 }
292 313
293 } // namespace 314 } // namespace
OLDNEW
« no previous file with comments | « base/histogram.cc ('k') | base/message_loop.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698