| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/metrics/histogram_base.h" | 8 #include "base/metrics/histogram_base.h" |
| 9 #include "base/metrics/sparse_histogram.h" | 9 #include "base/metrics/sparse_histogram.h" |
| 10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
| 11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 class HistogramBaseTest : public testing::Test { | 16 class HistogramBaseTest : public testing::Test { |
| 17 protected: | 17 protected: |
| 18 HistogramBaseTest() { | 18 HistogramBaseTest() { |
| 19 // Each test will have a clean state (no Histogram / BucketRanges | 19 // Each test will have a clean state (no Histogram / BucketRanges |
| 20 // registered). | 20 // registered). |
| 21 statistics_recorder_ = NULL; | |
| 22 ResetStatisticsRecorder(); | 21 ResetStatisticsRecorder(); |
| 23 } | 22 } |
| 24 | 23 |
| 25 ~HistogramBaseTest() override { delete statistics_recorder_; } | 24 ~HistogramBaseTest() override { |
| 25 HistogramBase::report_histogram_ = nullptr; |
| 26 } |
| 26 | 27 |
| 27 void ResetStatisticsRecorder() { | 28 void ResetStatisticsRecorder() { |
| 28 delete statistics_recorder_; | 29 // It is necessary to fully destruct any existing StatisticsRecorder |
| 29 statistics_recorder_ = new StatisticsRecorder(); | 30 // before creating a new one. |
| 31 statistics_recorder_.reset(); |
| 32 statistics_recorder_.reset(new StatisticsRecorder()); |
| 33 } |
| 34 |
| 35 HistogramBase* GetCreationReportHistogram(const std::string& name) { |
| 36 HistogramBase::EnableActivityReportHistogram(name); |
| 37 return HistogramBase::report_histogram_; |
| 30 } | 38 } |
| 31 | 39 |
| 32 private: | 40 private: |
| 33 StatisticsRecorder* statistics_recorder_; | 41 scoped_ptr<StatisticsRecorder> statistics_recorder_; |
| 42 |
| 43 DISALLOW_COPY_AND_ASSIGN(HistogramBaseTest); |
| 34 }; | 44 }; |
| 35 | 45 |
| 36 TEST_F(HistogramBaseTest, DeserializeHistogram) { | 46 TEST_F(HistogramBaseTest, DeserializeHistogram) { |
| 37 HistogramBase* histogram = Histogram::FactoryGet( | 47 HistogramBase* histogram = Histogram::FactoryGet( |
| 38 "TestHistogram", 1, 1000, 10, | 48 "TestHistogram", 1, 1000, 10, |
| 39 (HistogramBase::kUmaTargetedHistogramFlag | | 49 (HistogramBase::kUmaTargetedHistogramFlag | |
| 40 HistogramBase::kIPCSerializationSourceFlag)); | 50 HistogramBase::kIPCSerializationSourceFlag)); |
| 41 | 51 |
| 42 Pickle pickle; | 52 Pickle pickle; |
| 43 ASSERT_TRUE(histogram->SerializeInfo(&pickle)); | 53 ASSERT_TRUE(histogram->SerializeInfo(&pickle)); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 ResetStatisticsRecorder(); | 155 ResetStatisticsRecorder(); |
| 146 | 156 |
| 147 PickleIterator iter2(pickle); | 157 PickleIterator iter2(pickle); |
| 148 deserialized = DeserializeHistogramInfo(&iter2); | 158 deserialized = DeserializeHistogramInfo(&iter2); |
| 149 EXPECT_TRUE(deserialized); | 159 EXPECT_TRUE(deserialized); |
| 150 EXPECT_NE(histogram, deserialized); | 160 EXPECT_NE(histogram, deserialized); |
| 151 EXPECT_EQ("TestHistogram", deserialized->histogram_name()); | 161 EXPECT_EQ("TestHistogram", deserialized->histogram_name()); |
| 152 EXPECT_EQ(0, deserialized->flags()); | 162 EXPECT_EQ(0, deserialized->flags()); |
| 153 } | 163 } |
| 154 | 164 |
| 165 TEST_F(HistogramBaseTest, CreationReportHistogram) { |
| 166 // Enabled creation report. Itself is not included in the report. |
| 167 HistogramBase* report = GetCreationReportHistogram("CreationReportTest"); |
| 168 ASSERT_TRUE(report); |
| 169 |
| 170 std::vector<HistogramBase::Sample> ranges; |
| 171 ranges.push_back(1); |
| 172 ranges.push_back(2); |
| 173 ranges.push_back(4); |
| 174 ranges.push_back(8); |
| 175 ranges.push_back(10); |
| 176 |
| 177 // Create all histogram types and verify counts. |
| 178 Histogram::FactoryGet("CRH-Histogram", 1, 10, 5, 0); |
| 179 LinearHistogram::FactoryGet("CRH-Linear", 1, 10, 5, 0); |
| 180 BooleanHistogram::FactoryGet("CRH-Boolean", 0); |
| 181 CustomHistogram::FactoryGet("CRH-Custom", ranges, 0); |
| 182 SparseHistogram::FactoryGet("CRH-Sparse", 0); |
| 183 |
| 184 scoped_ptr<HistogramSamples> samples = report->SnapshotSamples(); |
| 185 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_CREATED)); |
| 186 EXPECT_EQ(5, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_CREATED)); |
| 187 EXPECT_EQ(0, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_LOOKUP)); |
| 188 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_LOGARITHMIC)); |
| 189 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_LINEAR)); |
| 190 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_BOOLEAN)); |
| 191 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_CUSTOM)); |
| 192 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_TYPE_SPARSE)); |
| 193 |
| 194 // Create all flag types and verify counts. |
| 195 Histogram::FactoryGet("CRH-Histogram-UMA-Targeted", 1, 10, 5, |
| 196 HistogramBase::kUmaTargetedHistogramFlag); |
| 197 Histogram::FactoryGet("CRH-Histogram-UMA-Stability", 1, 10, 5, |
| 198 HistogramBase::kUmaStabilityHistogramFlag); |
| 199 SparseHistogram::FactoryGet("CRH-Sparse-UMA-Targeted", |
| 200 HistogramBase::kUmaTargetedHistogramFlag); |
| 201 SparseHistogram::FactoryGet("CRH-Sparse-UMA-Stability", |
| 202 HistogramBase::kUmaStabilityHistogramFlag); |
| 203 samples = report->SnapshotSamples(); |
| 204 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_CREATED)); |
| 205 EXPECT_EQ(9, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_CREATED)); |
| 206 EXPECT_EQ(0, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_LOOKUP)); |
| 207 EXPECT_EQ(2, samples->GetCount(HISTOGRAM_REPORT_FLAG_UMA_TARGETED)); |
| 208 EXPECT_EQ(2, samples->GetCount(HISTOGRAM_REPORT_FLAG_UMA_STABILITY)); |
| 209 |
| 210 // Do lookup of existing histograms and verify counts. |
| 211 Histogram::FactoryGet("CRH-Histogram", 1, 10, 5, 0); |
| 212 LinearHistogram::FactoryGet("CRH-Linear", 1, 10, 5, 0); |
| 213 BooleanHistogram::FactoryGet("CRH-Boolean", 0); |
| 214 CustomHistogram::FactoryGet("CRH-Custom", ranges, 0); |
| 215 SparseHistogram::FactoryGet("CRH-Sparse", 0); |
| 216 samples = report->SnapshotSamples(); |
| 217 EXPECT_EQ(1, samples->GetCount(HISTOGRAM_REPORT_CREATED)); |
| 218 EXPECT_EQ(9, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_CREATED)); |
| 219 EXPECT_EQ(5, samples->GetCount(HISTOGRAM_REPORT_HISTOGRAM_LOOKUP)); |
| 220 } |
| 221 |
| 155 } // namespace base | 222 } // namespace base |
| OLD | NEW |