Chromium Code Reviews| 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/test/histogram_tester.h" | 5 #include "base/test/histogram_tester.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <vector> | |
| 9 | |
| 7 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 8 #include "base/metrics/histogram_samples.h" | 11 #include "base/metrics/histogram_samples.h" |
| 9 #include "base/metrics/statistics_recorder.h" | 12 #include "base/metrics/statistics_recorder.h" |
| 10 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 17 |
| 13 namespace base { | 18 namespace base { |
| 14 | 19 |
| 15 HistogramTester::HistogramTester() { | 20 HistogramTester::HistogramTester() { |
| 16 StatisticsRecorder::Initialize(); // Safe to call multiple times. | 21 StatisticsRecorder::Initialize(); // Safe to call multiple times. |
| 17 | 22 |
| 18 // Record any histogram data that exists when the object is created so it can | 23 // Record any histogram data that exists when the object is created so it can |
| 19 // be subtracted later. | 24 // be subtracted later. |
| 20 StatisticsRecorder::Histograms histograms; | 25 StatisticsRecorder::Histograms histograms; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 base::StatisticsRecorder::FindHistogram(name); | 71 base::StatisticsRecorder::FindHistogram(name); |
| 67 if (histogram) { | 72 if (histogram) { |
| 68 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | 73 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
| 69 CheckTotalCount(name, count, *samples); | 74 CheckTotalCount(name, count, *samples); |
| 70 } else { | 75 } else { |
| 71 // No histogram means there were zero samples. | 76 // No histogram means there were zero samples. |
| 72 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; | 77 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; |
| 73 } | 78 } |
| 74 } | 79 } |
| 75 | 80 |
| 81 std::string HistogramTester::GetTotalCountsForQuery( | |
| 82 const std::string& query) const { | |
| 83 EXPECT_TRUE(query.find('.') != std::string::npos) | |
| 84 << "|query| ought to contain at least one period, to avoid matching too" | |
| 85 << " many histograms."; | |
|
Ilya Sherman
2015/06/23 00:57:40
nit: Perhaps output the query?
| |
| 86 | |
| 87 std::vector<std::string> result; | |
| 88 StatisticsRecorder::Histograms histograms; | |
| 89 StatisticsRecorder::GetSnapshot(query, &histograms); | |
| 90 for (base::HistogramBase* histogram : histograms) { | |
| 91 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | |
| 92 auto old_histogram = histograms_snapshot_.find(histogram->histogram_name()); | |
| 93 if (old_histogram != histograms_snapshot_.end()) | |
| 94 samples->Subtract(*old_histogram->second); | |
| 95 | |
| 96 if (histogram->GetHistogramType() == LINEAR_HISTOGRAM || | |
| 97 histogram->GetHistogramType() == BOOLEAN_HISTOGRAM) { | |
| 98 for (scoped_ptr<SampleCountIterator> bucket_it = samples->Iterator(); | |
| 99 !bucket_it->Done(); bucket_it->Next()) { | |
| 100 HistogramBase::Sample min, max; | |
| 101 HistogramBase::Count count; | |
| 102 bucket_it->Get(&min, &max, &count); | |
| 103 if (count != 0) { | |
| 104 result.push_back(base::StringPrintf( | |
| 105 "%s[%d]=%d", histogram->histogram_name().c_str(), min, count)); | |
| 106 } | |
| 107 } | |
| 108 } else { | |
| 109 HistogramBase::Count count = samples->TotalCount(); | |
| 110 if (count != 0) { | |
| 111 result.push_back(base::StringPrintf( | |
| 112 "%s=%d", histogram->histogram_name().c_str(), count)); | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 std::sort(result.begin(), result.end()); | |
| 117 return JoinString(result, "\n"); | |
| 118 } | |
| 119 | |
| 76 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation( | 120 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation( |
| 77 const std::string& histogram_name) { | 121 const std::string& histogram_name) { |
| 78 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); | 122 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); |
| 79 if (!histogram) | 123 if (!histogram) |
| 80 return scoped_ptr<HistogramSamples>(); | 124 return scoped_ptr<HistogramSamples>(); |
| 81 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); | 125 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); |
| 82 HistogramSamples* named_original_samples = | 126 HistogramSamples* named_original_samples = |
| 83 histograms_snapshot_[histogram_name]; | 127 histograms_snapshot_[histogram_name]; |
| 84 if (named_original_samples) | 128 if (named_original_samples) |
| 85 named_samples->Subtract(*named_original_samples); | 129 named_samples->Subtract(*named_original_samples); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 114 if (histogram_data != histograms_snapshot_.end()) | 158 if (histogram_data != histograms_snapshot_.end()) |
| 115 actual_count -= histogram_data->second->TotalCount(); | 159 actual_count -= histogram_data->second->TotalCount(); |
| 116 | 160 |
| 117 EXPECT_EQ(expected_count, actual_count) | 161 EXPECT_EQ(expected_count, actual_count) |
| 118 << "Histogram \"" << name | 162 << "Histogram \"" << name |
| 119 << "\" does not have the right total number of samples (" | 163 << "\" does not have the right total number of samples (" |
| 120 << expected_count << "). It has (" << actual_count << ")."; | 164 << expected_count << "). It has (" << actual_count << ")."; |
| 121 } | 165 } |
| 122 | 166 |
| 123 } // namespace base | 167 } // namespace base |
| OLD | NEW |