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
3 // found in the LICENSE file.
5 #include "chrome/common/metrics/metrics_log_base.h"
9 #include "base/base64.h"
10 #include "base/format_macros.h"
11 #include "base/metrics/bucket_ranges.h"
12 #include "base/metrics/sample_vector.h"
13 #include "base/strings/stringprintf.h"
14 #include "chrome/common/metrics/proto/chrome_user_metrics_extension.pb.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 class TestMetricsLogBase
: public MetricsLogBase
{
21 TestMetricsLogBase() : MetricsLogBase("client_id", 1, "1.2.3.4") {}
22 virtual ~TestMetricsLogBase() {}
24 using MetricsLogBase::uma_proto
;
27 DISALLOW_COPY_AND_ASSIGN(TestMetricsLogBase
);
32 TEST(MetricsLogBaseTest
, EmptyRecord
) {
33 MetricsLogBase
log("totally bogus client ID", 137, "bogus version");
34 log
.set_hardware_class("sample-class");
38 log
.GetEncodedLog(&encoded
);
40 // A couple of fields are hard to mock, so these will be copied over directly
41 // for the expected output.
42 metrics::ChromeUserMetricsExtension parsed
;
43 ASSERT_TRUE(parsed
.ParseFromString(encoded
));
45 metrics::ChromeUserMetricsExtension expected
;
46 expected
.set_client_id(5217101509553811875); // Hashed bogus client ID
47 expected
.set_session_id(137);
48 expected
.mutable_system_profile()->set_build_timestamp(
49 parsed
.system_profile().build_timestamp());
50 expected
.mutable_system_profile()->set_app_version("bogus version");
51 expected
.mutable_system_profile()->set_channel(
52 parsed
.system_profile().channel());
53 expected
.mutable_system_profile()->mutable_hardware()->set_hardware_class(
56 EXPECT_EQ(expected
.SerializeAsString(), encoded
);
59 // Make sure our ID hashes are the same as what we see on the server side.
60 TEST(MetricsLogBaseTest
, Hashes
) {
65 {"Back", "0x0557fa923dcee4d0"},
66 {"Forward", "0x67d2f6740a8eaebf"},
67 {"NewTab", "0x290eb683f96572f1"},
70 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
71 uint64 hash
= MetricsLogBase::Hash(cases
[i
].input
);
72 std::string hash_hex
= base::StringPrintf("0x%016" PRIx64
, hash
);
73 EXPECT_EQ(cases
[i
].output
, hash_hex
);
77 TEST(MetricsLogBaseTest
, HistogramBucketFields
) {
78 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12.
79 base::BucketRanges
ranges(8);
80 ranges
.set_range(0, 1);
81 ranges
.set_range(1, 5);
82 ranges
.set_range(2, 7);
83 ranges
.set_range(3, 8);
84 ranges
.set_range(4, 9);
85 ranges
.set_range(5, 10);
86 ranges
.set_range(6, 11);
87 ranges
.set_range(7, 12);
89 base::SampleVector
samples(&ranges
);
90 samples
.Accumulate(3, 1); // Bucket 1-5.
91 samples
.Accumulate(6, 1); // Bucket 5-7.
92 samples
.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped)
93 samples
.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped)
94 samples
.Accumulate(11, 1); // Bucket 11-12.
96 TestMetricsLogBase log
;
97 log
.RecordHistogramDelta("Test", samples
);
99 const metrics::ChromeUserMetricsExtension
* uma_proto
= log
.uma_proto();
100 const metrics::HistogramEventProto
& histogram_proto
=
101 uma_proto
->histogram_event(uma_proto
->histogram_event_size() - 1);
103 // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12.
104 // Should become: 1-/, 5-7, /-9, 10-/, /-12.
105 ASSERT_EQ(5, histogram_proto
.bucket_size());
107 // 1-5 becomes 1-/ (max is same as next min).
108 EXPECT_TRUE(histogram_proto
.bucket(0).has_min());
109 EXPECT_FALSE(histogram_proto
.bucket(0).has_max());
110 EXPECT_EQ(1, histogram_proto
.bucket(0).min());
112 // 5-7 stays 5-7 (no optimization possible).
113 EXPECT_TRUE(histogram_proto
.bucket(1).has_min());
114 EXPECT_TRUE(histogram_proto
.bucket(1).has_max());
115 EXPECT_EQ(5, histogram_proto
.bucket(1).min());
116 EXPECT_EQ(7, histogram_proto
.bucket(1).max());
118 // 8-9 becomes /-9 (min is same as max - 1).
119 EXPECT_FALSE(histogram_proto
.bucket(2).has_min());
120 EXPECT_TRUE(histogram_proto
.bucket(2).has_max());
121 EXPECT_EQ(9, histogram_proto
.bucket(2).max());
123 // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized).
124 EXPECT_TRUE(histogram_proto
.bucket(3).has_min());
125 EXPECT_FALSE(histogram_proto
.bucket(3).has_max());
126 EXPECT_EQ(10, histogram_proto
.bucket(3).min());
128 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1).
129 EXPECT_FALSE(histogram_proto
.bucket(4).has_min());
130 EXPECT_TRUE(histogram_proto
.bucket(4).has_max());
131 EXPECT_EQ(12, histogram_proto
.bucket(4).max());