| Index: components/metrics/file_metrics_provider_unittest.cc
|
| diff --git a/components/metrics/file_metrics_provider_unittest.cc b/components/metrics/file_metrics_provider_unittest.cc
|
| index 3f5a016c2d1a1e1ba366b89d252f1521ffeb36c6..8450251df16a1e6937a642dc1debc6c5fa604185 100644
|
| --- a/components/metrics/file_metrics_provider_unittest.cc
|
| +++ b/components/metrics/file_metrics_provider_unittest.cc
|
| @@ -12,7 +12,9 @@
|
| #include "base/metrics/histogram_snapshot_manager.h"
|
| #include "base/metrics/persistent_histogram_allocator.h"
|
| #include "base/metrics/persistent_memory_allocator.h"
|
| +#include "base/metrics/sparse_histogram.h"
|
| #include "base/metrics/statistics_recorder.h"
|
| +#include "base/strings/stringprintf.h"
|
| #include "base/test/test_simple_task_runner.h"
|
| #include "base/thread_task_runner_handle.h"
|
| #include "components/metrics/metrics_pref_names.h"
|
| @@ -86,51 +88,54 @@ class FileMetricsProviderTest : public testing::Test {
|
| task_runner_->RunUntilIdle();
|
| }
|
|
|
| - private:
|
| - scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
|
| - base::ThreadTaskRunnerHandle thread_task_runner_handle_;
|
| -
|
| - base::ScopedTempDir temp_dir_;
|
| - std::unique_ptr<TestingPrefServiceSimple> prefs_;
|
| - std::unique_ptr<FileMetricsProvider> provider_;
|
| -
|
| - DISALLOW_COPY_AND_ASSIGN(FileMetricsProviderTest);
|
| -};
|
| -
|
| -
|
| -TEST_F(FileMetricsProviderTest, AccessMetrics) {
|
| - ASSERT_FALSE(PathExists(metrics_file()));
|
| -
|
| - {
|
| + void CreateMetricsFileWithHistograms(int histogram_count) {
|
| // Get this first so it isn't created inside the persistent allocator.
|
| base::GlobalHistogramAllocator::GetCreateHistogramResultHistogram();
|
|
|
| base::GlobalHistogramAllocator::CreateWithLocalMemory(
|
| 64 << 10, 0, kMetricsName);
|
| - base::HistogramBase* foo =
|
| - base::Histogram::FactoryGet("foo", 1, 100, 10, 0);
|
| - base::HistogramBase* bar =
|
| - base::Histogram::FactoryGet("bar", 1, 100, 10, 0);
|
| - foo->Add(42);
|
| - bar->Add(84);
|
| +
|
| + // Create both sparse and normal histograms in the allocator.
|
| + base::SparseHistogram::FactoryGet("h0", 0)->Add(0);
|
| + for (int i = 1; i < histogram_count; ++i) {
|
| + base::Histogram::FactoryGet(base::StringPrintf("h%d", i), 1, 100, 10, 0)
|
| + ->Add(i);
|
| + }
|
|
|
| std::unique_ptr<base::PersistentHistogramAllocator> histogram_allocator =
|
| base::GlobalHistogramAllocator::ReleaseForTesting();
|
| base::PersistentMemoryAllocator* allocator =
|
| histogram_allocator->memory_allocator();
|
| base::File writer(metrics_file(),
|
| - base::File::FLAG_CREATE | base::File::FLAG_WRITE);
|
| + base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
|
| ASSERT_TRUE(writer.IsValid());
|
| ASSERT_EQ(static_cast<int>(allocator->used()),
|
| writer.Write(0, (const char*)allocator->data(),
|
| allocator->used()));
|
| }
|
|
|
| + private:
|
| + scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
|
| + base::ThreadTaskRunnerHandle thread_task_runner_handle_;
|
| +
|
| + base::ScopedTempDir temp_dir_;
|
| + std::unique_ptr<TestingPrefServiceSimple> prefs_;
|
| + std::unique_ptr<FileMetricsProvider> provider_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(FileMetricsProviderTest);
|
| +};
|
| +
|
| +TEST_F(FileMetricsProviderTest, AccessMetrics) {
|
| + ASSERT_FALSE(PathExists(metrics_file()));
|
| + CreateMetricsFileWithHistograms(2);
|
| +
|
| // Register the file and allow the "checker" task to run.
|
| ASSERT_TRUE(PathExists(metrics_file()));
|
| - provider()->RegisterFile(metrics_file(),
|
| - FileMetricsProvider::FILE_HISTOGRAMS_ATOMIC,
|
| - kMetricsName);
|
| + provider()->RegisterFile(
|
| + metrics_file(),
|
| + FileMetricsProvider::FILE_HISTOGRAMS_ATOMIC,
|
| + FileMetricsProvider::ASSOCIATE_CURRENT_RUN,
|
| + kMetricsName);
|
|
|
| // Record embedded snapshots via snapshot-manager.
|
| provider()->OnDidCreateMetricsLog();
|
| @@ -189,4 +194,50 @@ TEST_F(FileMetricsProviderTest, AccessMetrics) {
|
| }
|
| }
|
|
|
| +TEST_F(FileMetricsProviderTest, AccessInitialMetrics) {
|
| + ASSERT_FALSE(PathExists(metrics_file()));
|
| + CreateMetricsFileWithHistograms(2);
|
| +
|
| + // Register the file and allow the "checker" task to run.
|
| + ASSERT_TRUE(PathExists(metrics_file()));
|
| + provider()->RegisterFile(
|
| + metrics_file(),
|
| + FileMetricsProvider::FILE_HISTOGRAMS_ATOMIC,
|
| + FileMetricsProvider::ASSOCIATE_PREVIOUS_RUN,
|
| + kMetricsName);
|
| +
|
| + // Record embedded snapshots via snapshot-manager.
|
| + provider()->HasInitialStabilityMetrics();
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + snapshot_manager.StartDeltas();
|
| + provider()->RecordInitialHistogramSnapshots(&snapshot_manager);
|
| + snapshot_manager.FinishDeltas();
|
| + EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size());
|
| + }
|
| +
|
| + // Second full run on the same file should produce nothing.
|
| + provider()->OnDidCreateMetricsLog();
|
| + RunTasks();
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + snapshot_manager.StartDeltas();
|
| + provider()->RecordInitialHistogramSnapshots(&snapshot_manager);
|
| + snapshot_manager.FinishDeltas();
|
| + EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size());
|
| + }
|
| +
|
| + // A run for normal histograms should produce nothing.
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + snapshot_manager.StartDeltas();
|
| + provider()->RecordHistogramSnapshots(&snapshot_manager);
|
| + snapshot_manager.FinishDeltas();
|
| + EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size());
|
| + }
|
| +}
|
| +
|
| } // namespace metrics
|
|
|