Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: base/test/histogram_tester.cc

Issue 379283002: Rework UMAHistogramHelper and StatisticsDeltaReader into [Chrome]HistogramTester. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/test/histogram_tester.h ('k') | base/test/histogram_tester_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/test/histogram_tester.h"
6
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"
12
13 namespace base {
14
15 HistogramTester::HistogramTester() {
16 StatisticsRecorder::Initialize(); // Safe to call multiple times.
17
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();
25 }
26 }
27
28 HistogramTester::~HistogramTester() {
29 STLDeleteValues(&histograms_snapshot_);
30 }
31
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.";
40
41 if (histogram) {
42 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
43 CheckBucketCount(name, sample, expected_count, *samples);
44 CheckTotalCount(name, expected_count, *samples);
45 }
46 }
47
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.";
56
57 if (histogram) {
58 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
59 CheckBucketCount(name, sample, expected_count, *samples);
60 }
61 }
62
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.";
73 }
74 }
75
76 scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation(
77 const std::string& histogram_name) {
78 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name);
79 if (!histogram)
80 return scoped_ptr<HistogramSamples>();
81 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples());
82 HistogramSamples* named_original_samples =
83 histograms_snapshot_[histogram_name];
84 if (named_original_samples)
85 named_samples->Subtract(*named_original_samples);
86 return named_samples.Pass();
87 }
88
89 void HistogramTester::CheckBucketCount(
90 const std::string& name,
91 base::HistogramBase::Sample sample,
92 base::HistogramBase::Count expected_count,
93 base::HistogramSamples& samples) const {
94 int actual_count = samples.GetCount(sample);
95 std::map<std::string, HistogramSamples*>::const_iterator histogram_data;
96 histogram_data = histograms_snapshot_.find(name);
97 if (histogram_data != histograms_snapshot_.end())
98 actual_count -= histogram_data->second->GetCount(sample);
99
100 EXPECT_EQ(expected_count, actual_count)
101 << "Histogram \"" << name
102 << "\" does not have the right number of samples (" << expected_count
103 << ") in the expected bucket (" << sample << "). It has (" << actual_count
104 << ").";
105 }
106
107 void HistogramTester::CheckTotalCount(const std::string& name,
108 base::HistogramBase::Count expected_count,
109 base::HistogramSamples& samples) const {
110 int actual_count = samples.TotalCount();
111 std::map<std::string, HistogramSamples*>::const_iterator histogram_data;
112 histogram_data = histograms_snapshot_.find(name);
113 if (histogram_data != histograms_snapshot_.end())
114 actual_count -= histogram_data->second->TotalCount();
115
116 EXPECT_EQ(expected_count, actual_count)
117 << "Histogram \"" << name
118 << "\" does not have the right total number of samples ("
119 << expected_count << "). It has (" << actual_count << ").";
120 }
121
122 } // namespace base
OLDNEW
« no previous file with comments | « base/test/histogram_tester.h ('k') | base/test/histogram_tester_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698