OLD | NEW |
1 // Copyright (c) 2012 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 "chrome/test/base/uma_histogram_helper.h" | 5 #include "base/test/histogram_tester.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/metrics/histogram_samples.h" |
8 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
9 #include "base/test/test_timeouts.h" | 10 #include "base/stl_util.h" |
10 #include "chrome/test/base/ui_test_utils.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "content/public/browser/histogram_fetcher.h" | |
12 | 12 |
13 UMAHistogramHelper::UMAHistogramHelper() { | 13 namespace base { |
14 base::StatisticsRecorder::Initialize(); | |
15 } | |
16 | 14 |
17 UMAHistogramHelper::~UMAHistogramHelper() { | 15 HistogramTester::HistogramTester() { |
18 } | 16 StatisticsRecorder::Initialize(); // Safe to call multiple times. |
19 | 17 |
20 void UMAHistogramHelper::PrepareSnapshot(const char* const histogram_names[], | 18 // Record any histogram data that exists when the object is created so it can |
21 size_t num_histograms) { | 19 // be subtracted later. |
22 for (size_t i = 0; i < num_histograms; ++i) { | 20 StatisticsRecorder::Histograms histograms; |
23 std::string histogram_name = histogram_names[i]; | 21 StatisticsRecorder::GetSnapshot(std::string(), &histograms); |
24 | 22 for (size_t i = 0; i < histograms.size(); ++i) { |
25 base::HistogramBase* histogram = | 23 base_snapshot_[histograms[i]->histogram_name()] = |
26 base::StatisticsRecorder::FindHistogram(histogram_name); | 24 histograms[i]->SnapshotSamples().release(); |
27 // If there is no histogram present, then don't record a snapshot. The logic | |
28 // in the Expect* methods will act to treat no histogram equivalent to | |
29 // samples with zeros. | |
30 if (histogram) { | |
31 histogram_snapshots[histogram_name] = | |
32 make_linked_ptr(histogram->SnapshotSamples().release()); | |
33 } | |
34 } | 25 } |
35 } | 26 } |
36 | 27 |
37 void UMAHistogramHelper::Fetch() { | 28 HistogramTester::~HistogramTester() { |
38 base::Closure callback = base::Bind(&UMAHistogramHelper::FetchCallback, | 29 STLDeleteValues(&base_snapshot_); |
39 base::Unretained(this)); | |
40 | |
41 content::FetchHistogramsAsynchronously( | |
42 base::MessageLoop::current(), | |
43 callback, | |
44 // If this call times out, it means that a child process is not | |
45 // responding, which is something we should not ignore. The timeout is | |
46 // set to be longer than the normal browser test timeout so that it will | |
47 // be prempted by the normal timeout. | |
48 TestTimeouts::action_max_timeout() * 2); | |
49 content::RunMessageLoop(); | |
50 } | 30 } |
51 | 31 |
52 void UMAHistogramHelper::ExpectUniqueSample( | 32 void HistogramTester::FetchTestingSnapshot() {} |
| 33 |
| 34 void HistogramTester::ExpectUniqueSample( |
53 const std::string& name, | 35 const std::string& name, |
54 base::HistogramBase::Sample sample, | 36 base::HistogramBase::Sample sample, |
55 base::HistogramBase::Count expected_count) { | 37 base::HistogramBase::Count expected_count) { |
56 base::HistogramBase* histogram = | 38 base::HistogramBase* histogram = |
57 base::StatisticsRecorder::FindHistogram(name); | 39 base::StatisticsRecorder::FindHistogram(name); |
58 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram) | 40 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram) |
59 << "Histogram \"" << name << "\" does not exist."; | 41 << "Histogram \"" << name << "\" does not exist."; |
60 | 42 |
61 if (histogram) { | 43 if (histogram) { |
62 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | 44 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
63 CheckBucketCount(name, sample, expected_count, *samples); | 45 CheckBucketCount(name, sample, expected_count, *samples); |
64 CheckTotalCount(name, expected_count, *samples); | 46 CheckTotalCount(name, expected_count, *samples); |
65 } | 47 } |
66 } | 48 } |
67 | 49 |
68 void UMAHistogramHelper::ExpectBucketCount( | 50 void HistogramTester::ExpectBucketCount( |
69 const std::string& name, | 51 const std::string& name, |
70 base::HistogramBase::Sample sample, | 52 base::HistogramBase::Sample sample, |
71 base::HistogramBase::Count expected_count) { | 53 base::HistogramBase::Count expected_count) { |
72 base::HistogramBase* histogram = | 54 base::HistogramBase* histogram = |
73 base::StatisticsRecorder::FindHistogram(name); | 55 base::StatisticsRecorder::FindHistogram(name); |
74 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram) | 56 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram) |
75 << "Histogram \"" << name << "\" does not exist."; | 57 << "Histogram \"" << name << "\" does not exist."; |
76 | 58 |
77 if (histogram) { | 59 if (histogram) { |
78 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | 60 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
79 CheckBucketCount(name, sample, expected_count, *samples); | 61 CheckBucketCount(name, sample, expected_count, *samples); |
80 } | 62 } |
81 } | 63 } |
82 | 64 |
83 void UMAHistogramHelper::ExpectTotalCount( | 65 void HistogramTester::ExpectTotalCount( |
84 const std::string& name, | 66 const std::string& name, |
85 base::HistogramBase::Count count) { | 67 base::HistogramBase::Count count) { |
86 base::HistogramBase* histogram = | 68 base::HistogramBase* histogram = |
87 base::StatisticsRecorder::FindHistogram(name); | 69 base::StatisticsRecorder::FindHistogram(name); |
88 if (histogram) { | 70 if (histogram) { |
89 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | 71 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
90 CheckTotalCount(name, count, *samples); | 72 CheckTotalCount(name, count, *samples); |
91 } else { | 73 } else { |
92 // No histogram means there were zero samples. | 74 // No histogram means there were zero samples. |
93 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; | 75 EXPECT_EQ(count, 0) << "Histogram \"" << name << "\" does not exist."; |
94 } | 76 } |
95 } | 77 } |
96 | 78 |
97 void UMAHistogramHelper::FetchCallback() { | 79 scoped_ptr<HistogramSamples> |
98 base::MessageLoopForUI::current()->Quit(); | 80 HistogramTester::GetBaseHistogramSamples( |
| 81 const std::string& histogram_name) { |
| 82 HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); |
| 83 if (!histogram) |
| 84 return scoped_ptr<HistogramSamples>(); |
| 85 scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); |
| 86 HistogramSamples* named_original_samples = base_snapshot_[histogram_name]; |
| 87 if (named_original_samples) |
| 88 named_samples->Subtract(*named_original_samples); |
| 89 return named_samples.Pass(); |
99 } | 90 } |
100 | 91 |
101 void UMAHistogramHelper::CheckBucketCount( | 92 void HistogramTester::CheckBucketCount( |
102 const std::string& name, | 93 const std::string& name, |
103 base::HistogramBase::Sample sample, | 94 base::HistogramBase::Sample sample, |
104 base::HistogramBase::Count expected_count, | 95 base::HistogramBase::Count expected_count, |
105 base::HistogramSamples& samples) { | 96 base::HistogramSamples& samples) { |
106 int actual_count = samples.GetCount(sample); | 97 int actual_count = samples.GetCount(sample); |
107 if (histogram_snapshots.count(name)) | 98 if (base_snapshot_.count(name)) |
108 actual_count -= histogram_snapshots[name]->GetCount(sample); | 99 actual_count -= base_snapshot_[name]->GetCount(sample); |
109 EXPECT_EQ(expected_count, actual_count) | 100 EXPECT_EQ(expected_count, actual_count) |
110 << "Histogram \"" << name | 101 << "Histogram \"" << name |
111 << "\" does not have the right number of samples (" << expected_count | 102 << "\" does not have the right number of samples (" << expected_count |
112 << ") in the expected bucket (" << sample << "). It has (" << actual_count | 103 << ") in the expected bucket (" << sample << "). It has (" << actual_count |
113 << ")."; | 104 << ")."; |
114 } | 105 } |
115 | 106 |
116 void UMAHistogramHelper::CheckTotalCount( | 107 void HistogramTester::CheckTotalCount( |
117 const std::string& name, | 108 const std::string& name, |
118 base::HistogramBase::Count expected_count, | 109 base::HistogramBase::Count expected_count, |
119 base::HistogramSamples& samples) { | 110 base::HistogramSamples& samples) { |
120 int actual_count = samples.TotalCount(); | 111 int actual_count = samples.TotalCount(); |
121 if (histogram_snapshots.count(name)) | 112 if (base_snapshot_.count(name)) |
122 actual_count -= histogram_snapshots[name]->TotalCount(); | 113 actual_count -= base_snapshot_[name]->TotalCount(); |
123 EXPECT_EQ(expected_count, actual_count) | 114 EXPECT_EQ(expected_count, actual_count) |
124 << "Histogram \"" << name | 115 << "Histogram \"" << name |
125 << "\" does not have the right total number of samples (" | 116 << "\" does not have the right total number of samples (" |
126 << expected_count << "). It has (" << actual_count << ")."; | 117 << expected_count << "). It has (" << actual_count << ")."; |
127 } | 118 } |
| 119 |
| 120 } // namespace base |
OLD | NEW |