| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/histogram_snapshot_manager.h" | 5 #include "base/metrics/histogram_snapshot_manager.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/metrics/histogram_delta_serialization.h" | 11 #include "base/metrics/histogram_delta_serialization.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/metrics/sample_vector.h" |
| 13 #include "base/metrics/statistics_recorder.h" | 14 #include "base/metrics/statistics_recorder.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 | 18 |
| 18 class HistogramFlattenerDeltaRecorder : public HistogramFlattener { | 19 class HistogramFlattenerDeltaRecorder : public HistogramFlattener { |
| 19 public: | 20 public: |
| 20 HistogramFlattenerDeltaRecorder() {} | 21 HistogramFlattenerDeltaRecorder() {} |
| 21 | 22 |
| 22 void RecordDelta(const HistogramBase& histogram, | 23 void RecordDelta(const HistogramBase& histogram, |
| 23 const HistogramSamples& snapshot) override { | 24 const HistogramSamples& snapshot) override { |
| 24 recorded_delta_histogram_names_.push_back(histogram.histogram_name()); | 25 recorded_delta_histogram_names_.push_back(histogram.histogram_name()); |
| 26 ASSERT_EQ(recorded_delta_histogram_sum_.end(), |
| 27 recorded_delta_histogram_sum_.find( |
| 28 histogram.histogram_name())); |
| 29 // Keep pointer to snapshot for testing. This really isn't ideal but the |
| 30 // snapshot-manager keeps the snapshot alive until it's "forgotten". |
| 31 recorded_delta_histogram_sum_[histogram.histogram_name()] = snapshot.sum(); |
| 25 } | 32 } |
| 26 | 33 |
| 27 void InconsistencyDetected(HistogramBase::Inconsistency problem) override { | 34 void InconsistencyDetected(HistogramBase::Inconsistency problem) override { |
| 28 ASSERT_TRUE(false); | 35 ASSERT_TRUE(false); |
| 29 } | 36 } |
| 30 | 37 |
| 31 void UniqueInconsistencyDetected( | 38 void UniqueInconsistencyDetected( |
| 32 HistogramBase::Inconsistency problem) override { | 39 HistogramBase::Inconsistency problem) override { |
| 33 ASSERT_TRUE(false); | 40 ASSERT_TRUE(false); |
| 34 } | 41 } |
| 35 | 42 |
| 36 void InconsistencyDetectedInLoggedCount(int amount) override { | 43 void InconsistencyDetectedInLoggedCount(int amount) override { |
| 37 ASSERT_TRUE(false); | 44 ASSERT_TRUE(false); |
| 38 } | 45 } |
| 39 | 46 |
| 47 void Reset() { |
| 48 recorded_delta_histogram_names_.clear(); |
| 49 recorded_delta_histogram_sum_.clear(); |
| 50 } |
| 51 |
| 40 std::vector<std::string> GetRecordedDeltaHistogramNames() { | 52 std::vector<std::string> GetRecordedDeltaHistogramNames() { |
| 41 return recorded_delta_histogram_names_; | 53 return recorded_delta_histogram_names_; |
| 42 } | 54 } |
| 43 | 55 |
| 56 int64_t GetRecordedDeltaHistogramSum( |
| 57 const std::string& name) { |
| 58 EXPECT_NE(recorded_delta_histogram_sum_.end(), |
| 59 recorded_delta_histogram_sum_.find(name)); |
| 60 return recorded_delta_histogram_sum_[name]; |
| 61 } |
| 62 |
| 44 private: | 63 private: |
| 45 std::vector<std::string> recorded_delta_histogram_names_; | 64 std::vector<std::string> recorded_delta_histogram_names_; |
| 65 std::map<std::string, int64_t> recorded_delta_histogram_sum_; |
| 46 | 66 |
| 47 DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder); | 67 DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder); |
| 48 }; | 68 }; |
| 49 | 69 |
| 50 class HistogramSnapshotManagerTest : public testing::Test { | 70 class HistogramSnapshotManagerTest : public testing::Test { |
| 51 protected: | 71 protected: |
| 52 HistogramSnapshotManagerTest() | 72 HistogramSnapshotManagerTest() |
| 53 : histogram_snapshot_manager_(&histogram_flattener_delta_recorder_) {} | 73 : histogram_snapshot_manager_(&histogram_flattener_delta_recorder_) {} |
| 54 | 74 |
| 55 ~HistogramSnapshotManagerTest() override {} | 75 ~HistogramSnapshotManagerTest() override {} |
| 56 | 76 |
| 57 StatisticsRecorder statistics_recorder_; | 77 StatisticsRecorder statistics_recorder_; |
| 58 HistogramFlattenerDeltaRecorder histogram_flattener_delta_recorder_; | 78 HistogramFlattenerDeltaRecorder histogram_flattener_delta_recorder_; |
| 59 HistogramSnapshotManager histogram_snapshot_manager_; | 79 HistogramSnapshotManager histogram_snapshot_manager_; |
| 60 }; | 80 }; |
| 61 | 81 |
| 62 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasNoFlagsFilter) { | 82 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasNoFlagsFilter) { |
| 63 // kNoFlags filter should record all histograms. | 83 // kNoFlags filter should record all histograms. |
| 64 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 2); | 84 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4); |
| 65 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); | 85 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); |
| 66 | 86 |
| 67 histogram_snapshot_manager_.PrepareDeltas( | 87 histogram_snapshot_manager_.PrepareDeltas( |
| 68 StatisticsRecorder::begin(false), StatisticsRecorder::end(), | 88 StatisticsRecorder::begin(false), StatisticsRecorder::end(), |
| 69 HistogramBase::kNoFlags, HistogramBase::kNoFlags); | 89 HistogramBase::kNoFlags, HistogramBase::kNoFlags); |
| 70 | 90 |
| 71 const std::vector<std::string>& histograms = | 91 const std::vector<std::string>& histograms = |
| 72 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); | 92 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); |
| 73 EXPECT_EQ(2U, histograms.size()); | 93 EXPECT_EQ(2U, histograms.size()); |
| 74 EXPECT_EQ("UmaHistogram", histograms[0]); | 94 EXPECT_EQ("UmaHistogram", histograms[0]); |
| 75 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); | 95 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); |
| 76 } | 96 } |
| 77 | 97 |
| 78 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasUmaHistogramFlagFilter) { | 98 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasUmaHistogramFlagFilter) { |
| 79 // Note that kUmaStabilityHistogramFlag includes kUmaTargetedHistogramFlag. | 99 // Note that kUmaStabilityHistogramFlag includes kUmaTargetedHistogramFlag. |
| 80 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 2); | 100 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4); |
| 81 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); | 101 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); |
| 82 | 102 |
| 83 histogram_snapshot_manager_.PrepareDeltas( | 103 histogram_snapshot_manager_.PrepareDeltas( |
| 84 StatisticsRecorder::begin(false), StatisticsRecorder::end(), | 104 StatisticsRecorder::begin(false), StatisticsRecorder::end(), |
| 85 HistogramBase::kNoFlags, HistogramBase::kUmaTargetedHistogramFlag); | 105 HistogramBase::kNoFlags, HistogramBase::kUmaTargetedHistogramFlag); |
| 86 | 106 |
| 87 const std::vector<std::string>& histograms = | 107 const std::vector<std::string>& histograms = |
| 88 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); | 108 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); |
| 89 EXPECT_EQ(2U, histograms.size()); | 109 EXPECT_EQ(2U, histograms.size()); |
| 90 EXPECT_EQ("UmaHistogram", histograms[0]); | 110 EXPECT_EQ("UmaHistogram", histograms[0]); |
| 91 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); | 111 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); |
| 92 } | 112 } |
| 93 | 113 |
| 94 TEST_F(HistogramSnapshotManagerTest, | 114 TEST_F(HistogramSnapshotManagerTest, |
| 95 PrepareDeltasUmaStabilityHistogramFlagFilter) { | 115 PrepareDeltasUmaStabilityHistogramFlagFilter) { |
| 96 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 2); | 116 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4); |
| 97 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); | 117 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); |
| 98 | 118 |
| 99 histogram_snapshot_manager_.PrepareDeltas( | 119 histogram_snapshot_manager_.PrepareDeltas( |
| 100 StatisticsRecorder::begin(false), StatisticsRecorder::end(), | 120 StatisticsRecorder::begin(false), StatisticsRecorder::end(), |
| 101 HistogramBase::kNoFlags, HistogramBase::kUmaStabilityHistogramFlag); | 121 HistogramBase::kNoFlags, HistogramBase::kUmaStabilityHistogramFlag); |
| 102 | 122 |
| 103 const std::vector<std::string>& histograms = | 123 const std::vector<std::string>& histograms = |
| 104 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); | 124 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); |
| 105 EXPECT_EQ(1U, histograms.size()); | 125 EXPECT_EQ(1U, histograms.size()); |
| 106 EXPECT_EQ("UmaStabilityHistogram", histograms[0]); | 126 EXPECT_EQ("UmaStabilityHistogram", histograms[0]); |
| 107 } | 127 } |
| 108 | 128 |
| 129 TEST_F(HistogramSnapshotManagerTest, CheckMerge) { |
| 130 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4); |
| 131 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); |
| 132 |
| 133 base::HistogramBase* h1 = base::LinearHistogram::FactoryGet( |
| 134 "UmaHistogram", 1, 4, 5, 0); |
| 135 ASSERT_TRUE(h1); |
| 136 base::HistogramBase* h2 = base::LinearHistogram::FactoryGet( |
| 137 "UmaStabilityHistogram", 1, 2, 3, 0); |
| 138 ASSERT_TRUE(h2); |
| 139 |
| 140 histogram_snapshot_manager_.StartDeltas(); |
| 141 histogram_snapshot_manager_.PrepareDelta(h1); |
| 142 histogram_snapshot_manager_.PrepareDelta(h1); // Delta will be zero. |
| 143 histogram_snapshot_manager_.PrepareDelta(h2); |
| 144 h1->Add(2); |
| 145 h2->Add(1); |
| 146 histogram_snapshot_manager_.PrepareDelta(h2); |
| 147 histogram_snapshot_manager_.PrepareDelta(h1); |
| 148 histogram_snapshot_manager_.FinishDeltas(); |
| 149 { |
| 150 const std::vector<std::string>& histograms = |
| 151 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); |
| 152 EXPECT_EQ(2U, histograms.size()); |
| 153 EXPECT_EQ(3, histogram_flattener_delta_recorder_. |
| 154 GetRecordedDeltaHistogramSum("UmaHistogram")); |
| 155 EXPECT_EQ(2, histogram_flattener_delta_recorder_. |
| 156 GetRecordedDeltaHistogramSum("UmaStabilityHistogram")); |
| 157 } |
| 158 } |
| 159 |
| 109 } // namespace base | 160 } // namespace base |
| OLD | NEW |