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

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

Issue 1425533011: Support "shared" histograms between processes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shmem-alloc
Patch Set: added a couple tests (and fixed related issues) Created 5 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
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 #include "base/metrics/histogram.h" 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/persistent_memory_allocator.h"
12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/bucket_ranges.h" 14 #include "base/metrics/bucket_ranges.h"
14 #include "base/metrics/histogram_macros.h" 15 #include "base/metrics/histogram_macros.h"
15 #include "base/metrics/sample_vector.h" 16 #include "base/metrics/sample_vector.h"
16 #include "base/metrics/statistics_recorder.h" 17 #include "base/metrics/statistics_recorder.h"
17 #include "base/pickle.h" 18 #include "base/pickle.h"
18 #include "base/time/time.h" 19 #include "base/time/time.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
21 namespace base { 22 namespace base {
22 23
23 class HistogramTest : public testing::Test { 24 class HistogramTest : public testing::Test {
24 protected: 25 protected:
26 const int32_t kAllocatorMemorySize = 64 << 10; // 64 KiB
27
25 void SetUp() override { 28 void SetUp() override {
26 // Each test will have a clean state (no Histogram / BucketRanges 29 // Each test will have a clean state (no Histogram / BucketRanges
27 // registered). 30 // registered).
28 InitializeStatisticsRecorder(); 31 InitializeStatisticsRecorder();
29 } 32 }
30 33
31 void TearDown() override { UninitializeStatisticsRecorder(); } 34 void TearDown() override {
35 UninitializeStatisticsRecorder();
36 DestroyPersistentMemoryAllocator();
37 }
32 38
33 void InitializeStatisticsRecorder() { 39 void InitializeStatisticsRecorder() {
34 statistics_recorder_ = new StatisticsRecorder(); 40 statistics_recorder_ = new StatisticsRecorder();
35 } 41 }
36 42
37 void UninitializeStatisticsRecorder() { 43 void UninitializeStatisticsRecorder() {
38 delete statistics_recorder_; 44 delete statistics_recorder_;
39 statistics_recorder_ = NULL; 45 statistics_recorder_ = NULL;
40 } 46 }
41 47
48 void CreatePersistentMemoryAllocator() {
49 if (!allocator_memory_)
50 allocator_memory_.reset(new char[kAllocatorMemorySize]);
51
52 HistogramBase::SetDefaultPersistentMemoryAllocator(nullptr);
53 memset(allocator_memory_.get(), 0, kAllocatorMemorySize);
54 HistogramBase::SetDefaultPersistentMemoryAllocator(
55 new PersistentMemoryAllocator(
56 allocator_memory_.get(), kAllocatorMemorySize, 0,
57 "HistogramAllocatorTest"));
58 allocator_ = HistogramBase::GetDefaultPersistentMemoryAllocator();
59 }
60
61 void DestroyPersistentMemoryAllocator() {
62 allocator_ = nullptr;
63 HistogramBase::SetDefaultPersistentMemoryAllocator(nullptr);
64 }
65
42 StatisticsRecorder* statistics_recorder_; 66 StatisticsRecorder* statistics_recorder_;
67 scoped_ptr<char[]> allocator_memory_;
68 PersistentMemoryAllocator* allocator_;
43 }; 69 };
44 70
45 // Check for basic syntax and use. 71 // Check for basic syntax and use.
46 TEST_F(HistogramTest, BasicTest) { 72 TEST_F(HistogramTest, BasicTest) {
47 // Try basic construction 73 // Try basic construction
48 HistogramBase* histogram = Histogram::FactoryGet( 74 HistogramBase* histogram = Histogram::FactoryGet(
49 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 75 "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
50 EXPECT_TRUE(histogram); 76 EXPECT_TRUE(histogram);
51 77
52 HistogramBase* linear_histogram = LinearHistogram::FactoryGet( 78 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
53 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags); 79 "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
54 EXPECT_TRUE(linear_histogram); 80 EXPECT_TRUE(linear_histogram);
55 81
56 std::vector<int> custom_ranges; 82 std::vector<int> custom_ranges;
57 custom_ranges.push_back(1); 83 custom_ranges.push_back(1);
58 custom_ranges.push_back(5); 84 custom_ranges.push_back(5);
59 HistogramBase* custom_histogram = CustomHistogram::FactoryGet( 85 HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
60 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags); 86 "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
61 EXPECT_TRUE(custom_histogram); 87 EXPECT_TRUE(custom_histogram);
62 88
63 // Use standard macros (but with fixed samples) 89 // Use standard macros (but with fixed samples)
64 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1)); 90 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1));
65 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30); 91 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30);
66 92
67 LOCAL_HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130); 93 LOCAL_HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130);
68 } 94 }
69 95
96 // Check for basic syntax and use.
97 TEST_F(HistogramTest, PersistentTest) {
98 CreatePersistentMemoryAllocator();
99 PersistentMemoryAllocator::MemoryInfo meminfo0;
100 allocator_->GetMemoryInfo(&meminfo0);
101
102 // Try basic construction
103 HistogramBase* histogram = Histogram::FactoryGet(
104 "TestHistogram", 1, 1000, 10,
105 HistogramBase::kIsPersistent);
106 EXPECT_TRUE(histogram);
107 histogram->CheckName("TestHistogram");
108 PersistentMemoryAllocator::MemoryInfo meminfo1;
109 allocator_->GetMemoryInfo(&meminfo1);
110 EXPECT_GT(meminfo0.free, meminfo1.free);
111
112 HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
113 "TestLinearHistogram", 1, 1000, 10,
114 HistogramBase::kIsPersistent);
115 EXPECT_TRUE(linear_histogram);
116 linear_histogram->CheckName("TestLinearHistogram");
117 PersistentMemoryAllocator::MemoryInfo meminfo2;
118 allocator_->GetMemoryInfo(&meminfo2);
119 EXPECT_GT(meminfo1.free, meminfo2.free);
120
121 HistogramBase* boolean_histogram = BooleanHistogram::FactoryGet(
122 "TestBooleanHistogram", HistogramBase::kIsPersistent);
123 EXPECT_TRUE(boolean_histogram);
124 boolean_histogram->CheckName("TestBooleanHistogram");
125 PersistentMemoryAllocator::MemoryInfo meminfo3;
126 allocator_->GetMemoryInfo(&meminfo3);
127 EXPECT_GT(meminfo2.free, meminfo3.free);
128
129 std::vector<int> custom_ranges;
130 custom_ranges.push_back(1);
131 custom_ranges.push_back(5);
132 HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
133 "TestCustomHistogram", custom_ranges,
134 HistogramBase::kIsPersistent);
135 EXPECT_TRUE(custom_histogram);
136 custom_histogram->CheckName("TestCustomHistogram");
137 PersistentMemoryAllocator::MemoryInfo meminfo4;
138 allocator_->GetMemoryInfo(&meminfo4);
139 EXPECT_GT(meminfo3.free, meminfo4.free);
140
141 PersistentMemoryAllocator::Iterator iter;
142 uint32_t type;
143 allocator_->CreateIterator(&iter);
144 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // Histogram
145 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // LinearHistogram
146 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // BooleanHistogram
147 EXPECT_NE(0U, allocator_->GetNextIterable(&iter, &type)); // CustomHistogram
148 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type));
149
150 // Create a second allocator and have it access the memory of the first.
151 scoped_ptr<HistogramBase> recovered;
152 PersistentMemoryAllocator recovery(
153 allocator_memory_.get(), kAllocatorMemorySize, 0,
154 std::string());
155 recovery.CreateIterator(&iter);
156
157 recovered.reset(HistogramBase::GetNextPersistentHistogram(&recovery, &iter));
158 EXPECT_TRUE(recovered);
159 recovered->CheckName("TestHistogram");
160
161 recovered.reset(HistogramBase::GetNextPersistentHistogram(&recovery, &iter));
162 EXPECT_TRUE(recovered);
163 recovered->CheckName("TestLinearHistogram");
164
165 recovered.reset(HistogramBase::GetNextPersistentHistogram(&recovery, &iter));
166 EXPECT_TRUE(recovered);
167 recovered->CheckName("TestBooleanHistogram");
168
169 recovered.reset(HistogramBase::GetNextPersistentHistogram(&recovery, &iter));
170 EXPECT_TRUE(recovered);
171 recovered->CheckName("TestCustomHistogram");
172
173 recovered.reset(HistogramBase::GetNextPersistentHistogram(&recovery, &iter));
174 EXPECT_FALSE(recovered);
175
176 // Use standard macros (but with fixed samples)
177 LOCAL_HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1));
178 LOCAL_HISTOGRAM_COUNTS("Test3Histogram", 30);
179 LOCAL_HISTOGRAM_ENUMERATION("Test6Histogram", 129, 130);
180 }
181
70 // Check that the macro correctly matches histograms by name and records their 182 // Check that the macro correctly matches histograms by name and records their
71 // data together. 183 // data together.
72 TEST_F(HistogramTest, NameMatchTest) { 184 TEST_F(HistogramTest, NameMatchTest) {
73 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); 185 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10);
74 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10); 186 LOCAL_HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10);
75 HistogramBase* histogram = LinearHistogram::FactoryGet( 187 HistogramBase* histogram = LinearHistogram::FactoryGet(
76 "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags); 188 "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags);
77 189
78 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); 190 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
79 EXPECT_EQ(2, samples->TotalCount()); 191 EXPECT_EQ(2, samples->TotalCount());
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 // CustomHistogram needs at least 1 valid range. 639 // CustomHistogram needs at least 1 valid range.
528 custom_ranges.clear(); 640 custom_ranges.clear();
529 custom_ranges.push_back(0); 641 custom_ranges.push_back(0);
530 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges, 642 EXPECT_DEATH(CustomHistogram::FactoryGet("BadRangesCustom3", custom_ranges,
531 HistogramBase::kNoFlags), 643 HistogramBase::kNoFlags),
532 ""); 644 "");
533 } 645 }
534 #endif 646 #endif
535 647
536 } // namespace base 648 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698