OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/metrics/metrics_log_base.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/base64.h" | |
10 #include "base/metrics/bucket_ranges.h" | |
11 #include "base/metrics/sample_vector.h" | |
12 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace metrics { | |
16 | |
17 namespace { | |
18 | |
19 class TestMetricsLogBase : public MetricsLogBase { | |
20 public: | |
21 TestMetricsLogBase() | |
22 : MetricsLogBase("client_id", 1, MetricsLogBase::ONGOING_LOG, "1.2.3.4") { | |
23 } | |
24 virtual ~TestMetricsLogBase() {} | |
25 | |
26 using MetricsLogBase::uma_proto; | |
27 | |
28 private: | |
29 DISALLOW_COPY_AND_ASSIGN(TestMetricsLogBase); | |
30 }; | |
31 | |
32 } // namespace | |
33 | |
34 TEST(MetricsLogBaseTest, LogType) { | |
35 MetricsLogBase log1("id", 0, MetricsLogBase::ONGOING_LOG, "1.2.3"); | |
36 EXPECT_EQ(MetricsLogBase::ONGOING_LOG, log1.log_type()); | |
37 | |
38 MetricsLogBase log2("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "1.2.3"); | |
39 EXPECT_EQ(MetricsLogBase::INITIAL_STABILITY_LOG, log2.log_type()); | |
40 } | |
41 | |
42 TEST(MetricsLogBaseTest, EmptyRecord) { | |
43 MetricsLogBase log("totally bogus client ID", 137, | |
44 MetricsLogBase::ONGOING_LOG, "bogus version"); | |
45 log.CloseLog(); | |
46 | |
47 std::string encoded; | |
48 log.GetEncodedLog(&encoded); | |
49 | |
50 // A couple of fields are hard to mock, so these will be copied over directly | |
51 // for the expected output. | |
52 metrics::ChromeUserMetricsExtension parsed; | |
53 ASSERT_TRUE(parsed.ParseFromString(encoded)); | |
54 | |
55 metrics::ChromeUserMetricsExtension expected; | |
56 expected.set_client_id(5217101509553811875); // Hashed bogus client ID | |
57 expected.set_session_id(137); | |
58 expected.mutable_system_profile()->set_build_timestamp( | |
59 parsed.system_profile().build_timestamp()); | |
60 expected.mutable_system_profile()->set_app_version("bogus version"); | |
61 | |
62 EXPECT_EQ(expected.SerializeAsString(), encoded); | |
63 } | |
64 | |
65 TEST(MetricsLogBaseTest, HistogramBucketFields) { | |
66 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. | |
67 base::BucketRanges ranges(8); | |
68 ranges.set_range(0, 1); | |
69 ranges.set_range(1, 5); | |
70 ranges.set_range(2, 7); | |
71 ranges.set_range(3, 8); | |
72 ranges.set_range(4, 9); | |
73 ranges.set_range(5, 10); | |
74 ranges.set_range(6, 11); | |
75 ranges.set_range(7, 12); | |
76 | |
77 base::SampleVector samples(&ranges); | |
78 samples.Accumulate(3, 1); // Bucket 1-5. | |
79 samples.Accumulate(6, 1); // Bucket 5-7. | |
80 samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped) | |
81 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped) | |
82 samples.Accumulate(11, 1); // Bucket 11-12. | |
83 | |
84 TestMetricsLogBase log; | |
85 log.RecordHistogramDelta("Test", samples); | |
86 | |
87 const metrics::ChromeUserMetricsExtension* uma_proto = log.uma_proto(); | |
88 const metrics::HistogramEventProto& histogram_proto = | |
89 uma_proto->histogram_event(uma_proto->histogram_event_size() - 1); | |
90 | |
91 // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12. | |
92 // Should become: 1-/, 5-7, /-9, 10-/, /-12. | |
93 ASSERT_EQ(5, histogram_proto.bucket_size()); | |
94 | |
95 // 1-5 becomes 1-/ (max is same as next min). | |
96 EXPECT_TRUE(histogram_proto.bucket(0).has_min()); | |
97 EXPECT_FALSE(histogram_proto.bucket(0).has_max()); | |
98 EXPECT_EQ(1, histogram_proto.bucket(0).min()); | |
99 | |
100 // 5-7 stays 5-7 (no optimization possible). | |
101 EXPECT_TRUE(histogram_proto.bucket(1).has_min()); | |
102 EXPECT_TRUE(histogram_proto.bucket(1).has_max()); | |
103 EXPECT_EQ(5, histogram_proto.bucket(1).min()); | |
104 EXPECT_EQ(7, histogram_proto.bucket(1).max()); | |
105 | |
106 // 8-9 becomes /-9 (min is same as max - 1). | |
107 EXPECT_FALSE(histogram_proto.bucket(2).has_min()); | |
108 EXPECT_TRUE(histogram_proto.bucket(2).has_max()); | |
109 EXPECT_EQ(9, histogram_proto.bucket(2).max()); | |
110 | |
111 // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized). | |
112 EXPECT_TRUE(histogram_proto.bucket(3).has_min()); | |
113 EXPECT_FALSE(histogram_proto.bucket(3).has_max()); | |
114 EXPECT_EQ(10, histogram_proto.bucket(3).min()); | |
115 | |
116 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1). | |
117 EXPECT_FALSE(histogram_proto.bucket(4).has_min()); | |
118 EXPECT_TRUE(histogram_proto.bucket(4).has_max()); | |
119 EXPECT_EQ(12, histogram_proto.bucket(4).max()); | |
120 } | |
121 | |
122 } // namespace metrics | |
OLD | NEW |