blob: ee299d150d8ae9e4d08eb66811c47013f05b3a00 [file] [log] [blame]
[email protected]064107e2014-05-02 00:59:061// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]fe58acc22012-02-29 01:29:582// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]bfb77b52014-06-07 01:54:015#include "components/metrics/metrics_log.h"
[email protected]46a0efc2013-07-17 15:40:476
[email protected]fe58acc22012-02-29 01:29:587#include <string>
8
9#include "base/base64.h"
[email protected]b0122eb2014-06-11 18:58:3210#include "base/basictypes.h"
[email protected]838f7702013-10-04 22:20:5111#include "base/metrics/bucket_ranges.h"
12#include "base/metrics/sample_vector.h"
[email protected]b0122eb2014-06-11 18:58:3213#include "base/prefs/pref_service.h"
[email protected]bfb77b52014-06-07 01:54:0114#include "base/prefs/testing_pref_service.h"
[email protected]b0122eb2014-06-11 18:58:3215#include "base/strings/string_number_conversions.h"
16#include "base/time/time.h"
17#include "components/metrics/metrics_pref_names.h"
18#include "components/metrics/metrics_state_manager.h"
[email protected]064107e2014-05-02 00:59:0619#include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
[email protected]bfb77b52014-06-07 01:54:0120#include "components/metrics/test_metrics_service_client.h"
[email protected]b0122eb2014-06-11 18:58:3221#include "components/variations/active_field_trials.h"
[email protected]fe58acc22012-02-29 01:29:5822#include "testing/gtest/include/gtest/gtest.h"
[email protected]fe58acc22012-02-29 01:29:5823
[email protected]064107e2014-05-02 00:59:0624namespace metrics {
25
[email protected]838f7702013-10-04 22:20:5126namespace {
27
[email protected]b0122eb2014-06-11 18:58:3228const char kClientId[] = "bogus client ID";
29const int64 kInstallDate = 1373051956;
30const int64 kInstallDateExpected = 1373050800; // Computed from kInstallDate.
31const int64 kEnabledDate = 1373001211;
32const int64 kEnabledDateExpected = 1373000400; // Computed from kEnabledDate.
33const int kSessionId = 127;
34const variations::ActiveGroupId kFieldTrialIds[] = {
35 {37, 43},
36 {13, 47},
37 {23, 17}
38};
39const variations::ActiveGroupId kSyntheticTrials[] = {
40 {55, 15},
41 {66, 16}
42};
43
[email protected]bfb77b52014-06-07 01:54:0144class TestMetricsLog : public MetricsLog {
[email protected]838f7702013-10-04 22:20:5145 public:
[email protected]b0122eb2014-06-11 18:58:3246 TestMetricsLog(const std::string& client_id,
47 int session_id,
48 LogType log_type,
asvitkine4c1d1ef2014-09-29 20:57:3249 MetricsServiceClient* client,
[email protected]bfb77b52014-06-07 01:54:0150 TestingPrefServiceSimple* prefs)
[email protected]b0122eb2014-06-11 18:58:3251 : MetricsLog(client_id, session_id, log_type, client, prefs),
52 prefs_(prefs) {
53 InitPrefs();
asvitkine4c1d1ef2014-09-29 20:57:3254 }
[email protected]b0122eb2014-06-11 18:58:3255
dcheng00ea022b2014-10-21 11:24:5656 ~TestMetricsLog() override {}
[email protected]838f7702013-10-04 22:20:5157
asvitkine4c1d1ef2014-09-29 20:57:3258 const ChromeUserMetricsExtension& uma_proto() const {
[email protected]b0122eb2014-06-11 18:58:3259 return *MetricsLog::uma_proto();
60 }
61
asvitkine4c1d1ef2014-09-29 20:57:3262 const SystemProfileProto& system_profile() const {
[email protected]b0122eb2014-06-11 18:58:3263 return uma_proto().system_profile();
64 }
[email protected]838f7702013-10-04 22:20:5165
66 private:
[email protected]b0122eb2014-06-11 18:58:3267 void InitPrefs() {
asvitkine4c1d1ef2014-09-29 20:57:3268 prefs_->SetString(prefs::kMetricsReportingEnabledTimestamp,
[email protected]b0122eb2014-06-11 18:58:3269 base::Int64ToString(kEnabledDate));
70 }
71
dcheng00ea022b2014-10-21 11:24:5672 void GetFieldTrialIds(
73 std::vector<variations::ActiveGroupId>* field_trial_ids) const override {
[email protected]b0122eb2014-06-11 18:58:3274 ASSERT_TRUE(field_trial_ids->empty());
75
76 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) {
77 field_trial_ids->push_back(kFieldTrialIds[i]);
78 }
79 }
80
81 // Weak pointer to the PrefsService used by this log.
82 TestingPrefServiceSimple* prefs_;
83
[email protected]bfb77b52014-06-07 01:54:0184 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog);
[email protected]838f7702013-10-04 22:20:5185};
86
87} // namespace
88
[email protected]b0122eb2014-06-11 18:58:3289class MetricsLogTest : public testing::Test {
90 public:
91 MetricsLogTest() {
92 MetricsLog::RegisterPrefs(prefs_.registry());
asvitkine4c1d1ef2014-09-29 20:57:3293 MetricsStateManager::RegisterPrefs(prefs_.registry());
[email protected]b0122eb2014-06-11 18:58:3294 }
95
dcheng30a1b1542014-10-29 21:27:5096 ~MetricsLogTest() override {}
[email protected]b0122eb2014-06-11 18:58:3297
98 protected:
99 // Check that the values in |system_values| correspond to the test data
100 // defined at the top of this file.
asvitkine4c1d1ef2014-09-29 20:57:32101 void CheckSystemProfile(const SystemProfileProto& system_profile) {
[email protected]b0122eb2014-06-11 18:58:32102 EXPECT_EQ(kInstallDateExpected, system_profile.install_date());
103 EXPECT_EQ(kEnabledDateExpected, system_profile.uma_enabled_date());
104
105 ASSERT_EQ(arraysize(kFieldTrialIds) + arraysize(kSyntheticTrials),
106 static_cast<size_t>(system_profile.field_trial_size()));
107 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) {
asvitkine4c1d1ef2014-09-29 20:57:32108 const SystemProfileProto::FieldTrial& field_trial =
[email protected]b0122eb2014-06-11 18:58:32109 system_profile.field_trial(i);
110 EXPECT_EQ(kFieldTrialIds[i].name, field_trial.name_id());
111 EXPECT_EQ(kFieldTrialIds[i].group, field_trial.group_id());
112 }
113 // Verify the right data is present for the synthetic trials.
114 for (size_t i = 0; i < arraysize(kSyntheticTrials); ++i) {
asvitkine4c1d1ef2014-09-29 20:57:32115 const SystemProfileProto::FieldTrial& field_trial =
[email protected]b0122eb2014-06-11 18:58:32116 system_profile.field_trial(i + arraysize(kFieldTrialIds));
117 EXPECT_EQ(kSyntheticTrials[i].name, field_trial.name_id());
118 EXPECT_EQ(kSyntheticTrials[i].group, field_trial.group_id());
119 }
120
asvitkine4c1d1ef2014-09-29 20:57:32121 EXPECT_EQ(TestMetricsServiceClient::kBrandForTesting,
[email protected]b0122eb2014-06-11 18:58:32122 system_profile.brand_code());
123
asvitkine4c1d1ef2014-09-29 20:57:32124 const SystemProfileProto::Hardware& hardware =
[email protected]b0122eb2014-06-11 18:58:32125 system_profile.hardware();
126
127 EXPECT_TRUE(hardware.has_cpu());
128 EXPECT_TRUE(hardware.cpu().has_vendor_name());
129 EXPECT_TRUE(hardware.cpu().has_signature());
gayanef550f50f2015-03-11 00:44:59130 EXPECT_TRUE(hardware.cpu().has_num_cores());
[email protected]b0122eb2014-06-11 18:58:32131
132 // TODO(isherman): Verify other data written into the protobuf as a result
133 // of this call.
134 }
135
136 protected:
137 TestingPrefServiceSimple prefs_;
138
139 private:
140 DISALLOW_COPY_AND_ASSIGN(MetricsLogTest);
141};
142
143TEST_F(MetricsLogTest, LogType) {
[email protected]bfb77b52014-06-07 01:54:01144 TestMetricsServiceClient client;
145 TestingPrefServiceSimple prefs;
[email protected]9eae4032014-04-09 19:15:19146
[email protected]bfb77b52014-06-07 01:54:01147 MetricsLog log1("id", 0, MetricsLog::ONGOING_LOG, &client, &prefs);
148 EXPECT_EQ(MetricsLog::ONGOING_LOG, log1.log_type());
149
150 MetricsLog log2("id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &prefs);
151 EXPECT_EQ(MetricsLog::INITIAL_STABILITY_LOG, log2.log_type());
[email protected]9eae4032014-04-09 19:15:19152}
153
[email protected]b0122eb2014-06-11 18:58:32154TEST_F(MetricsLogTest, EmptyRecord) {
[email protected]bfb77b52014-06-07 01:54:01155 TestMetricsServiceClient client;
156 client.set_version_string("bogus version");
157 TestingPrefServiceSimple prefs;
158 MetricsLog log("totally bogus client ID", 137, MetricsLog::ONGOING_LOG,
159 &client, &prefs);
[email protected]fe58acc22012-02-29 01:29:58160 log.CloseLog();
161
[email protected]fe58acc22012-02-29 01:29:58162 std::string encoded;
[email protected]46a0efc2013-07-17 15:40:47163 log.GetEncodedLog(&encoded);
[email protected]fe58acc22012-02-29 01:29:58164
[email protected]46a0efc2013-07-17 15:40:47165 // A couple of fields are hard to mock, so these will be copied over directly
166 // for the expected output.
[email protected]bfb77b52014-06-07 01:54:01167 ChromeUserMetricsExtension parsed;
[email protected]46a0efc2013-07-17 15:40:47168 ASSERT_TRUE(parsed.ParseFromString(encoded));
[email protected]fe58acc22012-02-29 01:29:58169
[email protected]bfb77b52014-06-07 01:54:01170 ChromeUserMetricsExtension expected;
[email protected]46a0efc2013-07-17 15:40:47171 expected.set_client_id(5217101509553811875); // Hashed bogus client ID
172 expected.set_session_id(137);
173 expected.mutable_system_profile()->set_build_timestamp(
174 parsed.system_profile().build_timestamp());
175 expected.mutable_system_profile()->set_app_version("bogus version");
[email protected]bfb77b52014-06-07 01:54:01176 expected.mutable_system_profile()->set_channel(client.GetChannel());
[email protected]fe58acc22012-02-29 01:29:58177
[email protected]46a0efc2013-07-17 15:40:47178 EXPECT_EQ(expected.SerializeAsString(), encoded);
[email protected]fe58acc22012-02-29 01:29:58179}
180
[email protected]b0122eb2014-06-11 18:58:32181TEST_F(MetricsLogTest, HistogramBucketFields) {
[email protected]838f7702013-10-04 22:20:51182 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12.
183 base::BucketRanges ranges(8);
184 ranges.set_range(0, 1);
185 ranges.set_range(1, 5);
186 ranges.set_range(2, 7);
187 ranges.set_range(3, 8);
188 ranges.set_range(4, 9);
189 ranges.set_range(5, 10);
190 ranges.set_range(6, 11);
191 ranges.set_range(7, 12);
192
193 base::SampleVector samples(&ranges);
[email protected]064107e2014-05-02 00:59:06194 samples.Accumulate(3, 1); // Bucket 1-5.
195 samples.Accumulate(6, 1); // Bucket 5-7.
196 samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped)
[email protected]838f7702013-10-04 22:20:51197 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped)
198 samples.Accumulate(11, 1); // Bucket 11-12.
199
[email protected]bfb77b52014-06-07 01:54:01200 TestMetricsServiceClient client;
201 TestingPrefServiceSimple prefs;
[email protected]b0122eb2014-06-11 18:58:32202 TestMetricsLog log(
203 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
[email protected]838f7702013-10-04 22:20:51204 log.RecordHistogramDelta("Test", samples);
205
[email protected]b0122eb2014-06-11 18:58:32206 const ChromeUserMetricsExtension& uma_proto = log.uma_proto();
207 const HistogramEventProto& histogram_proto =
208 uma_proto.histogram_event(uma_proto.histogram_event_size() - 1);
[email protected]838f7702013-10-04 22:20:51209
210 // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12.
211 // Should become: 1-/, 5-7, /-9, 10-/, /-12.
212 ASSERT_EQ(5, histogram_proto.bucket_size());
213
214 // 1-5 becomes 1-/ (max is same as next min).
215 EXPECT_TRUE(histogram_proto.bucket(0).has_min());
216 EXPECT_FALSE(histogram_proto.bucket(0).has_max());
217 EXPECT_EQ(1, histogram_proto.bucket(0).min());
218
219 // 5-7 stays 5-7 (no optimization possible).
220 EXPECT_TRUE(histogram_proto.bucket(1).has_min());
221 EXPECT_TRUE(histogram_proto.bucket(1).has_max());
222 EXPECT_EQ(5, histogram_proto.bucket(1).min());
223 EXPECT_EQ(7, histogram_proto.bucket(1).max());
224
225 // 8-9 becomes /-9 (min is same as max - 1).
226 EXPECT_FALSE(histogram_proto.bucket(2).has_min());
227 EXPECT_TRUE(histogram_proto.bucket(2).has_max());
228 EXPECT_EQ(9, histogram_proto.bucket(2).max());
229
230 // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized).
231 EXPECT_TRUE(histogram_proto.bucket(3).has_min());
232 EXPECT_FALSE(histogram_proto.bucket(3).has_max());
233 EXPECT_EQ(10, histogram_proto.bucket(3).min());
234
235 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1).
236 EXPECT_FALSE(histogram_proto.bucket(4).has_min());
237 EXPECT_TRUE(histogram_proto.bucket(4).has_max());
238 EXPECT_EQ(12, histogram_proto.bucket(4).max());
239}
[email protected]064107e2014-05-02 00:59:06240
[email protected]b0122eb2014-06-11 18:58:32241TEST_F(MetricsLogTest, RecordEnvironment) {
242 TestMetricsServiceClient client;
[email protected]b0122eb2014-06-11 18:58:32243 TestMetricsLog log(
244 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
245
246 std::vector<variations::ActiveGroupId> synthetic_trials;
247 // Add two synthetic trials.
248 synthetic_trials.push_back(kSyntheticTrials[0]);
249 synthetic_trials.push_back(kSyntheticTrials[1]);
250
251 log.RecordEnvironment(std::vector<MetricsProvider*>(),
[email protected]65801452014-07-09 05:42:41252 synthetic_trials,
olivierrobinc3dfc5b2015-04-07 19:12:00253 kInstallDate, kEnabledDate);
[email protected]b0122eb2014-06-11 18:58:32254 // Check that the system profile on the log has the correct values set.
255 CheckSystemProfile(log.system_profile());
256
257 // Check that the system profile has also been written to prefs.
258 const std::string base64_system_profile =
259 prefs_.GetString(prefs::kStabilitySavedSystemProfile);
260 EXPECT_FALSE(base64_system_profile.empty());
261 std::string serialied_system_profile;
262 EXPECT_TRUE(base::Base64Decode(base64_system_profile,
263 &serialied_system_profile));
264 SystemProfileProto decoded_system_profile;
265 EXPECT_TRUE(decoded_system_profile.ParseFromString(serialied_system_profile));
266 CheckSystemProfile(decoded_system_profile);
267}
268
269TEST_F(MetricsLogTest, LoadSavedEnvironmentFromPrefs) {
270 const char* kSystemProfilePref = prefs::kStabilitySavedSystemProfile;
271 const char* kSystemProfileHashPref =
272 prefs::kStabilitySavedSystemProfileHash;
273
274 TestMetricsServiceClient client;
[email protected]b0122eb2014-06-11 18:58:32275
276 // The pref value is empty, so loading it from prefs should fail.
277 {
278 TestMetricsLog log(
279 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
280 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs());
281 }
282
283 // Do a RecordEnvironment() call and check whether the pref is recorded.
284 {
285 TestMetricsLog log(
286 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
287 log.RecordEnvironment(std::vector<MetricsProvider*>(),
[email protected]65801452014-07-09 05:42:41288 std::vector<variations::ActiveGroupId>(),
olivierrobinc3dfc5b2015-04-07 19:12:00289 kInstallDate, kEnabledDate);
[email protected]b0122eb2014-06-11 18:58:32290 EXPECT_FALSE(prefs_.GetString(kSystemProfilePref).empty());
291 EXPECT_FALSE(prefs_.GetString(kSystemProfileHashPref).empty());
292 }
293
294 {
295 TestMetricsLog log(
296 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
297 EXPECT_TRUE(log.LoadSavedEnvironmentFromPrefs());
298 // Check some values in the system profile.
299 EXPECT_EQ(kInstallDateExpected, log.system_profile().install_date());
300 EXPECT_EQ(kEnabledDateExpected, log.system_profile().uma_enabled_date());
301 // Ensure that the call cleared the prefs.
302 EXPECT_TRUE(prefs_.GetString(kSystemProfilePref).empty());
303 EXPECT_TRUE(prefs_.GetString(kSystemProfileHashPref).empty());
304 }
305
306 // Ensure that a non-matching hash results in the pref being invalid.
307 {
308 TestMetricsLog log(
309 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
310 // Call RecordEnvironment() to record the pref again.
311 log.RecordEnvironment(std::vector<MetricsProvider*>(),
[email protected]65801452014-07-09 05:42:41312 std::vector<variations::ActiveGroupId>(),
olivierrobinc3dfc5b2015-04-07 19:12:00313 kInstallDate, kEnabledDate);
[email protected]b0122eb2014-06-11 18:58:32314 }
315
316 {
317 // Set the hash to a bad value.
318 prefs_.SetString(kSystemProfileHashPref, "deadbeef");
319 TestMetricsLog log(
320 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
321 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs());
322 // Ensure that the prefs are cleared, even if the call failed.
323 EXPECT_TRUE(prefs_.GetString(kSystemProfilePref).empty());
324 EXPECT_TRUE(prefs_.GetString(kSystemProfileHashPref).empty());
325 }
326}
327
328TEST_F(MetricsLogTest, InitialLogStabilityMetrics) {
329 TestMetricsServiceClient client;
330 TestMetricsLog log(kClientId,
331 kSessionId,
332 MetricsLog::INITIAL_STABILITY_LOG,
333 &client,
334 &prefs_);
335 std::vector<MetricsProvider*> metrics_providers;
336 log.RecordEnvironment(metrics_providers,
[email protected]65801452014-07-09 05:42:41337 std::vector<variations::ActiveGroupId>(),
olivierrobinc3dfc5b2015-04-07 19:12:00338 kInstallDate, kEnabledDate);
[email protected]b0122eb2014-06-11 18:58:32339 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(),
340 base::TimeDelta());
341 const SystemProfileProto_Stability& stability =
342 log.system_profile().stability();
343 // Required metrics:
344 EXPECT_TRUE(stability.has_launch_count());
345 EXPECT_TRUE(stability.has_crash_count());
346 // Initial log metrics:
347 EXPECT_TRUE(stability.has_incomplete_shutdown_count());
348 EXPECT_TRUE(stability.has_breakpad_registration_success_count());
349 EXPECT_TRUE(stability.has_breakpad_registration_failure_count());
350 EXPECT_TRUE(stability.has_debugger_present_count());
351 EXPECT_TRUE(stability.has_debugger_not_present_count());
352}
353
354TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) {
355 TestMetricsServiceClient client;
356 TestMetricsLog log(
357 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
358 std::vector<MetricsProvider*> metrics_providers;
359 log.RecordEnvironment(metrics_providers,
[email protected]65801452014-07-09 05:42:41360 std::vector<variations::ActiveGroupId>(),
olivierrobinc3dfc5b2015-04-07 19:12:00361 kInstallDate, kEnabledDate);
[email protected]b0122eb2014-06-11 18:58:32362 log.RecordStabilityMetrics(metrics_providers, base::TimeDelta(),
363 base::TimeDelta());
364 const SystemProfileProto_Stability& stability =
365 log.system_profile().stability();
366 // Required metrics:
367 EXPECT_TRUE(stability.has_launch_count());
368 EXPECT_TRUE(stability.has_crash_count());
369 // Initial log metrics:
370 EXPECT_FALSE(stability.has_incomplete_shutdown_count());
371 EXPECT_FALSE(stability.has_breakpad_registration_success_count());
372 EXPECT_FALSE(stability.has_breakpad_registration_failure_count());
373 EXPECT_FALSE(stability.has_debugger_present_count());
374 EXPECT_FALSE(stability.has_debugger_not_present_count());
375}
376
377TEST_F(MetricsLogTest, ChromeChannelWrittenToProtobuf) {
378 TestMetricsServiceClient client;
379 TestMetricsLog log(
asvitkine4c1d1ef2014-09-29 20:57:32380 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
[email protected]b0122eb2014-06-11 18:58:32381 EXPECT_TRUE(log.uma_proto().system_profile().has_channel());
382}
383
asvitkine4c1d1ef2014-09-29 20:57:32384TEST_F(MetricsLogTest, ProductNotSetIfDefault) {
385 TestMetricsServiceClient client;
386 EXPECT_EQ(ChromeUserMetricsExtension::CHROME, client.GetProduct());
387 TestMetricsLog log(
388 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
389 // Check that the product isn't set, since it's default and also verify the
390 // default value is indeed equal to Chrome.
391 EXPECT_FALSE(log.uma_proto().has_product());
392 EXPECT_EQ(ChromeUserMetricsExtension::CHROME, log.uma_proto().product());
393}
394
395TEST_F(MetricsLogTest, ProductSetIfNotDefault) {
396 const int32_t kTestProduct = 100;
397 EXPECT_NE(ChromeUserMetricsExtension::CHROME, kTestProduct);
398
399 TestMetricsServiceClient client;
400 client.set_product(kTestProduct);
401 TestMetricsLog log(
402 kClientId, kSessionId, MetricsLog::ONGOING_LOG, &client, &prefs_);
403 // Check that the product is set to |kTestProduct|.
404 EXPECT_TRUE(log.uma_proto().has_product());
405 EXPECT_EQ(kTestProduct, log.uma_proto().product());
406}
407
[email protected]064107e2014-05-02 00:59:06408} // namespace metrics