OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/statistics_recorder.h" | 5 #include "base/metrics/statistics_recorder.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/weak_ptr.h" | |
15 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
16 #include "base/metrics/persistent_histogram_allocator.h" | 17 #include "base/metrics/persistent_histogram_allocator.h" |
17 #include "base/metrics/sparse_histogram.h" | 18 #include "base/metrics/sparse_histogram.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // Class to make sure any manipulations we do to the min log level are | 24 // Class to make sure any manipulations we do to the min log level are |
24 // contained (i.e., do not affect other unit tests). | 25 // contained (i.e., do not affect other unit tests). |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
649 } | 650 } |
650 | 651 |
651 TEST_P(StatisticsRecorderTest, LogOnShutdownInitialized) { | 652 TEST_P(StatisticsRecorderTest, LogOnShutdownInitialized) { |
652 UninitializeStatisticsRecorder(); | 653 UninitializeStatisticsRecorder(); |
653 logging::SetMinLogLevel(logging::LOG_VERBOSE); | 654 logging::SetMinLogLevel(logging::LOG_VERBOSE); |
654 InitializeStatisticsRecorder(); | 655 InitializeStatisticsRecorder(); |
655 EXPECT_TRUE(VLOG_IS_ON(1)); | 656 EXPECT_TRUE(VLOG_IS_ON(1)); |
656 EXPECT_TRUE(VLogInitialized()); | 657 EXPECT_TRUE(VLogInitialized()); |
657 } | 658 } |
658 | 659 |
660 class TestHistogramProvider : public StatisticsRecorder::HistogramProvider { | |
661 public: | |
662 TestHistogramProvider(std::unique_ptr<PersistentHistogramAllocator> allocator) | |
663 : allocator_(std::move(allocator)), weak_factory_(this) { | |
664 StatisticsRecorder::RegisterHistogramProvider(weak_factory_.GetWeakPtr()); | |
665 } | |
666 | |
667 void MergeHistogramDeltas() override { | |
668 PersistentHistogramAllocator::Iterator hist_iter(allocator_.get()); | |
669 while (true) { | |
670 std::unique_ptr<base::HistogramBase> histogram = hist_iter.GetNext(); | |
671 if (!histogram) | |
672 break; | |
673 allocator_->MergeHistogramDeltaToStatisticsRecorder(histogram.get()); | |
674 } | |
675 } | |
676 | |
677 private: | |
678 std::unique_ptr<PersistentHistogramAllocator> allocator_; | |
679 WeakPtrFactory<TestHistogramProvider> weak_factory_; | |
680 }; | |
Alexei Svitkine (slow)
2017/01/27 16:22:03
Nit: DISALLOW_COPY_AND_ASSIGN()
bcwhite
2017/01/27 18:05:31
Done.
| |
681 | |
682 TEST_P(StatisticsRecorderTest, ImportHistogramsTest) { | |
683 // Create a second SR to create some histograms for later import. | |
684 std::unique_ptr<StatisticsRecorder> temp_sr = | |
685 StatisticsRecorder::CreateTemporaryForTesting(); | |
686 | |
687 // Extract any existing global allocator so a new one can be created. | |
688 std::unique_ptr<GlobalHistogramAllocator> old_allocator = | |
689 GlobalHistogramAllocator::ReleaseForTesting(); | |
690 | |
691 // Create a histogram inside a new allocator for testing. | |
692 GlobalHistogramAllocator::CreateWithLocalMemory(kAllocatorMemorySize, 0, ""); | |
693 HistogramBase* histogram = LinearHistogram::FactoryGet("Foo", 1, 10, 11, 0); | |
694 histogram->Add(3); | |
695 | |
696 // Undo back to the starting point. | |
697 std::unique_ptr<GlobalHistogramAllocator> new_allocator = | |
698 GlobalHistogramAllocator::ReleaseForTesting(); | |
699 GlobalHistogramAllocator::Set(std::move(old_allocator)); | |
700 temp_sr.reset(); | |
701 | |
702 // Create a provider that can supply histograms to the current SR. | |
703 TestHistogramProvider provider(std::move(new_allocator)); | |
704 | |
705 // Verify that the created histogram is no longer known. | |
706 ASSERT_FALSE(StatisticsRecorder::FindHistogram(histogram->histogram_name())); | |
707 | |
708 // Now test that it merges. | |
709 StatisticsRecorder::ImportProvidedHistograms(); | |
710 HistogramBase* found = | |
711 StatisticsRecorder::FindHistogram(histogram->histogram_name()); | |
712 ASSERT_TRUE(found); | |
713 EXPECT_NE(histogram, found); | |
714 std::unique_ptr<HistogramSamples> snapshot = found->SnapshotSamples(); | |
715 EXPECT_EQ(1, snapshot->TotalCount()); | |
716 EXPECT_EQ(1, snapshot->GetCount(3)); | |
717 | |
718 // Finally, verify that updates can also be merged. | |
719 histogram->Add(3); | |
720 histogram->Add(5); | |
721 StatisticsRecorder::ImportProvidedHistograms(); | |
722 snapshot = found->SnapshotSamples(); | |
723 EXPECT_EQ(3, snapshot->TotalCount()); | |
724 EXPECT_EQ(2, snapshot->GetCount(3)); | |
725 EXPECT_EQ(1, snapshot->GetCount(5)); | |
726 } | |
727 | |
659 } // namespace base | 728 } // namespace base |
OLD | NEW |