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" |
| 15 #include "base/stl_util.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 namespace base { | 18 namespace base { |
17 | 19 |
18 class HistogramFlattenerDeltaRecorder : public HistogramFlattener { | 20 class HistogramFlattenerDeltaRecorder : public HistogramFlattener { |
19 public: | 21 public: |
20 HistogramFlattenerDeltaRecorder() {} | 22 HistogramFlattenerDeltaRecorder() {} |
21 | 23 |
22 void RecordDelta(const HistogramBase& histogram, | 24 void RecordDelta(const HistogramBase& histogram, |
23 const HistogramSamples& snapshot) override { | 25 const HistogramSamples& snapshot) override { |
24 recorded_delta_histogram_names_.push_back(histogram.histogram_name()); | 26 recorded_delta_histogram_names_.push_back(histogram.histogram_name()); |
| 27 ASSERT_FALSE(ContainsKey(recorded_delta_histogram_sum_, |
| 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(const std::string& name) { |
| 57 EXPECT_TRUE(ContainsKey(recorded_delta_histogram_sum_, name)); |
| 58 return recorded_delta_histogram_sum_[name]; |
| 59 } |
| 60 |
44 private: | 61 private: |
45 std::vector<std::string> recorded_delta_histogram_names_; | 62 std::vector<std::string> recorded_delta_histogram_names_; |
| 63 std::map<std::string, int64_t> recorded_delta_histogram_sum_; |
46 | 64 |
47 DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder); | 65 DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder); |
48 }; | 66 }; |
49 | 67 |
50 class HistogramSnapshotManagerTest : public testing::Test { | 68 class HistogramSnapshotManagerTest : public testing::Test { |
51 protected: | 69 protected: |
52 HistogramSnapshotManagerTest() | 70 HistogramSnapshotManagerTest() |
53 : histogram_snapshot_manager_(&histogram_flattener_delta_recorder_) {} | 71 : histogram_snapshot_manager_(&histogram_flattener_delta_recorder_) {} |
54 | 72 |
55 ~HistogramSnapshotManagerTest() override {} | 73 ~HistogramSnapshotManagerTest() override {} |
56 | 74 |
57 StatisticsRecorder statistics_recorder_; | 75 StatisticsRecorder statistics_recorder_; |
58 HistogramFlattenerDeltaRecorder histogram_flattener_delta_recorder_; | 76 HistogramFlattenerDeltaRecorder histogram_flattener_delta_recorder_; |
59 HistogramSnapshotManager histogram_snapshot_manager_; | 77 HistogramSnapshotManager histogram_snapshot_manager_; |
60 }; | 78 }; |
61 | 79 |
62 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasNoFlagsFilter) { | 80 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasNoFlagsFilter) { |
63 // kNoFlags filter should record all histograms. | 81 // kNoFlags filter should record all histograms. |
64 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 2); | 82 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4); |
65 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); | 83 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); |
66 | 84 |
67 histogram_snapshot_manager_.PrepareDeltas( | 85 histogram_snapshot_manager_.PrepareDeltas( |
68 StatisticsRecorder::begin(false), StatisticsRecorder::end(), | 86 StatisticsRecorder::begin(false), StatisticsRecorder::end(), |
69 HistogramBase::kNoFlags, HistogramBase::kNoFlags); | 87 HistogramBase::kNoFlags, HistogramBase::kNoFlags); |
70 | 88 |
71 const std::vector<std::string>& histograms = | 89 const std::vector<std::string>& histograms = |
72 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); | 90 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); |
73 EXPECT_EQ(2U, histograms.size()); | 91 EXPECT_EQ(2U, histograms.size()); |
74 EXPECT_EQ("UmaHistogram", histograms[0]); | 92 EXPECT_EQ("UmaHistogram", histograms[0]); |
75 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); | 93 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); |
76 } | 94 } |
77 | 95 |
78 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasUmaHistogramFlagFilter) { | 96 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasUmaHistogramFlagFilter) { |
79 // Note that kUmaStabilityHistogramFlag includes kUmaTargetedHistogramFlag. | 97 // Note that kUmaStabilityHistogramFlag includes kUmaTargetedHistogramFlag. |
80 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 2); | 98 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4); |
81 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); | 99 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); |
82 | 100 |
83 histogram_snapshot_manager_.PrepareDeltas( | 101 histogram_snapshot_manager_.PrepareDeltas( |
84 StatisticsRecorder::begin(false), StatisticsRecorder::end(), | 102 StatisticsRecorder::begin(false), StatisticsRecorder::end(), |
85 HistogramBase::kNoFlags, HistogramBase::kUmaTargetedHistogramFlag); | 103 HistogramBase::kNoFlags, HistogramBase::kUmaTargetedHistogramFlag); |
86 | 104 |
87 const std::vector<std::string>& histograms = | 105 const std::vector<std::string>& histograms = |
88 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); | 106 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); |
89 EXPECT_EQ(2U, histograms.size()); | 107 EXPECT_EQ(2U, histograms.size()); |
90 EXPECT_EQ("UmaHistogram", histograms[0]); | 108 EXPECT_EQ("UmaHistogram", histograms[0]); |
91 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); | 109 EXPECT_EQ("UmaStabilityHistogram", histograms[1]); |
92 } | 110 } |
93 | 111 |
94 TEST_F(HistogramSnapshotManagerTest, | 112 TEST_F(HistogramSnapshotManagerTest, |
95 PrepareDeltasUmaStabilityHistogramFlagFilter) { | 113 PrepareDeltasUmaStabilityHistogramFlagFilter) { |
96 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 2); | 114 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4); |
97 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); | 115 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); |
98 | 116 |
99 histogram_snapshot_manager_.PrepareDeltas( | 117 histogram_snapshot_manager_.PrepareDeltas( |
100 StatisticsRecorder::begin(false), StatisticsRecorder::end(), | 118 StatisticsRecorder::begin(false), StatisticsRecorder::end(), |
101 HistogramBase::kNoFlags, HistogramBase::kUmaStabilityHistogramFlag); | 119 HistogramBase::kNoFlags, HistogramBase::kUmaStabilityHistogramFlag); |
102 | 120 |
103 const std::vector<std::string>& histograms = | 121 const std::vector<std::string>& histograms = |
104 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); | 122 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); |
105 EXPECT_EQ(1U, histograms.size()); | 123 EXPECT_EQ(1U, histograms.size()); |
106 EXPECT_EQ("UmaStabilityHistogram", histograms[0]); | 124 EXPECT_EQ("UmaStabilityHistogram", histograms[0]); |
107 } | 125 } |
108 | 126 |
| 127 TEST_F(HistogramSnapshotManagerTest, CheckMerge) { |
| 128 UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4); |
| 129 UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2); |
| 130 |
| 131 base::HistogramBase* h1 = base::LinearHistogram::FactoryGet( |
| 132 "UmaHistogram", 1, 4, 5, 0); |
| 133 ASSERT_TRUE(h1); |
| 134 base::HistogramBase* h2 = base::LinearHistogram::FactoryGet( |
| 135 "UmaStabilityHistogram", 1, 2, 3, 0); |
| 136 ASSERT_TRUE(h2); |
| 137 |
| 138 histogram_snapshot_manager_.StartDeltas(); |
| 139 histogram_snapshot_manager_.PrepareDelta(h1); |
| 140 histogram_snapshot_manager_.PrepareDelta(h1); // Delta will be zero. |
| 141 histogram_snapshot_manager_.PrepareDelta(h2); |
| 142 h1->Add(2); |
| 143 h2->Add(1); |
| 144 histogram_snapshot_manager_.PrepareDelta(h2); |
| 145 histogram_snapshot_manager_.PrepareDelta(h1); |
| 146 histogram_snapshot_manager_.FinishDeltas(); |
| 147 { |
| 148 const std::vector<std::string> histograms = |
| 149 histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames(); |
| 150 EXPECT_EQ(2U, histograms.size()); |
| 151 EXPECT_EQ(3, histogram_flattener_delta_recorder_. |
| 152 GetRecordedDeltaHistogramSum("UmaHistogram")); |
| 153 EXPECT_EQ(2, histogram_flattener_delta_recorder_. |
| 154 GetRecordedDeltaHistogramSum("UmaStabilityHistogram")); |
| 155 } |
| 156 } |
| 157 |
109 } // namespace base | 158 } // namespace base |
OLD | NEW |