OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/metrics/file_metrics_provider.h" | 5 #include "components/metrics/file_metrics_provider.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/memory_mapped_file.h" | 8 #include "base/files/memory_mapped_file.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/metrics/histogram_flattener.h" | 11 #include "base/metrics/histogram_flattener.h" |
12 #include "base/metrics/histogram_snapshot_manager.h" | 12 #include "base/metrics/histogram_snapshot_manager.h" |
13 #include "base/metrics/persistent_histogram_allocator.h" | 13 #include "base/metrics/persistent_histogram_allocator.h" |
14 #include "base/metrics/persistent_memory_allocator.h" | 14 #include "base/metrics/persistent_memory_allocator.h" |
| 15 #include "base/metrics/sparse_histogram.h" |
15 #include "base/metrics/statistics_recorder.h" | 16 #include "base/metrics/statistics_recorder.h" |
| 17 #include "base/strings/stringprintf.h" |
16 #include "base/test/test_simple_task_runner.h" | 18 #include "base/test/test_simple_task_runner.h" |
17 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
18 #include "components/metrics/metrics_pref_names.h" | 20 #include "components/metrics/metrics_pref_names.h" |
19 #include "components/prefs/pref_registry_simple.h" | 21 #include "components/prefs/pref_registry_simple.h" |
20 #include "components/prefs/testing_pref_service.h" | 22 #include "components/prefs/testing_pref_service.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 | 24 |
23 namespace { | 25 namespace { |
24 const char kMetricsName[] = "TestMetrics"; | 26 const char kMetricsName[] = "TestMetrics"; |
25 const char kMetricsFilename[] = "file.metrics"; | 27 const char kMetricsFilename[] = "file.metrics"; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 FileMetricsProvider* provider() { | 81 FileMetricsProvider* provider() { |
80 if (!provider_) | 82 if (!provider_) |
81 provider_.reset(new FileMetricsProvider(task_runner_, prefs())); | 83 provider_.reset(new FileMetricsProvider(task_runner_, prefs())); |
82 return provider_.get(); | 84 return provider_.get(); |
83 } | 85 } |
84 | 86 |
85 void RunTasks() { | 87 void RunTasks() { |
86 task_runner_->RunUntilIdle(); | 88 task_runner_->RunUntilIdle(); |
87 } | 89 } |
88 | 90 |
| 91 void CreateMetricsFileWithHistograms(int histogram_count) { |
| 92 // Get this first so it isn't created inside the persistent allocator. |
| 93 base::GlobalHistogramAllocator::GetCreateHistogramResultHistogram(); |
| 94 |
| 95 base::GlobalHistogramAllocator::CreateWithLocalMemory( |
| 96 64 << 10, 0, kMetricsName); |
| 97 |
| 98 // Create both sparse and normal histograms in the allocator. |
| 99 base::SparseHistogram::FactoryGet("h0", 0)->Add(0); |
| 100 for (int i = 1; i < histogram_count; ++i) { |
| 101 base::Histogram::FactoryGet(base::StringPrintf("h%d", i), 1, 100, 10, 0) |
| 102 ->Add(i); |
| 103 } |
| 104 |
| 105 std::unique_ptr<base::PersistentHistogramAllocator> histogram_allocator = |
| 106 base::GlobalHistogramAllocator::ReleaseForTesting(); |
| 107 base::PersistentMemoryAllocator* allocator = |
| 108 histogram_allocator->memory_allocator(); |
| 109 base::File writer(metrics_file(), |
| 110 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 111 ASSERT_TRUE(writer.IsValid()); |
| 112 ASSERT_EQ(static_cast<int>(allocator->used()), |
| 113 writer.Write(0, (const char*)allocator->data(), |
| 114 allocator->used())); |
| 115 } |
| 116 |
89 private: | 117 private: |
90 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 118 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
91 base::ThreadTaskRunnerHandle thread_task_runner_handle_; | 119 base::ThreadTaskRunnerHandle thread_task_runner_handle_; |
92 | 120 |
93 base::ScopedTempDir temp_dir_; | 121 base::ScopedTempDir temp_dir_; |
94 std::unique_ptr<TestingPrefServiceSimple> prefs_; | 122 std::unique_ptr<TestingPrefServiceSimple> prefs_; |
95 std::unique_ptr<FileMetricsProvider> provider_; | 123 std::unique_ptr<FileMetricsProvider> provider_; |
96 | 124 |
97 DISALLOW_COPY_AND_ASSIGN(FileMetricsProviderTest); | 125 DISALLOW_COPY_AND_ASSIGN(FileMetricsProviderTest); |
98 }; | 126 }; |
99 | 127 |
100 | |
101 TEST_F(FileMetricsProviderTest, AccessMetrics) { | 128 TEST_F(FileMetricsProviderTest, AccessMetrics) { |
102 ASSERT_FALSE(PathExists(metrics_file())); | 129 ASSERT_FALSE(PathExists(metrics_file())); |
103 | 130 CreateMetricsFileWithHistograms(2); |
104 { | |
105 // Get this first so it isn't created inside the persistent allocator. | |
106 base::GlobalHistogramAllocator::GetCreateHistogramResultHistogram(); | |
107 | |
108 base::GlobalHistogramAllocator::CreateWithLocalMemory( | |
109 64 << 10, 0, kMetricsName); | |
110 base::HistogramBase* foo = | |
111 base::Histogram::FactoryGet("foo", 1, 100, 10, 0); | |
112 base::HistogramBase* bar = | |
113 base::Histogram::FactoryGet("bar", 1, 100, 10, 0); | |
114 foo->Add(42); | |
115 bar->Add(84); | |
116 | |
117 std::unique_ptr<base::PersistentHistogramAllocator> histogram_allocator = | |
118 base::GlobalHistogramAllocator::ReleaseForTesting(); | |
119 base::PersistentMemoryAllocator* allocator = | |
120 histogram_allocator->memory_allocator(); | |
121 base::File writer(metrics_file(), | |
122 base::File::FLAG_CREATE | base::File::FLAG_WRITE); | |
123 ASSERT_TRUE(writer.IsValid()); | |
124 ASSERT_EQ(static_cast<int>(allocator->used()), | |
125 writer.Write(0, (const char*)allocator->data(), | |
126 allocator->used())); | |
127 } | |
128 | 131 |
129 // Register the file and allow the "checker" task to run. | 132 // Register the file and allow the "checker" task to run. |
130 ASSERT_TRUE(PathExists(metrics_file())); | 133 ASSERT_TRUE(PathExists(metrics_file())); |
131 provider()->RegisterFile(metrics_file(), | 134 provider()->RegisterFile( |
132 FileMetricsProvider::FILE_HISTOGRAMS_ATOMIC, | 135 metrics_file(), |
133 kMetricsName); | 136 FileMetricsProvider::FILE_HISTOGRAMS_ATOMIC, |
| 137 FileMetricsProvider::ASSOCIATE_CURRENT_RUN, |
| 138 kMetricsName); |
134 | 139 |
135 // Record embedded snapshots via snapshot-manager. | 140 // Record embedded snapshots via snapshot-manager. |
136 provider()->OnDidCreateMetricsLog(); | 141 provider()->OnDidCreateMetricsLog(); |
137 RunTasks(); | 142 RunTasks(); |
138 { | 143 { |
139 HistogramFlattenerDeltaRecorder flattener; | 144 HistogramFlattenerDeltaRecorder flattener; |
140 base::HistogramSnapshotManager snapshot_manager(&flattener); | 145 base::HistogramSnapshotManager snapshot_manager(&flattener); |
141 snapshot_manager.StartDeltas(); | 146 snapshot_manager.StartDeltas(); |
142 provider()->RecordHistogramSnapshots(&snapshot_manager); | 147 provider()->RecordHistogramSnapshots(&snapshot_manager); |
143 snapshot_manager.FinishDeltas(); | 148 snapshot_manager.FinishDeltas(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 { | 187 { |
183 HistogramFlattenerDeltaRecorder flattener; | 188 HistogramFlattenerDeltaRecorder flattener; |
184 base::HistogramSnapshotManager snapshot_manager(&flattener); | 189 base::HistogramSnapshotManager snapshot_manager(&flattener); |
185 snapshot_manager.StartDeltas(); | 190 snapshot_manager.StartDeltas(); |
186 provider()->RecordHistogramSnapshots(&snapshot_manager); | 191 provider()->RecordHistogramSnapshots(&snapshot_manager); |
187 snapshot_manager.FinishDeltas(); | 192 snapshot_manager.FinishDeltas(); |
188 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); | 193 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); |
189 } | 194 } |
190 } | 195 } |
191 | 196 |
| 197 TEST_F(FileMetricsProviderTest, AccessInitialMetrics) { |
| 198 ASSERT_FALSE(PathExists(metrics_file())); |
| 199 CreateMetricsFileWithHistograms(2); |
| 200 |
| 201 // Register the file and allow the "checker" task to run. |
| 202 ASSERT_TRUE(PathExists(metrics_file())); |
| 203 provider()->RegisterFile( |
| 204 metrics_file(), |
| 205 FileMetricsProvider::FILE_HISTOGRAMS_ATOMIC, |
| 206 FileMetricsProvider::ASSOCIATE_PREVIOUS_RUN, |
| 207 kMetricsName); |
| 208 |
| 209 // Record embedded snapshots via snapshot-manager. |
| 210 provider()->HasInitialStabilityMetrics(); |
| 211 { |
| 212 HistogramFlattenerDeltaRecorder flattener; |
| 213 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 214 snapshot_manager.StartDeltas(); |
| 215 provider()->RecordInitialHistogramSnapshots(&snapshot_manager); |
| 216 snapshot_manager.FinishDeltas(); |
| 217 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 218 } |
| 219 |
| 220 // Second full run on the same file should produce nothing. |
| 221 provider()->OnDidCreateMetricsLog(); |
| 222 RunTasks(); |
| 223 { |
| 224 HistogramFlattenerDeltaRecorder flattener; |
| 225 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 226 snapshot_manager.StartDeltas(); |
| 227 provider()->RecordInitialHistogramSnapshots(&snapshot_manager); |
| 228 snapshot_manager.FinishDeltas(); |
| 229 EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 230 } |
| 231 |
| 232 // A run for normal histograms should produce nothing. |
| 233 { |
| 234 HistogramFlattenerDeltaRecorder flattener; |
| 235 base::HistogramSnapshotManager snapshot_manager(&flattener); |
| 236 snapshot_manager.StartDeltas(); |
| 237 provider()->RecordHistogramSnapshots(&snapshot_manager); |
| 238 snapshot_manager.FinishDeltas(); |
| 239 EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size()); |
| 240 } |
| 241 } |
| 242 |
192 } // namespace metrics | 243 } // namespace metrics |
OLD | NEW |