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