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