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 "chrome/test/base/uma_histogram_helper.h" | 5 #include "chrome/test/base/uma_histogram_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/statistics_recorder.h" | 8 #include "base/metrics/statistics_recorder.h" |
9 #include "base/test/test_timeouts.h" | 9 #include "base/test/test_timeouts.h" |
10 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
11 #include "content/public/browser/histogram_fetcher.h" | 11 #include "content/public/browser/histogram_fetcher.h" |
12 | 12 |
13 UMAHistogramHelper::UMAHistogramHelper() { | 13 UMAHistogramHelper::UMAHistogramHelper() { |
14 base::StatisticsRecorder::Initialize(); | |
15 } | |
16 | |
17 void UMAHistogramHelper::PrepareSnapshot(const char** histogram_names, | |
18 int num_histograms) { | |
19 for (int i = 0; i < num_histograms; i++) { | |
20 std::string histogram_name = histogram_names[i]; | |
21 | |
22 base::HistogramBase* histogram = | |
23 base::StatisticsRecorder::FindHistogram(histogram_name); | |
24 // If there is no histogram present, then don't record a snapshot. The logic | |
25 // in the Expect* methods will act to treat no histogram equivalent to | |
26 // samples with zeros. | |
27 if (histogram) | |
28 histogram_snapshots[histogram_name] = histogram->SnapshotSamples(); | |
29 } | |
14 } | 30 } |
15 | 31 |
16 void UMAHistogramHelper::Fetch() { | 32 void UMAHistogramHelper::Fetch() { |
17 base::Closure callback = base::Bind(&UMAHistogramHelper::FetchCallback, | 33 base::Closure callback = base::Bind(&UMAHistogramHelper::FetchCallback, |
18 base::Unretained(this)); | 34 base::Unretained(this)); |
19 | 35 |
20 content::FetchHistogramsAsynchronously( | 36 content::FetchHistogramsAsynchronously( |
21 base::MessageLoop::current(), | 37 base::MessageLoop::current(), |
22 callback, | 38 callback, |
23 // If this call times out, it means that a child process is not | 39 // If this call times out, it means that a child process is not |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
75 | 91 |
76 void UMAHistogramHelper::FetchCallback() { | 92 void UMAHistogramHelper::FetchCallback() { |
77 base::MessageLoopForUI::current()->Quit(); | 93 base::MessageLoopForUI::current()->Quit(); |
78 } | 94 } |
79 | 95 |
80 void UMAHistogramHelper::CheckBucketCount( | 96 void UMAHistogramHelper::CheckBucketCount( |
81 const std::string& name, | 97 const std::string& name, |
82 base::HistogramBase::Sample sample, | 98 base::HistogramBase::Sample sample, |
83 base::HistogramBase::Count expected_count, | 99 base::HistogramBase::Count expected_count, |
84 base::HistogramSamples& samples) { | 100 base::HistogramSamples& samples) { |
85 EXPECT_EQ(expected_count, samples.GetCount(sample)) | 101 int actual_count; |
102 if (histogram_snapshots.count(name)) { | |
103 actual_count = samples.GetCount(sample) - | |
104 histogram_snapshots[name]->GetCount(sample); | |
105 } else { | |
106 actual_count = samples.GetCount(sample); | |
107 } | |
108 EXPECT_EQ(expected_count, actual_count) | |
86 << "Histogram \"" << name | 109 << "Histogram \"" << name |
87 << "\" does not have the right number of samples (" << expected_count | 110 << "\" does not have the right number of samples (" << expected_count |
88 << ") in the expected bucket (" << sample << ")."; | 111 << ") in the expected bucket (" << sample << "). It has (" |
112 << actual_count << ")."; | |
89 } | 113 } |
90 | 114 |
91 void UMAHistogramHelper::CheckTotalCount( | 115 void UMAHistogramHelper::CheckTotalCount( |
92 const std::string& name, | 116 const std::string& name, |
93 base::HistogramBase::Count expected_count, | 117 base::HistogramBase::Count expected_count, |
94 base::HistogramSamples& samples) { | 118 base::HistogramSamples& samples) { |
95 EXPECT_EQ(expected_count, samples.TotalCount()) | 119 int actual_count; |
120 if (histogram_snapshots.count(name)) { | |
Alexei Svitkine (slow)
2014/06/12 17:09:11
Nit: How about:
int actual_count = samples.TotalC
Mike Lerman
2014/06/12 18:12:15
Slick. Done.
| |
121 actual_count = samples.TotalCount() - | |
122 histogram_snapshots[name]->TotalCount(); | |
123 } else { | |
124 actual_count = samples.TotalCount(); | |
125 } | |
126 EXPECT_EQ(expected_count, actual_count) | |
96 << "Histogram \"" << name | 127 << "Histogram \"" << name |
97 << "\" does not have the right total number of samples (" | 128 << "\" does not have the right total number of samples (" |
98 << expected_count << ")."; | 129 << expected_count << "). It has (" << actual_count << ")."; |
99 } | 130 } |
OLD | NEW |