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

Side by Side Diff: components/metrics/metrics_log_unittest.cc

Issue 327523003: Move all MetricsLog unittests into components_unittests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 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 | « chrome/chrome_tests_unit.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 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 "components/metrics/metrics_log.h" 5 #include "components/metrics/metrics_log.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
Alexei Svitkine (slow) 2014/06/11 00:59:19 Is this include needed?
blundell 2014/06/11 09:36:30 Done.
10 #include "base/metrics/bucket_ranges.h" 12 #include "base/metrics/bucket_ranges.h"
11 #include "base/metrics/sample_vector.h" 13 #include "base/metrics/sample_vector.h"
14 #include "base/port.h"
15 #include "base/prefs/pref_service.h"
12 #include "base/prefs/testing_pref_service.h" 16 #include "base/prefs/testing_pref_service.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/threading/sequenced_worker_pool.h"
21 #include "base/time/time.h"
22 #include "components/metrics/metrics_hashes.h"
23 #include "components/metrics/metrics_pref_names.h"
24 #include "components/metrics/metrics_provider.h"
25 #include "components/metrics/metrics_state_manager.h"
13 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" 26 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
27 #include "components/metrics/proto/profiler_event.pb.h"
28 #include "components/metrics/proto/system_profile.pb.h"
14 #include "components/metrics/test_metrics_service_client.h" 29 #include "components/metrics/test_metrics_service_client.h"
30 #include "components/variations/active_field_trials.h"
15 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
32 #include "url/gurl.h"
Alexei Svitkine (slow) 2014/06/11 00:59:19 Is this include needed? Maybe check all the other
blundell 2014/06/11 09:36:30 Done.
33
34 using base::TimeDelta;
Alexei Svitkine (slow) 2014/06/11 00:59:19 Nit: Remove this using declaration. Prefix things
blundell 2014/06/11 09:36:30 Done.
16 35
17 namespace metrics { 36 namespace metrics {
18 37
19 namespace { 38 namespace {
20 39
40 const char kClientId[] = "bogus client ID";
41 const int64 kInstallDate = 1373051956;
42 const int64 kInstallDateExpected = 1373050800; // Computed from kInstallDate.
43 const int64 kEnabledDate = 1373001211;
44 const int64 kEnabledDateExpected = 1373000400; // Computed from kEnabledDate.
45 const int kSessionId = 127;
46 const variations::ActiveGroupId kFieldTrialIds[] = {
47 {37, 43},
48 {13, 47},
49 {23, 17}
50 };
51 const variations::ActiveGroupId kSyntheticTrials[] = {
52 {55, 15},
53 {66, 16}
54 };
55
21 class TestMetricsLog : public MetricsLog { 56 class TestMetricsLog : public MetricsLog {
22 public: 57 public:
23 TestMetricsLog(TestMetricsServiceClient* client, 58 TestMetricsLog(const std::string& client_id,
59 int session_id,
60 LogType log_type,
61 metrics::MetricsServiceClient* client,
24 TestingPrefServiceSimple* prefs) 62 TestingPrefServiceSimple* prefs)
25 : MetricsLog("client_id", 1, MetricsLog::ONGOING_LOG, client, prefs) { 63 : MetricsLog(client_id, session_id, log_type, client, prefs),
26 } 64 prefs_(prefs) {
65 InitPrefs();
66 }
67
27 virtual ~TestMetricsLog() {} 68 virtual ~TestMetricsLog() {}
28 69
29 using MetricsLog::uma_proto; 70 const metrics::ChromeUserMetricsExtension& uma_proto() const {
71 return *MetricsLog::uma_proto();
72 }
73
74 const metrics::SystemProfileProto& system_profile() const {
75 return uma_proto().system_profile();
76 }
30 77
31 private: 78 private:
79 void InitPrefs() {
80 prefs_->SetString(metrics::prefs::kMetricsReportingEnabledTimestamp,
81 base::Int64ToString(kEnabledDate));
82 }
83
84 virtual void GetFieldTrialIds(
85 std::vector<variations::ActiveGroupId>* field_trial_ids) const
86 OVERRIDE {
87 ASSERT_TRUE(field_trial_ids->empty());
88
89 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) {
90 field_trial_ids->push_back(kFieldTrialIds[i]);
91 }
92 }
93
94 // Weak pointer to the PrefsService used by this log.
95 TestingPrefServiceSimple* prefs_;
96
32 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); 97 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog);
33 }; 98 };
34 99
35 } // namespace 100 } // namespace
36 101
37 TEST(MetricsLogTest, LogType) { 102 class MetricsLogTest : public testing::Test {
103 public:
104 MetricsLogTest() {
105 MetricsLog::RegisterPrefs(prefs_.registry());
106 metrics::MetricsStateManager::RegisterPrefs(prefs_.registry());
107 }
108
109 virtual ~MetricsLogTest() {
110 }
111
112 protected:
113 // Check that the values in |system_values| correspond to the test data
114 // defined at the top of this file.
115 void CheckSystemProfile(const metrics::SystemProfileProto& system_profile) {
116 EXPECT_EQ(kInstallDateExpected, system_profile.install_date());
117 EXPECT_EQ(kEnabledDateExpected, system_profile.uma_enabled_date());
118
119 ASSERT_EQ(arraysize(kFieldTrialIds) + arraysize(kSyntheticTrials),
120 static_cast<size_t>(system_profile.field_trial_size()));
121 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) {
122 const metrics::SystemProfileProto::FieldTrial& field_trial =
123 system_profile.field_trial(i);
124 EXPECT_EQ(kFieldTrialIds[i].name, field_trial.name_id());
125 EXPECT_EQ(kFieldTrialIds[i].group, field_trial.group_id());
126 }
127 // Verify the right data is present for the synthetic trials.
128 for (size_t i = 0; i < arraysize(kSyntheticTrials); ++i) {
129 const metrics::SystemProfileProto::FieldTrial& field_trial =
130 system_profile.field_trial(i + arraysize(kFieldTrialIds));
131 EXPECT_EQ(kSyntheticTrials[i].name, field_trial.name_id());
132 EXPECT_EQ(kSyntheticTrials[i].group, field_trial.group_id());
133 }
134
135 EXPECT_EQ(metrics::TestMetricsServiceClient::kBrandForTesting,
136 system_profile.brand_code());
137
138 const metrics::SystemProfileProto::Hardware& hardware =
139 system_profile.hardware();
140
141 EXPECT_TRUE(hardware.has_cpu());
142 EXPECT_TRUE(hardware.cpu().has_vendor_name());
143 EXPECT_TRUE(hardware.cpu().has_signature());
144
145 // TODO(isherman): Verify other data written into the protobuf as a result
146 // of this call.
147 }
148
149 protected:
150 TestingPrefServiceSimple prefs_;
151
152 private:
153 DISALLOW_COPY_AND_ASSIGN(MetricsLogTest);
154 };
155
156 TEST_F(MetricsLogTest, LogType) {
38 TestMetricsServiceClient client; 157 TestMetricsServiceClient client;
39 TestingPrefServiceSimple prefs; 158 TestingPrefServiceSimple prefs;
40 159
41 MetricsLog log1("id", 0, MetricsLog::ONGOING_LOG, &client, &prefs); 160 MetricsLog log1("id", 0, MetricsLog::ONGOING_LOG, &client, &prefs);
42 EXPECT_EQ(MetricsLog::ONGOING_LOG, log1.log_type()); 161 EXPECT_EQ(MetricsLog::ONGOING_LOG, log1.log_type());
43 162
44 MetricsLog log2("id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &prefs); 163 MetricsLog log2("id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &prefs);
45 EXPECT_EQ(MetricsLog::INITIAL_STABILITY_LOG, log2.log_type()); 164 EXPECT_EQ(MetricsLog::INITIAL_STABILITY_LOG, log2.log_type());
46 } 165 }
47 166
48 TEST(MetricsLogTest, EmptyRecord) { 167 TEST_F(MetricsLogTest, EmptyRecord) {
49 TestMetricsServiceClient client; 168 TestMetricsServiceClient client;
50 client.set_version_string("bogus version"); 169 client.set_version_string("bogus version");
51 TestingPrefServiceSimple prefs; 170 TestingPrefServiceSimple prefs;
52 MetricsLog log("totally bogus client ID", 137, MetricsLog::ONGOING_LOG, 171 MetricsLog log("totally bogus client ID", 137, MetricsLog::ONGOING_LOG,
53 &client, &prefs); 172 &client, &prefs);
54 log.CloseLog(); 173 log.CloseLog();
55 174
56 std::string encoded; 175 std::string encoded;
57 log.GetEncodedLog(&encoded); 176 log.GetEncodedLog(&encoded);
58 177
59 // A couple of fields are hard to mock, so these will be copied over directly 178 // A couple of fields are hard to mock, so these will be copied over directly
60 // for the expected output. 179 // for the expected output.
61 ChromeUserMetricsExtension parsed; 180 ChromeUserMetricsExtension parsed;
62 ASSERT_TRUE(parsed.ParseFromString(encoded)); 181 ASSERT_TRUE(parsed.ParseFromString(encoded));
63 182
64 ChromeUserMetricsExtension expected; 183 ChromeUserMetricsExtension expected;
65 expected.set_client_id(5217101509553811875); // Hashed bogus client ID 184 expected.set_client_id(5217101509553811875); // Hashed bogus client ID
66 expected.set_session_id(137); 185 expected.set_session_id(137);
67 expected.mutable_system_profile()->set_build_timestamp( 186 expected.mutable_system_profile()->set_build_timestamp(
68 parsed.system_profile().build_timestamp()); 187 parsed.system_profile().build_timestamp());
69 expected.mutable_system_profile()->set_app_version("bogus version"); 188 expected.mutable_system_profile()->set_app_version("bogus version");
70 expected.mutable_system_profile()->set_channel(client.GetChannel()); 189 expected.mutable_system_profile()->set_channel(client.GetChannel());
71 190
72 EXPECT_EQ(expected.SerializeAsString(), encoded); 191 EXPECT_EQ(expected.SerializeAsString(), encoded);
73 } 192 }
74 193
75 TEST(MetricsLogTest, HistogramBucketFields) { 194 TEST_F(MetricsLogTest, HistogramBucketFields) {
76 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. 195 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12.
77 base::BucketRanges ranges(8); 196 base::BucketRanges ranges(8);
78 ranges.set_range(0, 1); 197 ranges.set_range(0, 1);
79 ranges.set_range(1, 5); 198 ranges.set_range(1, 5);
80 ranges.set_range(2, 7); 199 ranges.set_range(2, 7);
81 ranges.set_range(3, 8); 200 ranges.set_range(3, 8);
82 ranges.set_range(4, 9); 201 ranges.set_range(4, 9);
83 ranges.set_range(5, 10); 202 ranges.set_range(5, 10);
84 ranges.set_range(6, 11); 203 ranges.set_range(6, 11);
85 ranges.set_range(7, 12); 204 ranges.set_range(7, 12);
86 205
87 base::SampleVector samples(&ranges); 206 base::SampleVector samples(&ranges);
88 samples.Accumulate(3, 1); // Bucket 1-5. 207 samples.Accumulate(3, 1); // Bucket 1-5.
89 samples.Accumulate(6, 1); // Bucket 5-7. 208 samples.Accumulate(6, 1); // Bucket 5-7.
90 samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped) 209 samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped)
91 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped) 210 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped)
92 samples.Accumulate(11, 1); // Bucket 11-12. 211 samples.Accumulate(11, 1); // Bucket 11-12.
93 212
94 TestMetricsServiceClient client; 213 TestMetricsServiceClient client;
95 TestingPrefServiceSimple prefs; 214 TestingPrefServiceSimple prefs;
96 TestMetricsLog log(&client, &prefs); 215 TestMetricsLog log(
216 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
97 log.RecordHistogramDelta("Test", samples); 217 log.RecordHistogramDelta("Test", samples);
98 218
99 const metrics::ChromeUserMetricsExtension* uma_proto = log.uma_proto(); 219 const ChromeUserMetricsExtension& uma_proto = log.uma_proto();
100 const metrics::HistogramEventProto& histogram_proto = 220 const HistogramEventProto& histogram_proto =
101 uma_proto->histogram_event(uma_proto->histogram_event_size() - 1); 221 uma_proto.histogram_event(uma_proto.histogram_event_size() - 1);
102 222
103 // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12. 223 // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12.
104 // Should become: 1-/, 5-7, /-9, 10-/, /-12. 224 // Should become: 1-/, 5-7, /-9, 10-/, /-12.
105 ASSERT_EQ(5, histogram_proto.bucket_size()); 225 ASSERT_EQ(5, histogram_proto.bucket_size());
106 226
107 // 1-5 becomes 1-/ (max is same as next min). 227 // 1-5 becomes 1-/ (max is same as next min).
108 EXPECT_TRUE(histogram_proto.bucket(0).has_min()); 228 EXPECT_TRUE(histogram_proto.bucket(0).has_min());
109 EXPECT_FALSE(histogram_proto.bucket(0).has_max()); 229 EXPECT_FALSE(histogram_proto.bucket(0).has_max());
110 EXPECT_EQ(1, histogram_proto.bucket(0).min()); 230 EXPECT_EQ(1, histogram_proto.bucket(0).min());
111 231
(...skipping 12 matching lines...) Expand all
124 EXPECT_TRUE(histogram_proto.bucket(3).has_min()); 244 EXPECT_TRUE(histogram_proto.bucket(3).has_min());
125 EXPECT_FALSE(histogram_proto.bucket(3).has_max()); 245 EXPECT_FALSE(histogram_proto.bucket(3).has_max());
126 EXPECT_EQ(10, histogram_proto.bucket(3).min()); 246 EXPECT_EQ(10, histogram_proto.bucket(3).min());
127 247
128 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1). 248 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1).
129 EXPECT_FALSE(histogram_proto.bucket(4).has_min()); 249 EXPECT_FALSE(histogram_proto.bucket(4).has_min());
130 EXPECT_TRUE(histogram_proto.bucket(4).has_max()); 250 EXPECT_TRUE(histogram_proto.bucket(4).has_max());
131 EXPECT_EQ(12, histogram_proto.bucket(4).max()); 251 EXPECT_EQ(12, histogram_proto.bucket(4).max());
132 } 252 }
133 253
254 TEST_F(MetricsLogTest, RecordEnvironment) {
255 TestMetricsServiceClient client;
256 client.set_install_date(kInstallDate);
257 TestMetricsLog log(
258 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
259
260 std::vector<variations::ActiveGroupId> synthetic_trials;
261 // Add two synthetic trials.
262 synthetic_trials.push_back(kSyntheticTrials[0]);
263 synthetic_trials.push_back(kSyntheticTrials[1]);
264
265 log.RecordEnvironment(std::vector<MetricsProvider*>(),
266 synthetic_trials);
267 // Check that the system profile on the log has the correct values set.
268 CheckSystemProfile(log.system_profile());
269
270 // Check that the system profile has also been written to prefs.
271 const std::string base64_system_profile =
272 prefs_.GetString(prefs::kStabilitySavedSystemProfile);
273 EXPECT_FALSE(base64_system_profile.empty());
274 std::string serialied_system_profile;
275 EXPECT_TRUE(base::Base64Decode(base64_system_profile,
276 &serialied_system_profile));
277 SystemProfileProto decoded_system_profile;
278 EXPECT_TRUE(decoded_system_profile.ParseFromString(serialied_system_profile));
279 CheckSystemProfile(decoded_system_profile);
280 }
281
282 TEST_F(MetricsLogTest, LoadSavedEnvironmentFromPrefs) {
283 const char* kSystemProfilePref = prefs::kStabilitySavedSystemProfile;
284 const char* kSystemProfileHashPref =
285 prefs::kStabilitySavedSystemProfileHash;
286
287 TestMetricsServiceClient client;
288 client.set_install_date(kInstallDate);
289
290 // The pref value is empty, so loading it from prefs should fail.
291 {
292 TestMetricsLog log(
293 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
294 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs());
295 }
296
297 // Do a RecordEnvironment() call and check whether the pref is recorded.
298 {
299 TestMetricsLog log(
300 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
301 log.RecordEnvironment(std::vector<MetricsProvider*>(),
302 std::vector<variations::ActiveGroupId>());
303 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
304 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
305 }
306
307 {
308 TestMetricsLog log(
309 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
310 EXPECT_TRUE(log.LoadSavedEnvironmentFromPrefs());
311 // Check some values in the system profile.
312 EXPECT_EQ(kInstallDateExpected, log.system_profile().install_date());
313 EXPECT_EQ(kEnabledDateExpected, log.system_profile().uma_enabled_date());
314 // Ensure that the call cleared the prefs.
315 EXPECT_TRUE(prefs_.GetString(kSystemProfilePref).empty());
316 EXPECT_TRUE(prefs_.GetString(kSystemProfileHashPref).empty());
317 }
318
319 // Ensure that a non-matching hash results in the pref being invalid.
320 {
321 TestMetricsLog log(
322 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
323 // Call RecordEnvironment() to record the pref again.
324 log.RecordEnvironment(std::vector<MetricsProvider*>(),
325 std::vector<variations::ActiveGroupId>());
326 }
327
328 {
329 // Set the hash to a bad value.
330 prefs_.SetString(kSystemProfileHashPref, "deadbeef");
331 TestMetricsLog log(
332 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
333 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs());
334 // Ensure that the prefs are cleared, even if the call failed.
335 EXPECT_TRUE(prefs_.GetString(kSystemProfilePref).empty());
336 EXPECT_TRUE(prefs_.GetString(kSystemProfileHashPref).empty());
337 }
338 }
339
340 TEST_F(MetricsLogTest, InitialLogStabilityMetrics) {
341 TestMetricsServiceClient client;
342 TestMetricsLog log(kClientId,
343 kSessionId,
344 MetricsLog::INITIAL_STABILITY_LOG,
345 &client,
346 &prefs_);
347 std::vector<MetricsProvider*> metrics_providers;
348 log.RecordEnvironment(metrics_providers,
349 std::vector<variations::ActiveGroupId>());
350 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(),
351 base::TimeDelta());
352 const SystemProfileProto_Stability& stability =
353 log.system_profile().stability();
354 // Required metrics:
355 EXPECT_TRUE(stability.has_launch_count());
356 EXPECT_TRUE(stability.has_crash_count());
357 // Initial log metrics:
358 EXPECT_TRUE(stability.has_incomplete_shutdown_count());
359 EXPECT_TRUE(stability.has_breakpad_registration_success_count());
360 EXPECT_TRUE(stability.has_breakpad_registration_failure_count());
361 EXPECT_TRUE(stability.has_debugger_present_count());
362 EXPECT_TRUE(stability.has_debugger_not_present_count());
363 }
364
365 TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) {
366 TestMetricsServiceClient client;
367 TestMetricsLog log(
368 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
369 std::vector<MetricsProvider*> metrics_providers;
370 log.RecordEnvironment(metrics_providers,
371 std::vector<variations::ActiveGroupId>());
372 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(),
373 base::TimeDelta());
374 const SystemProfileProto_Stability& stability =
375 log.system_profile().stability();
376 // Required metrics:
377 EXPECT_TRUE(stability.has_launch_count());
378 EXPECT_TRUE(stability.has_crash_count());
379 // Initial log metrics:
380 EXPECT_FALSE(stability.has_incomplete_shutdown_count());
381 EXPECT_FALSE(stability.has_breakpad_registration_success_count());
382 EXPECT_FALSE(stability.has_breakpad_registration_failure_count());
383 EXPECT_FALSE(stability.has_debugger_present_count());
384 EXPECT_FALSE(stability.has_debugger_not_present_count());
385 }
386
387 TEST_F(MetricsLogTest, ChromeChannelWrittenToProtobuf) {
388 TestMetricsServiceClient client;
389 TestMetricsLog log(
390 "user@test.com", kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
391 EXPECT_TRUE(log.uma_proto().system_profile().has_channel());
392 }
393
134 } // namespace metrics 394 } // namespace metrics
OLDNEW
« no previous file with comments | « chrome/chrome_tests_unit.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698