OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/metrics/file_metrics_provider.h" | |
6 | |
7 #include "base/files/file_util.h" | |
8 #include "base/files/memory_mapped_file.h" | |
9 #include "base/files/scoped_temp_dir.h" | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/metrics/histogram_flattener.h" | |
12 #include "base/metrics/histogram_persistence.h" | |
13 #include "base/metrics/histogram_snapshot_manager.h" | |
14 #include "base/metrics/persistent_memory_allocator.h" | |
15 #include "base/metrics/statistics_recorder.h" | |
16 #include "base/test/test_simple_task_runner.h" | |
17 #include "base/thread_task_runner_handle.h" | |
18 #include "components/metrics/metrics_pref_names.h" | |
19 #include "components/prefs/pref_registry_simple.h" | |
20 #include "components/prefs/testing_pref_service.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 using namespace base; | |
Alexei Svitkine (slow)
2016/02/17 21:55:36
Nit: I'm not a fan of using namespace in metrics c
bcwhite
2016/02/18 01:46:57
Even in unittest code? Okay. Done.
| |
24 | |
25 namespace { | |
26 const char kMetricsName[] = "TestMetrics"; | |
27 const char kMetricsFilename[] = "file.metrics"; | |
28 } // namespace | |
29 | |
30 namespace metrics { | |
31 | |
32 class HistogramFlattenerDeltaRecorder : public HistogramFlattener { | |
33 public: | |
34 HistogramFlattenerDeltaRecorder() {} | |
35 | |
36 void RecordDelta(const HistogramBase& histogram, | |
37 const HistogramSamples& snapshot) override { | |
38 recorded_delta_histogram_names_.push_back(histogram.histogram_name()); | |
39 } | |
40 | |
41 void InconsistencyDetected(HistogramBase::Inconsistency problem) override { | |
42 ASSERT_TRUE(false); | |
43 } | |
44 | |
45 void UniqueInconsistencyDetected( | |
46 HistogramBase::Inconsistency problem) override { | |
47 ASSERT_TRUE(false); | |
48 } | |
49 | |
50 void InconsistencyDetectedInLoggedCount(int amount) override { | |
51 ASSERT_TRUE(false); | |
52 } | |
53 | |
54 std::vector<std::string> GetRecordedDeltaHistogramNames() { | |
55 return recorded_delta_histogram_names_; | |
56 } | |
57 | |
58 private: | |
59 std::vector<std::string> recorded_delta_histogram_names_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder); | |
62 }; | |
63 | |
64 class FileMetricsProviderTest : public testing::Test { | |
65 protected: | |
66 FileMetricsProviderTest() | |
67 : task_runner_(new TestSimpleTaskRunner()), | |
68 thread_task_runner_handle_(task_runner_), | |
69 prefs_(new TestingPrefServiceSimple) { | |
70 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
71 FileMetricsProvider::RegisterPrefs(prefs_->registry(), kMetricsName); | |
72 } | |
73 | |
74 TestingPrefServiceSimple* prefs() { return prefs_.get(); } | |
75 FilePath temp_dir() { return temp_dir_.path(); } | |
76 FilePath metrics_file() { | |
77 return temp_dir_.path().AppendASCII(kMetricsFilename); | |
78 } | |
79 | |
80 FileMetricsProvider* provider() { | |
81 if (!provider_) | |
82 provider_.reset(new FileMetricsProvider(task_runner_, prefs())); | |
83 return provider_.get(); | |
84 } | |
85 | |
86 void RunTasks() { | |
87 task_runner_->RunUntilIdle(); | |
88 } | |
89 | |
90 private: | |
91 scoped_refptr<TestSimpleTaskRunner> task_runner_; | |
92 ThreadTaskRunnerHandle thread_task_runner_handle_; | |
93 | |
94 ScopedTempDir temp_dir_; | |
95 scoped_ptr<TestingPrefServiceSimple> prefs_; | |
96 scoped_ptr<FileMetricsProvider> provider_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(FileMetricsProviderTest); | |
99 }; | |
100 | |
101 | |
102 TEST_F(FileMetricsProviderTest, AccessMetrics) { | |
103 ASSERT_FALSE(PathExists(metrics_file())); | |
104 | |
105 { | |
106 // Get this first so it isn't created inside the persistent allocator. | |
107 GetCreateHistogramResultHistogram(); | |
108 | |
109 SetPersistentHistogramMemoryAllocator( | |
110 new LocalPersistentMemoryAllocator(64 << 10, 0, kMetricsName)); | |
111 HistogramBase* foo = Histogram::FactoryGet("foo", 1, 100, 10, 0); | |
112 HistogramBase* bar = Histogram::FactoryGet("bar", 1, 100, 10, 0); | |
113 foo->Add(42); | |
114 bar->Add(84); | |
115 | |
116 scoped_ptr<PersistentMemoryAllocator> allocator( | |
117 ReleasePersistentHistogramMemoryAllocatorForTesting()); | |
118 File writer(metrics_file(), File::FLAG_CREATE | File::FLAG_WRITE); | |
119 ASSERT_TRUE(writer.IsValid()); | |
120 ASSERT_EQ(static_cast<int>(allocator->used()), | |
121 writer.Write(0, (const char*)allocator->data(), | |
122 allocator->used())); | |
123 } | |
124 | |
125 // Register the file and allow the "checker" task to run. | |
126 ASSERT_TRUE(PathExists(metrics_file())); | |
127 provider()->RegisterFile(metrics_file(), | |
128 FileMetricsProvider::FILE_HISTOGRAMS_ATOMIC, | |
129 kMetricsName); | |
130 | |
131 // Record embedded snapshots via snapshot-manager. | |
132 provider()->OnDidCreateMetricsLog(); | |
133 RunTasks(); | |
134 { | |
135 HistogramFlattenerDeltaRecorder flattener; | |
136 HistogramSnapshotManager snapshot_manager(&flattener); | |
137 snapshot_manager.StartDeltas(); | |
138 provider()->RecordHistogramSnapshots(&snapshot_manager); | |
139 snapshot_manager.FinishDeltas(); | |
140 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); | |
141 } | |
142 | |
143 // Make sure a second call to the snapshot-recorder doesn't break anything. | |
144 { | |
145 HistogramFlattenerDeltaRecorder flattener; | |
146 HistogramSnapshotManager snapshot_manager(&flattener); | |
147 snapshot_manager.StartDeltas(); | |
148 provider()->RecordHistogramSnapshots(&snapshot_manager); | |
149 snapshot_manager.FinishDeltas(); | |
150 EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size()); | |
151 } | |
152 | |
153 // Second full run on the same file should produce nothing. | |
154 provider()->OnDidCreateMetricsLog(); | |
155 RunTasks(); | |
156 { | |
157 HistogramFlattenerDeltaRecorder flattener; | |
158 HistogramSnapshotManager snapshot_manager(&flattener); | |
159 snapshot_manager.StartDeltas(); | |
160 provider()->RecordHistogramSnapshots(&snapshot_manager); | |
161 snapshot_manager.FinishDeltas(); | |
162 EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size()); | |
163 } | |
164 | |
165 // Update the time-stamp of the file to indicate that it is "new" and | |
166 // must be recorded. | |
167 { | |
168 File touch(metrics_file(), File::FLAG_OPEN | File::FLAG_WRITE); | |
169 ASSERT_TRUE(touch.IsValid()); | |
170 Time next = Time::Now() + TimeDelta::FromSeconds(1); | |
171 touch.SetTimes(next, next); | |
172 } | |
173 | |
174 // This run should again have "new" histograms. | |
175 provider()->OnDidCreateMetricsLog(); | |
176 RunTasks(); | |
177 { | |
178 HistogramFlattenerDeltaRecorder flattener; | |
179 HistogramSnapshotManager snapshot_manager(&flattener); | |
180 snapshot_manager.StartDeltas(); | |
181 provider()->RecordHistogramSnapshots(&snapshot_manager); | |
182 snapshot_manager.FinishDeltas(); | |
183 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); | |
184 } | |
185 } | |
186 | |
187 } // namespace metrics | |
OLD | NEW |