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 <map> | 5 #include <map> |
6 | 6 |
7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/metrics/statistics_recorder.h" | 9 #include "base/metrics/statistics_recorder.h" |
10 #include "base/metrics/user_metrics.h" | 10 #include "base/metrics/user_metrics.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 const char* name; | 31 const char* name; |
32 base::HistogramType type; | 32 base::HistogramType type; |
33 int min; | 33 int min; |
34 int max; | 34 int max; |
35 size_t buckets; | 35 size_t buckets; |
36 int count; | 36 int count; |
37 } g_histograms[] = { | 37 } g_histograms[] = { |
38 {"test.h.1", base::HISTOGRAM, 1, 100, 50, 1}, // custom | 38 {"test.h.1", base::HISTOGRAM, 1, 100, 50, 1}, // custom |
39 {"test.h.2", base::LINEAR_HISTOGRAM, 1, 200, 50, 1}, // custom | 39 {"test.h.2", base::LINEAR_HISTOGRAM, 1, 200, 50, 1}, // custom |
40 {"test.h.3", base::LINEAR_HISTOGRAM, 1, 101, 102, 2}, // percentage | 40 {"test.h.3", base::LINEAR_HISTOGRAM, 1, 101, 102, 2}, // percentage |
41 {"test.sparse.1", base::SPARSE_HISTOGRAM, 0, 0, 0, 1}, | |
42 {"test.sparse.2", base::SPARSE_HISTOGRAM, 0, 0, 0, 2}, | |
43 {"test.sparse.3", base::SPARSE_HISTOGRAM, 0, 0, 0, 6}, | |
41 {"test.time", base::HISTOGRAM, 1, 10000, 50, 1}, | 44 {"test.time", base::HISTOGRAM, 1, 10000, 50, 1}, |
42 {"test.medium.time", base::HISTOGRAM, 1, 180000, 50, 1}, | 45 {"test.medium.time", base::HISTOGRAM, 1, 180000, 50, 1}, |
43 {"test.long.time", base::HISTOGRAM, 1, 3600000, 50, 1}, | 46 {"test.long.time", base::HISTOGRAM, 1, 3600000, 50, 1}, |
44 {"test.count", base::HISTOGRAM, 1, 1000000, 50, 1}, | 47 {"test.count", base::HISTOGRAM, 1, 1000000, 50, 1}, |
45 {"test.medium.count", base::HISTOGRAM, 1, 10000, 50, 1}, | 48 {"test.medium.count", base::HISTOGRAM, 1, 10000, 50, 1}, |
46 {"test.small.count", base::HISTOGRAM, 1, 100, 50, 1}, | 49 {"test.small.count", base::HISTOGRAM, 1, 100, 50, 1}, |
47 {"test.bucketchange.linear", base::LINEAR_HISTOGRAM, 1, 100, 10, 2}, | 50 {"test.bucketchange.linear", base::LINEAR_HISTOGRAM, 1, 100, 10, 2}, |
48 {"test.bucketchange.log", base::HISTOGRAM, 1, 100, 10, 2}, }; | 51 {"test.bucketchange.log", base::HISTOGRAM, 1, 100, 10, 2}, }; |
49 | 52 |
53 // Represents a bucket in a sparse histogram. | |
54 struct Bucket { | |
55 int histogram_value; | |
56 int count; | |
57 }; | |
58 | |
59 // We expect the following sparse histograms. | |
60 struct SparseHistogram { | |
61 const char* name; | |
62 int bucket_count; | |
63 Bucket buckets[10]; | |
64 } g_sparse_histograms[] = { | |
65 {"test.sparse.1", 1, {{42, 1}}}, | |
66 {"test.sparse.2", 1, {{24, 2}}}, | |
67 {"test.sparse.3", 3, {{1, 1}, {2, 2}, {3, 3}}}}; | |
68 | |
50 // This class observes and collects user action notifications that are sent | 69 // This class observes and collects user action notifications that are sent |
51 // by the tests, so that they can be examined afterwards for correctness. | 70 // by the tests, so that they can be examined afterwards for correctness. |
52 class UserActionObserver { | 71 class UserActionObserver { |
53 public: | 72 public: |
54 UserActionObserver(); | 73 UserActionObserver(); |
55 ~UserActionObserver(); | 74 ~UserActionObserver(); |
56 | 75 |
57 void ValidateUserActions(const RecordedUserAction* recorded, int count); | 76 void ValidateUserActions(const RecordedUserAction* recorded, int count); |
58 | 77 |
59 private: | 78 private: |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 } | 111 } |
93 | 112 |
94 void UserActionObserver::ValidateUserActions(const RecordedUserAction* recorded, | 113 void UserActionObserver::ValidateUserActions(const RecordedUserAction* recorded, |
95 int count) { | 114 int count) { |
96 for (int i = 0; i < count; ++i) { | 115 for (int i = 0; i < count; ++i) { |
97 const RecordedUserAction& ua = recorded[i]; | 116 const RecordedUserAction& ua = recorded[i]; |
98 EXPECT_EQ(ua.count, GetMetricCount(ua.name)); | 117 EXPECT_EQ(ua.count, GetMetricCount(ua.name)); |
99 } | 118 } |
100 } | 119 } |
101 | 120 |
121 void ValidateSparseHistogramSamples( | |
122 const char* name, | |
Ilya Sherman
2014/03/27 00:08:16
nit: Please pass this as a const std::string&, rat
robliao
2014/03/27 00:21:53
Done.
| |
123 const base::HistogramSamples* samples) { | |
Ilya Sherman
2014/03/27 00:08:16
nit: Please pass by const-ref rather than const-po
robliao
2014/03/27 00:21:53
This is why it was scoped_ptr<...>&. That was pref
| |
124 for (int i = 0; i < arraysize(g_sparse_histograms); ++i) { | |
125 const SparseHistogram& sparse_histogram = g_sparse_histograms[i]; | |
126 if (std::string(name) == sparse_histogram.name) { | |
127 for (int j = 0; j < sparse_histogram.bucket_count; ++j) { | |
128 const Bucket& bucket = sparse_histogram.buckets[j]; | |
129 EXPECT_EQ(bucket.count, samples->GetCount(bucket.histogram_value)); | |
130 } | |
131 } | |
132 } | |
133 } | |
134 | |
102 void ValidateHistograms(const RecordedHistogram* recorded, | 135 void ValidateHistograms(const RecordedHistogram* recorded, |
103 int count) { | 136 int count) { |
104 base::StatisticsRecorder::Histograms histograms; | 137 base::StatisticsRecorder::Histograms histograms; |
105 base::StatisticsRecorder::GetHistograms(&histograms); | 138 base::StatisticsRecorder::GetHistograms(&histograms); |
106 | 139 |
107 // Code other than the tests tun here will record some histogram values, but | 140 // Code other than the tests tun here will record some histogram values, but |
108 // we will ignore those. This function validates that all the histogram we | 141 // we will ignore those. This function validates that all the histogram we |
109 // expect to see are present in the list, and that their basic info is | 142 // expect to see are present in the list, and that their basic info is |
110 // correct. | 143 // correct. |
111 for (int i = 0; i < count; ++i) { | 144 for (int i = 0; i < count; ++i) { |
112 const RecordedHistogram& r = recorded[i]; | 145 const RecordedHistogram& r = recorded[i]; |
113 size_t j = 0; | 146 size_t j = 0; |
114 for (j = 0; j < histograms.size(); ++j) { | 147 for (j = 0; j < histograms.size(); ++j) { |
115 base::HistogramBase* histogram(histograms[j]); | 148 base::HistogramBase* histogram(histograms[j]); |
116 | |
117 if (r.name == histogram->histogram_name()) { | 149 if (r.name == histogram->histogram_name()) { |
118 EXPECT_EQ(r.type, histogram->GetHistogramType()); | |
119 EXPECT_TRUE( | |
120 histogram->HasConstructionArguments(r.min, r.max, r.buckets)); | |
121 scoped_ptr<base::HistogramSamples> snapshot = | 150 scoped_ptr<base::HistogramSamples> snapshot = |
122 histogram->SnapshotSamples(); | 151 histogram->SnapshotSamples(); |
123 base::HistogramBase::Count sample_count = snapshot->TotalCount(); | 152 base::HistogramBase::Count sample_count = snapshot->TotalCount(); |
124 EXPECT_EQ(sample_count, r.count); | 153 EXPECT_EQ(r.count, sample_count); |
154 | |
155 EXPECT_EQ(r.type, histogram->GetHistogramType()); | |
156 if (r.type == base::SPARSE_HISTOGRAM) { | |
157 ValidateSparseHistogramSamples(r.name, snapshot.get()); | |
158 } else { | |
159 EXPECT_TRUE( | |
160 histogram->HasConstructionArguments(r.min, r.max, r.buckets)); | |
161 } | |
125 break; | 162 break; |
126 } | 163 } |
127 } | 164 } |
128 EXPECT_LT(j, histograms.size()); | 165 EXPECT_LT(j, histograms.size()); |
129 } | 166 } |
130 } | 167 } |
131 | 168 |
132 } // anonymous namespace | 169 } // anonymous namespace |
133 | 170 |
134 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) { | 171 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Metrics) { |
135 UserActionObserver observer; | 172 UserActionObserver observer; |
136 | 173 |
137 base::FieldTrialList::CreateFieldTrial("apitestfieldtrial2", "group1"); | 174 base::FieldTrialList::CreateFieldTrial("apitestfieldtrial2", "group1"); |
138 | 175 |
139 std::map<std::string, std::string> params; | 176 std::map<std::string, std::string> params; |
140 params["a"] = "aa"; | 177 params["a"] = "aa"; |
141 params["b"] = "bb"; | 178 params["b"] = "bb"; |
142 ASSERT_TRUE(chrome_variations::AssociateVariationParams( | 179 ASSERT_TRUE(chrome_variations::AssociateVariationParams( |
143 "apitestfieldtrial2", "group1", params)); | 180 "apitestfieldtrial2", "group1", params)); |
144 | 181 |
145 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_; | 182 ASSERT_TRUE(RunComponentExtensionTest("metrics")) << message_; |
146 | 183 |
147 observer.ValidateUserActions(g_user_actions, arraysize(g_user_actions)); | 184 observer.ValidateUserActions(g_user_actions, arraysize(g_user_actions)); |
148 ValidateHistograms(g_histograms, arraysize(g_histograms)); | 185 ValidateHistograms(g_histograms, arraysize(g_histograms)); |
149 } | 186 } |
OLD | NEW |