Chromium Code Reviews| 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; | |
| 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 reply_runner_(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 reply_runner_; | |
|
grt (UTC plus 2)
2016/02/15 15:42:40
nit: call this thread_task_runner_handle_.
bcwhite
2016/02/15 19:22:11
Done.
| |
| 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 EXPECT_TRUE(writer.IsValid()); | |
|
grt (UTC plus 2)
2016/02/15 15:42:40
should this be ASSERT_TRUE? is there any point in
bcwhite
2016/02/15 19:22:11
Done.
| |
| 120 writer.Write(0, (const char*)allocator->data(), allocator->used()); | |
| 121 } | |
| 122 | |
| 123 // Register the file and allow the "checker" task to run. | |
| 124 ASSERT_TRUE(PathExists(metrics_file())); | |
| 125 provider()->RegisterFile(metrics_file(), | |
| 126 FileMetricsProvider::FILE_HISTOGRAMS_ATOMIC, | |
| 127 kMetricsName); | |
| 128 | |
| 129 // Record embedded snapshots via snapshot-manager. | |
| 130 provider()->OnDidCreateMetricsLog(); | |
| 131 RunTasks(); | |
| 132 { | |
| 133 HistogramFlattenerDeltaRecorder flattener; | |
| 134 HistogramSnapshotManager snapshot_manager(&flattener); | |
| 135 snapshot_manager.StartDeltas(); | |
| 136 provider()->RecordHistogramSnapshots(&snapshot_manager); | |
| 137 snapshot_manager.FinishDeltas(); | |
| 138 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); | |
| 139 } | |
| 140 | |
| 141 // Make sure a second call to the snapshot-recorder doesn't break anything. | |
| 142 { | |
| 143 HistogramFlattenerDeltaRecorder flattener; | |
| 144 HistogramSnapshotManager snapshot_manager(&flattener); | |
| 145 snapshot_manager.StartDeltas(); | |
| 146 provider()->RecordHistogramSnapshots(&snapshot_manager); | |
| 147 snapshot_manager.FinishDeltas(); | |
| 148 EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size()); | |
| 149 } | |
| 150 | |
| 151 // Second full run on the same file should produce nothing. | |
| 152 provider()->OnDidCreateMetricsLog(); | |
| 153 RunTasks(); | |
| 154 { | |
| 155 HistogramFlattenerDeltaRecorder flattener; | |
| 156 HistogramSnapshotManager snapshot_manager(&flattener); | |
| 157 snapshot_manager.StartDeltas(); | |
| 158 provider()->RecordHistogramSnapshots(&snapshot_manager); | |
| 159 snapshot_manager.FinishDeltas(); | |
| 160 EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size()); | |
| 161 } | |
| 162 | |
| 163 // Update the time-stamp of the file to indicate that it is "new" and | |
| 164 // must be recorded. | |
| 165 { | |
| 166 File touch(metrics_file(), File::FLAG_OPEN | File::FLAG_WRITE); | |
| 167 ASSERT_TRUE(touch.IsValid()); | |
| 168 Time next = Time::Now() + TimeDelta::FromSeconds(1); | |
| 169 touch.SetTimes(next, next); | |
| 170 } | |
| 171 | |
| 172 // This run should again have "new" histograms. | |
| 173 provider()->OnDidCreateMetricsLog(); | |
| 174 RunTasks(); | |
| 175 { | |
| 176 HistogramFlattenerDeltaRecorder flattener; | |
| 177 HistogramSnapshotManager snapshot_manager(&flattener); | |
| 178 snapshot_manager.StartDeltas(); | |
| 179 provider()->RecordHistogramSnapshots(&snapshot_manager); | |
| 180 snapshot_manager.FinishDeltas(); | |
| 181 EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size()); | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 } // namespace metrics | |
| OLD | NEW |