Don't preload rarely seen large images
[chromium-blink-merge.git] / base / test / histogram_tester.cc
blob0eba3a9a27938e6ef7ecd7872f466159284a5717
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
3 // found in the LICENSE file.
5 #include "base/test/histogram_tester.h"
7 #include "base/metrics/histogram.h"
8 #include "base/metrics/histogram_samples.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "base/stl_util.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace base {
15 HistogramTester::HistogramTester() {
16 StatisticsRecorder::Initialize(); // Safe to call multiple times.
18 // Record any histogram data that exists when the object is created so it can
19 // be subtracted later.
20 StatisticsRecorder::Histograms histograms;
21 StatisticsRecorder::GetSnapshot(std::string(), &histograms);
22 for (size_t i = 0; i < histograms.size(); ++i) {
23 histograms_snapshot_[histograms[i]->histogram_name()] =
24 histograms[i]->SnapshotSamples().release();
28 HistogramTester::~HistogramTester() {
29 STLDeleteValues(&histograms_snapshot_);
32 void HistogramTester::ExpectUniqueSample(
33 const std::string& name,
34 base::HistogramBase::Sample sample,
35 base::HistogramBase::Count expected_count) const {
36 base::HistogramBase* histogram =
37 base::StatisticsRecorder::FindHistogram(name);
38 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram)
39 << "Histogram \"" << name << "\" does not exist.";
41 if (histogram) {
42 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
43 CheckBucketCount(name, sample, expected_count, *samples);
44 CheckTotalCount(name, expected_count, *samples);
48 void HistogramTester::ExpectBucketCount(
49 const std::string& name,
50 base::HistogramBase::Sample sample,
51 base::HistogramBase::Count expected_count) const {
52 base::HistogramBase* histogram =
53 base::StatisticsRecorder::FindHistogram(name);
54 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram)
55 << "Histogram \"" << name << "\" does not exist.";
57 if (histogram) {
58 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
59 CheckBucketCount(name, sample, expected_count, *samples);
63 void HistogramTester::ExpectTotalCount(const std::string& name,
64 base::HistogramBase::Count count) const {
65 base::HistogramBase* histogram =
66 base::StatisticsRecorder::FindHistogram(name);
67 if (histogram) {
68 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
69 CheckTotalCount(name, count, *samples);
70 } else {
71 // No histogram means there were zero samples.
72 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist.";
76 std::vector<Bucket> HistogramTester::GetAllSamples(const std::string& name) {
77 std::vector<Bucket> samples;
78 scoped_ptr<HistogramSamples> snapshot =
79 GetHistogramSamplesSinceCreation(name);
80 for (auto it = snapshot->Iterator(); !it->Done(); it->Next()) {
81 HistogramBase::Sample sample;
82 HistogramBase::Count count;
83 it->Get(&sample, nullptr, &count);
84 samples.push_back(Bucket(sample, count));
86 return samples;
89 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation(
90 const std::string& histogram_name) {
91 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name);
92 if (!histogram)
93 return scoped_ptr<HistogramSamples>();
94 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples());
95 HistogramSamples* named_original_samples =
96 histograms_snapshot_[histogram_name];
97 if (named_original_samples)
98 named_samples->Subtract(*named_original_samples);
99 return named_samples.Pass();
102 void HistogramTester::CheckBucketCount(
103 const std::string& name,
104 base::HistogramBase::Sample sample,
105 base::HistogramBase::Count expected_count,
106 const base::HistogramSamples& samples) const {
107 int actual_count = samples.GetCount(sample);
108 std::map<std::string, HistogramSamples*>::const_iterator histogram_data;
109 histogram_data = histograms_snapshot_.find(name);
110 if (histogram_data != histograms_snapshot_.end())
111 actual_count -= histogram_data->second->GetCount(sample);
113 EXPECT_EQ(expected_count, actual_count)
114 << "Histogram \"" << name
115 << "\" does not have the right number of samples (" << expected_count
116 << ") in the expected bucket (" << sample << "). It has (" << actual_count
117 << ").";
120 void HistogramTester::CheckTotalCount(
121 const std::string& name,
122 base::HistogramBase::Count expected_count,
123 const base::HistogramSamples& samples) const {
124 int actual_count = samples.TotalCount();
125 std::map<std::string, HistogramSamples*>::const_iterator histogram_data;
126 histogram_data = histograms_snapshot_.find(name);
127 if (histogram_data != histograms_snapshot_.end())
128 actual_count -= histogram_data->second->TotalCount();
130 EXPECT_EQ(expected_count, actual_count)
131 << "Histogram \"" << name
132 << "\" does not have the right total number of samples ("
133 << expected_count << "). It has (" << actual_count << ").";
136 bool Bucket::operator==(const Bucket& other) const {
137 return min == other.min && count == other.count;
140 void PrintTo(const Bucket& bucket, std::ostream* os) {
141 *os << "Bucket " << bucket.min << ": " << bucket.count;
144 } // namespace base