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

Side by Side Diff: base/metrics/histogram_unittest.cc

Issue 1180693002: Update from https://crrev.com/333737 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: rebased Created 5 years, 6 months 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
« no previous file with comments | « base/metrics/histogram_snapshot_manager.cc ('k') | base/metrics/sample_map.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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "base/metrics/histogram.h"
6 6
7 #include <climits> 7 #include <climits>
8 #include <algorithm> 8 #include <algorithm>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/bucket_ranges.h" 13 #include "base/metrics/bucket_ranges.h"
14 #include "base/metrics/histogram.h"
15 #include "base/metrics/sample_vector.h" 14 #include "base/metrics/sample_vector.h"
16 #include "base/metrics/statistics_recorder.h" 15 #include "base/metrics/statistics_recorder.h"
17 #include "base/pickle.h" 16 #include "base/pickle.h"
18 #include "base/time/time.h" 17 #include "base/time/time.h"
19 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
20 19
21 using std::vector;
22
23 namespace base { 20 namespace base {
24 21
25 class HistogramTest : public testing::Test { 22 class HistogramTest : public testing::Test {
26 protected: 23 protected:
27 void SetUp() override { 24 void SetUp() override {
28 // Each test will have a clean state (no Histogram / BucketRanges 25 // Each test will have a clean state (no Histogram / BucketRanges
29 // registered). 26 // registered).
30 InitializeStatisticsRecorder(); 27 InitializeStatisticsRecorder();
31 } 28 }
32 29
(...skipping 15 matching lines...) Expand all
48 TEST_F(HistogramTest, BasicTest) { 45 TEST_F(HistogramTest, BasicTest) {
49 // Try basic construction 46 // Try basic construction
50 HistogramBase* histogram = Histogram::FactoryGet( 47 HistogramBase* histogram = Histogram::FactoryGet(
51 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 48 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
52 EXPECT_TRUE(histogram); 49 EXPECT_TRUE(histogram);
53 50
54 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( 51 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
55 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 52 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
56 EXPECT_TRUE(linear_histogram); 53 EXPECT_TRUE(linear_histogram);
57 54
58 vector<int> custom_ranges; 55 std::vector<int> custom_ranges;
59 custom_ranges.push_back(1); 56 custom_ranges.push_back(1);
60 custom_ranges.push_back(5); 57 custom_ranges.push_back(5);
61 HistogramBase* custom_histogram = CustomHistogram::FactoryGet( 58 HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
62 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags); 59 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
63 EXPECT_TRUE(custom_histogram); 60 EXPECT_TRUE(custom_histogram);
64 61
65 // Use standard macros (but with fixed samples) 62 // Use standard macros (but with fixed samples)
66 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); 63 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1));
67 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30); 64 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30);
68 65
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 EXPECT_EQ(6, ranges2.range(4)); 147 EXPECT_EQ(6, ranges2.range(4));
151 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges2.range(5)); 148 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges2.range(5));
152 // The correspoding LinearHistogram should use the correct ranges. 149 // The correspoding LinearHistogram should use the correct ranges.
153 Histogram* histogram2 = static_cast<Histogram*>( 150 Histogram* histogram2 = static_cast<Histogram*>(
154 LinearHistogram::FactoryGet("Linear2", 1, 6, 5, HistogramBase::kNoFlags)); 151 LinearHistogram::FactoryGet("Linear2", 1, 6, 5, HistogramBase::kNoFlags));
155 EXPECT_TRUE(ranges2.Equals(histogram2->bucket_ranges())); 152 EXPECT_TRUE(ranges2.Equals(histogram2->bucket_ranges()));
156 } 153 }
157 154
158 TEST_F(HistogramTest, ArrayToCustomRangesTest) { 155 TEST_F(HistogramTest, ArrayToCustomRangesTest) {
159 const HistogramBase::Sample ranges[3] = {5, 10, 20}; 156 const HistogramBase::Sample ranges[3] = {5, 10, 20};
160 vector<HistogramBase::Sample> ranges_vec = 157 std::vector<HistogramBase::Sample> ranges_vec =
161 CustomHistogram::ArrayToCustomRanges(ranges, 3); 158 CustomHistogram::ArrayToCustomRanges(ranges, 3);
162 ASSERT_EQ(6u, ranges_vec.size()); 159 ASSERT_EQ(6u, ranges_vec.size());
163 EXPECT_EQ(5, ranges_vec[0]); 160 EXPECT_EQ(5, ranges_vec[0]);
164 EXPECT_EQ(6, ranges_vec[1]); 161 EXPECT_EQ(6, ranges_vec[1]);
165 EXPECT_EQ(10, ranges_vec[2]); 162 EXPECT_EQ(10, ranges_vec[2]);
166 EXPECT_EQ(11, ranges_vec[3]); 163 EXPECT_EQ(11, ranges_vec[3]);
167 EXPECT_EQ(20, ranges_vec[4]); 164 EXPECT_EQ(20, ranges_vec[4]);
168 EXPECT_EQ(21, ranges_vec[5]); 165 EXPECT_EQ(21, ranges_vec[5]);
169 } 166 }
170 167
171 TEST_F(HistogramTest, CustomHistogramTest) { 168 TEST_F(HistogramTest, CustomHistogramTest) {
172 // A well prepared custom ranges. 169 // A well prepared custom ranges.
173 vector<HistogramBase::Sample> custom_ranges; 170 std::vector<HistogramBase::Sample> custom_ranges;
174 custom_ranges.push_back(1); 171 custom_ranges.push_back(1);
175 custom_ranges.push_back(2); 172 custom_ranges.push_back(2);
176 173
177 Histogram* histogram = static_cast<Histogram*>( 174 Histogram* histogram = static_cast<Histogram*>(
178 CustomHistogram::FactoryGet("TestCustomHistogram1", custom_ranges, 175 CustomHistogram::FactoryGet("TestCustomHistogram1", custom_ranges,
179 HistogramBase::kNoFlags)); 176 HistogramBase::kNoFlags));
180 const BucketRanges* ranges = histogram->bucket_ranges(); 177 const BucketRanges* ranges = histogram->bucket_ranges();
181 ASSERT_EQ(4u, ranges->size()); 178 ASSERT_EQ(4u, ranges->size());
182 EXPECT_EQ(0, ranges->range(0)); // Auto added. 179 EXPECT_EQ(0, ranges->range(0)); // Auto added.
183 EXPECT_EQ(1, ranges->range(1)); 180 EXPECT_EQ(1, ranges->range(1));
(...skipping 29 matching lines...) Expand all
213 EXPECT_EQ(4, ranges->range(2)); 210 EXPECT_EQ(4, ranges->range(2));
214 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(3)); 211 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(3));
215 } 212 }
216 213
217 TEST_F(HistogramTest, CustomHistogramWithOnly2Buckets) { 214 TEST_F(HistogramTest, CustomHistogramWithOnly2Buckets) {
218 // This test exploits the fact that the CustomHistogram can have 2 buckets, 215 // This test exploits the fact that the CustomHistogram can have 2 buckets,
219 // while the base class Histogram is *supposed* to have at least 3 buckets. 216 // while the base class Histogram is *supposed* to have at least 3 buckets.
220 // We should probably change the restriction on the base class (or not inherit 217 // We should probably change the restriction on the base class (or not inherit
221 // the base class!). 218 // the base class!).
222 219
223 vector<HistogramBase::Sample> custom_ranges; 220 std::vector<HistogramBase::Sample> custom_ranges;
224 custom_ranges.push_back(4); 221 custom_ranges.push_back(4);
225 222
226 Histogram* histogram = static_cast<Histogram*>( 223 Histogram* histogram = static_cast<Histogram*>(
227 CustomHistogram::FactoryGet("2BucketsCustomHistogram", custom_ranges, 224 CustomHistogram::FactoryGet("2BucketsCustomHistogram", custom_ranges,
228 HistogramBase::kNoFlags)); 225 HistogramBase::kNoFlags));
229 const BucketRanges* ranges = histogram->bucket_ranges(); 226 const BucketRanges* ranges = histogram->bucket_ranges();
230 ASSERT_EQ(3u, ranges->size()); 227 ASSERT_EQ(3u, ranges->size());
231 EXPECT_EQ(0, ranges->range(0)); 228 EXPECT_EQ(0, ranges->range(0));
232 EXPECT_EQ(4, ranges->range(1)); 229 EXPECT_EQ(4, ranges->range(1));
233 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2)); 230 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2));
(...skipping 15 matching lines...) Expand all
249 246
250 // Verify they landed in the underflow, and overflow buckets. 247 // Verify they landed in the underflow, and overflow buckets.
251 scoped_ptr<SampleVector> samples = histogram->SnapshotSampleVector(); 248 scoped_ptr<SampleVector> samples = histogram->SnapshotSampleVector();
252 EXPECT_EQ(2, samples->GetCountAtIndex(0)); 249 EXPECT_EQ(2, samples->GetCountAtIndex(0));
253 EXPECT_EQ(0, samples->GetCountAtIndex(1)); 250 EXPECT_EQ(0, samples->GetCountAtIndex(1));
254 size_t array_size = histogram->bucket_count(); 251 size_t array_size = histogram->bucket_count();
255 EXPECT_EQ(kBucketCount, array_size); 252 EXPECT_EQ(kBucketCount, array_size);
256 EXPECT_EQ(0, samples->GetCountAtIndex(array_size - 2)); 253 EXPECT_EQ(0, samples->GetCountAtIndex(array_size - 2));
257 EXPECT_EQ(2, samples->GetCountAtIndex(array_size - 1)); 254 EXPECT_EQ(2, samples->GetCountAtIndex(array_size - 1));
258 255
259 vector<int> custom_ranges; 256 std::vector<int> custom_ranges;
260 custom_ranges.push_back(10); 257 custom_ranges.push_back(10);
261 custom_ranges.push_back(50); 258 custom_ranges.push_back(50);
262 custom_ranges.push_back(100); 259 custom_ranges.push_back(100);
263 Histogram* test_custom_histogram = static_cast<Histogram*>( 260 Histogram* test_custom_histogram = static_cast<Histogram*>(
264 CustomHistogram::FactoryGet("TestCustomRangeBoundedHistogram", 261 CustomHistogram::FactoryGet("TestCustomRangeBoundedHistogram",
265 custom_ranges, HistogramBase::kNoFlags)); 262 custom_ranges, HistogramBase::kNoFlags));
266 263
267 // Put two samples "out of bounds" above and below. 264 // Put two samples "out of bounds" above and below.
268 test_custom_histogram->Add(5); 265 test_custom_histogram->Add(5);
269 test_custom_histogram->Add(-50); 266 test_custom_histogram->Add(-50);
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 395
399 uint32 checksum; 396 uint32 checksum;
400 EXPECT_TRUE(iter.ReadUInt32(&checksum)); 397 EXPECT_TRUE(iter.ReadUInt32(&checksum));
401 EXPECT_EQ(histogram->bucket_ranges()->checksum(), checksum); 398 EXPECT_EQ(histogram->bucket_ranges()->checksum(), checksum);
402 399
403 // No more data in the pickle. 400 // No more data in the pickle.
404 EXPECT_FALSE(iter.SkipBytes(1)); 401 EXPECT_FALSE(iter.SkipBytes(1));
405 } 402 }
406 403
407 TEST_F(HistogramTest, CustomHistogramSerializeInfo) { 404 TEST_F(HistogramTest, CustomHistogramSerializeInfo) {
408 vector<int> custom_ranges; 405 std::vector<int> custom_ranges;
409 custom_ranges.push_back(10); 406 custom_ranges.push_back(10);
410 custom_ranges.push_back(100); 407 custom_ranges.push_back(100);
411 408
412 HistogramBase* custom_histogram = CustomHistogram::FactoryGet( 409 HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
413 "TestCustomRangeBoundedHistogram", 410 "TestCustomRangeBoundedHistogram",
414 custom_ranges, 411 custom_ranges,
415 HistogramBase::kNoFlags); 412 HistogramBase::kNoFlags);
416 Pickle pickle; 413 Pickle pickle;
417 custom_histogram->SerializeInfo(&pickle); 414 custom_histogram->SerializeInfo(&pickle);
418 415
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 histogram->HasConstructionArguments( 474 histogram->HasConstructionArguments(
478 1, HistogramBase::kSampleType_MAX - 1, 8)); 475 1, HistogramBase::kSampleType_MAX - 1, 8));
479 476
480 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( 477 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
481 "BadRangesLinear", 0, HistogramBase::kSampleType_MAX, 8, 478 "BadRangesLinear", 0, HistogramBase::kSampleType_MAX, 8,
482 HistogramBase::kNoFlags); 479 HistogramBase::kNoFlags);
483 EXPECT_TRUE( 480 EXPECT_TRUE(
484 linear_histogram->HasConstructionArguments( 481 linear_histogram->HasConstructionArguments(
485 1, HistogramBase::kSampleType_MAX - 1, 8)); 482 1, HistogramBase::kSampleType_MAX - 1, 8));
486 483
487 vector<int> custom_ranges; 484 std::vector<int> custom_ranges;
488 custom_ranges.push_back(0); 485 custom_ranges.push_back(0);
489 custom_ranges.push_back(5); 486 custom_ranges.push_back(5);
490 Histogram* custom_histogram = static_cast<Histogram*>( 487 Histogram* custom_histogram = static_cast<Histogram*>(
491 CustomHistogram::FactoryGet( 488 CustomHistogram::FactoryGet(
492 "BadRangesCustom", custom_ranges, HistogramBase::kNoFlags)); 489 "BadRangesCustom", custom_ranges, HistogramBase::kNoFlags));
493 const BucketRanges* ranges = custom_histogram->bucket_ranges(); 490 const BucketRanges* ranges = custom_histogram->bucket_ranges();
494 ASSERT_EQ(3u, ranges->size()); 491 ASSERT_EQ(3u, ranges->size());
495 EXPECT_EQ(0, ranges->range(0)); 492 EXPECT_EQ(0, ranges->range(0));
496 EXPECT_EQ(5, ranges->range(1)); 493 EXPECT_EQ(5, ranges->range(1));
497 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2)); 494 EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges->range(2));
498 495
499 // CustomHistogram does not accepts kSampleType_MAX as range. 496 // CustomHistogram does not accepts kSampleType_MAX as range.
500 custom_ranges.push_back(HistogramBase::kSampleType_MAX); 497 custom_ranges.push_back(HistogramBase::kSampleType_MAX);
501 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom2", custom_ranges, 498 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom2", custom_ranges,
502 HistogramBase::kNoFlags), 499 HistogramBase::kNoFlags),
503 ""); 500 "");
504 501
505 // CustomHistogram needs at least 1 valid range. 502 // CustomHistogram needs at least 1 valid range.
506 custom_ranges.clear(); 503 custom_ranges.clear();
507 custom_ranges.push_back(0); 504 custom_ranges.push_back(0);
508 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, 505 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges,
509 HistogramBase::kNoFlags), 506 HistogramBase::kNoFlags),
510 ""); 507 "");
511 } 508 }
512 #endif 509 #endif
513 510
514 } // namespace base 511 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/histogram_snapshot_manager.cc ('k') | base/metrics/sample_map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698