| Index: chrome/browser/metrics/subprocess_metrics_provider_unittest.cc
|
| diff --git a/chrome/browser/metrics/subprocess_metrics_provider_unittest.cc b/chrome/browser/metrics/subprocess_metrics_provider_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..373ce98f47ce7ff080ce5e1a4650d192a2a8ded4
|
| --- /dev/null
|
| +++ b/chrome/browser/metrics/subprocess_metrics_provider_unittest.cc
|
| @@ -0,0 +1,132 @@
|
| +// 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 "chrome/browser/metrics/subprocess_metrics_provider.h"
|
| +
|
| +#include "base/memory/shared_memory.h"
|
| +#include "base/metrics/histogram.h"
|
| +#include "base/metrics/histogram_flattener.h"
|
| +#include "base/metrics/histogram_snapshot_manager.h"
|
| +#include "base/metrics/persistent_histogram_allocator.h"
|
| +#include "base/metrics/persistent_memory_allocator.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +const uint32_t TEST_MEMORY_SIZE = 64 << 10; // 64 KiB
|
| +} // namespace
|
| +
|
| +class HistogramFlattenerDeltaRecorder : public base::HistogramFlattener {
|
| + public:
|
| + HistogramFlattenerDeltaRecorder() {}
|
| +
|
| + void RecordDelta(const base::HistogramBase& histogram,
|
| + const base::HistogramSamples& snapshot) override {
|
| + recorded_delta_histogram_names_.push_back(histogram.histogram_name());
|
| + }
|
| +
|
| + void InconsistencyDetected(
|
| + base::HistogramBase::Inconsistency problem) override {
|
| + ASSERT_TRUE(false);
|
| + }
|
| +
|
| + void UniqueInconsistencyDetected(
|
| + base::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 SubprocessMetricsProviderTest : public testing::Test {
|
| + protected:
|
| + SubprocessMetricsProviderTest() {
|
| + // Get this first so it isn't created inside a persistent allocator.
|
| + base::PersistentHistogramAllocator::GetCreateHistogramResultHistogram();
|
| + }
|
| +
|
| + void TearDown() override {
|
| + base::PersistentHistogramAllocator::ReleaseGlobalAllocatorForTesting();
|
| + }
|
| +
|
| + SubprocessMetricsProvider* provider() {
|
| + if (!provider_)
|
| + provider_.reset(new SubprocessMetricsProvider());
|
| + return provider_.get();
|
| + }
|
| +
|
| + private:
|
| + scoped_ptr<SubprocessMetricsProvider> provider_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(SubprocessMetricsProviderTest);
|
| +};
|
| +
|
| +TEST_F(SubprocessMetricsProviderTest, SnapshotMetrics) {
|
| + scoped_ptr<base::SharedMemory> shmem1(new base::SharedMemory());
|
| + base::SharedMemoryHandle memory_handle;
|
| + ASSERT_TRUE(shmem1->CreateAndMapAnonymous(TEST_MEMORY_SIZE));
|
| + ASSERT_TRUE(shmem1->ShareToProcess(base::GetCurrentProcessHandle(),
|
| + &memory_handle));
|
| +
|
| + base::PersistentHistogramAllocator::CreateGlobalAllocatorOnSharedMemory(
|
| + std::move(shmem1), TEST_MEMORY_SIZE, 0, "");
|
| + 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);
|
| +
|
| + scoped_ptr<base::SharedMemory> shmem2(
|
| + new base::SharedMemory(memory_handle, /*readonly=*/false));
|
| + ASSERT_TRUE(shmem2->Map(TEST_MEMORY_SIZE));
|
| +
|
| + scoped_ptr<base::SharedPersistentMemoryAllocator> allocator(
|
| + new base::SharedPersistentMemoryAllocator(
|
| + std::move(shmem2), 0, "", false));
|
| + provider()->RegisterSubprocessAllocator(123, make_scoped_ptr(
|
| + new base::PersistentHistogramAllocator(std::move(allocator))));
|
| +
|
| + // Recording should find the two histograms created in persistent memory.
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + snapshot_manager.StartDeltas();
|
| + provider()->RecordHistogramSnapshots(&snapshot_manager);
|
| + snapshot_manager.FinishDeltas();
|
| + EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size());
|
| + }
|
| +
|
| + // A second run should have nothing to producte except one internal metric.
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + snapshot_manager.StartDeltas();
|
| + provider()->RecordHistogramSnapshots(&snapshot_manager);
|
| + snapshot_manager.FinishDeltas();
|
| + EXPECT_EQ(1U, flattener.GetRecordedDeltaHistogramNames().size());
|
| + }
|
| +
|
| + // Create a new histogram and update existing ones. Should now report 4 items.
|
| + base::HistogramBase* baz = base::Histogram::FactoryGet("baz", 1, 100, 10, 0);
|
| + baz->Add(1969);
|
| + foo->Add(10);
|
| + bar->Add(20);
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + snapshot_manager.StartDeltas();
|
| + provider()->RecordHistogramSnapshots(&snapshot_manager);
|
| + snapshot_manager.FinishDeltas();
|
| + EXPECT_EQ(4U, flattener.GetRecordedDeltaHistogramNames().size());
|
| + }
|
| +}
|
|
|