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 |
| 681 DISALLOW_COPY_AND_ASSIGN(TestHistogramProvider); |
| 682 }; |
| 683 |
| 684 TEST_P(StatisticsRecorderTest, ImportHistogramsTest) { |
| 685 // Create a second SR to create some histograms for later import. |
| 686 std::unique_ptr<StatisticsRecorder> temp_sr = |
| 687 StatisticsRecorder::CreateTemporaryForTesting(); |
| 688 |
| 689 // Extract any existing global allocator so a new one can be created. |
| 690 std::unique_ptr<GlobalHistogramAllocator> old_allocator = |
| 691 GlobalHistogramAllocator::ReleaseForTesting(); |
| 692 |
| 693 // Create a histogram inside a new allocator for testing. |
| 694 GlobalHistogramAllocator::CreateWithLocalMemory(kAllocatorMemorySize, 0, ""); |
| 695 HistogramBase* histogram = LinearHistogram::FactoryGet("Foo", 1, 10, 11, 0); |
| 696 histogram->Add(3); |
| 697 |
| 698 // Undo back to the starting point. |
| 699 std::unique_ptr<GlobalHistogramAllocator> new_allocator = |
| 700 GlobalHistogramAllocator::ReleaseForTesting(); |
| 701 GlobalHistogramAllocator::Set(std::move(old_allocator)); |
| 702 temp_sr.reset(); |
| 703 |
| 704 // Create a provider that can supply histograms to the current SR. |
| 705 TestHistogramProvider provider(std::move(new_allocator)); |
| 706 |
| 707 // Verify that the created histogram is no longer known. |
| 708 ASSERT_FALSE(StatisticsRecorder::FindHistogram(histogram->histogram_name())); |
| 709 |
| 710 // Now test that it merges. |
| 711 StatisticsRecorder::ImportProvidedHistograms(); |
| 712 HistogramBase* found = |
| 713 StatisticsRecorder::FindHistogram(histogram->histogram_name()); |
| 714 ASSERT_TRUE(found); |
| 715 EXPECT_NE(histogram, found); |
| 716 std::unique_ptr<HistogramSamples> snapshot = found->SnapshotSamples(); |
| 717 EXPECT_EQ(1, snapshot->TotalCount()); |
| 718 EXPECT_EQ(1, snapshot->GetCount(3)); |
| 719 |
| 720 // Finally, verify that updates can also be merged. |
| 721 histogram->Add(3); |
| 722 histogram->Add(5); |
| 723 StatisticsRecorder::ImportProvidedHistograms(); |
| 724 snapshot = found->SnapshotSamples(); |
| 725 EXPECT_EQ(3, snapshot->TotalCount()); |
| 726 EXPECT_EQ(2, snapshot->GetCount(3)); |
| 727 EXPECT_EQ(1, snapshot->GetCount(5)); |
| 728 } |
| 729 |
659 } // namespace base | 730 } // namespace base |
OLD | NEW |