Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Unified Diff: components/metrics/file_metrics_provider_unittest.cc

Issue 1537743006: Persist setup metrics and have Chrome report them during UMA upload. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shared-histograms
Patch Set: test needs to clear out statistics-recorder before releasing histogram memory Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
new file mode 100644
index 0000000000000000000000000000000000000000..b49c7842189650baa08dbee1f334a20ee6377851
--- /dev/null
+++ b/components/metrics/file_metrics_provider_unittest.cc
@@ -0,0 +1,185 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/metrics/file_metrics_provider.h"
+
+#include "base/files/file_util.h"
+#include "base/files/memory_mapped_file.h"
+#include "base/files/scoped_temp_dir.h"
+#include "base/metrics/histogram.h"
+#include "base/metrics/histogram_flattener.h"
+#include "base/metrics/histogram_persistence.h"
+#include "base/metrics/histogram_snapshot_manager.h"
+#include "base/metrics/persistent_memory_allocator.h"
+#include "base/metrics/statistics_recorder.h"
+#include "base/test/test_simple_task_runner.h"
+#include "base/thread_task_runner_handle.h"
+#include "components/metrics/metrics_pref_names.h"
+#include "components/prefs/pref_registry_simple.h"
+#include "components/prefs/testing_pref_service.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using namespace base;
+
+namespace {
+const char kMetricsName[] = "TestMetrics";
+const char kMetricsFilename[] = "file.metrics";
+} // namespace
+
+namespace metrics {
+
+class HistogramFlattenerDeltaRecorder : public HistogramFlattener {
+ public:
+ HistogramFlattenerDeltaRecorder() {}
+
+ void RecordDelta(const HistogramBase& histogram,
+ const HistogramSamples& snapshot) override {
+ recorded_delta_histogram_names_.push_back(histogram.histogram_name());
+ }
+
+ void InconsistencyDetected(HistogramBase::Inconsistency problem) override {
+ ASSERT_TRUE(false);
+ }
+
+ void UniqueInconsistencyDetected(
+ HistogramBase::Inconsistency problem) override {
+ ASSERT_TRUE(false);
+ }
+
+ void InconsistencyDetectedInLoggedCount(int amount) override {
+ ASSERT_TRUE(false);
+ }
+
+ std::vector<std::string> GetRecordedDeltaHistogramNames() {
+ return recorded_delta_histogram_names_;
+ }
+
+ private:
+ std::vector<std::string> recorded_delta_histogram_names_;
+
+ DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder);
+};
+
+class FileMetricsProviderTest : public testing::Test {
+ protected:
+ FileMetricsProviderTest()
+ : task_runner_(new TestSimpleTaskRunner()),
+ reply_runner_(task_runner_),
+ prefs_(new TestingPrefServiceSimple) {
+ EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
+ FileMetricsProvider::RegisterPrefs(prefs_->registry(), kMetricsName);
+ }
+
+ TestingPrefServiceSimple* prefs() { return prefs_.get(); }
+ FilePath temp_dir() { return temp_dir_.path(); }
+ FilePath metrics_file() {
+ return temp_dir_.path().AppendASCII(kMetricsFilename);
+ }
+
+ FileMetricsProvider* provider() {
+ if (!provider_)
+ provider_.reset(new FileMetricsProvider(task_runner_, prefs()));
+ return provider_.get();
+ }
+
+ void RunTasks() {
+ task_runner_->RunUntilIdle();
+ }
+
+ private:
+ scoped_refptr<TestSimpleTaskRunner> task_runner_;
+ 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.
+
+ ScopedTempDir temp_dir_;
+ scoped_ptr<TestingPrefServiceSimple> prefs_;
+ scoped_ptr<FileMetricsProvider> provider_;
+
+ DISALLOW_COPY_AND_ASSIGN(FileMetricsProviderTest);
+};
+
+
+TEST_F(FileMetricsProviderTest, AccessMetrics) {
+ ASSERT_FALSE(PathExists(metrics_file()));
+
+ {
+ // Get this first so it isn't created inside the persistent allocator.
+ GetCreateHistogramResultHistogram();
+
+ SetPersistentHistogramMemoryAllocator(
+ new LocalPersistentMemoryAllocator(64 << 10, 0, kMetricsName));
+ HistogramBase* foo = Histogram::FactoryGet("foo", 1, 100, 10, 0);
+ HistogramBase* bar = Histogram::FactoryGet("bar", 1, 100, 10, 0);
+ foo->Add(42);
+ bar->Add(84);
+
+ scoped_ptr<PersistentMemoryAllocator> allocator(
+ ReleasePersistentHistogramMemoryAllocatorForTesting());
+ File writer(metrics_file(), File::FLAG_CREATE | File::FLAG_WRITE);
+ 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.
+ writer.Write(0, (const char*)allocator->data(), allocator->used());
+ }
+
+ // Register the file and allow the "checker" task to run.
+ ASSERT_TRUE(PathExists(metrics_file()));
+ provider()->RegisterFile(metrics_file(),
+ FileMetricsProvider::FILE_HISTOGRAMS_ATOMIC,
+ kMetricsName);
+
+ // Record embedded snapshots via snapshot-manager.
+ provider()->OnDidCreateMetricsLog();
+ RunTasks();
+ {
+ HistogramFlattenerDeltaRecorder flattener;
+ HistogramSnapshotManager snapshot_manager(&flattener);
+ snapshot_manager.StartDeltas();
+ provider()->RecordHistogramSnapshots(&snapshot_manager);
+ snapshot_manager.FinishDeltas();
+ EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size());
+ }
+
+ // Make sure a second call to the snapshot-recorder doesn't break anything.
+ {
+ HistogramFlattenerDeltaRecorder flattener;
+ HistogramSnapshotManager snapshot_manager(&flattener);
+ snapshot_manager.StartDeltas();
+ provider()->RecordHistogramSnapshots(&snapshot_manager);
+ snapshot_manager.FinishDeltas();
+ EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size());
+ }
+
+ // Second full run on the same file should produce nothing.
+ provider()->OnDidCreateMetricsLog();
+ RunTasks();
+ {
+ HistogramFlattenerDeltaRecorder flattener;
+ HistogramSnapshotManager snapshot_manager(&flattener);
+ snapshot_manager.StartDeltas();
+ provider()->RecordHistogramSnapshots(&snapshot_manager);
+ snapshot_manager.FinishDeltas();
+ EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size());
+ }
+
+ // Update the time-stamp of the file to indicate that it is "new" and
+ // must be recorded.
+ {
+ File touch(metrics_file(), File::FLAG_OPEN | File::FLAG_WRITE);
+ ASSERT_TRUE(touch.IsValid());
+ Time next = Time::Now() + TimeDelta::FromSeconds(1);
+ touch.SetTimes(next, next);
+ }
+
+ // This run should again have "new" histograms.
+ provider()->OnDidCreateMetricsLog();
+ RunTasks();
+ {
+ HistogramFlattenerDeltaRecorder flattener;
+ HistogramSnapshotManager snapshot_manager(&flattener);
+ snapshot_manager.StartDeltas();
+ provider()->RecordHistogramSnapshots(&snapshot_manager);
+ snapshot_manager.FinishDeltas();
+ EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size());
+ }
+}
+
+} // namespace metrics

Powered by Google App Engine
This is Rietveld 408576698