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

Side by Side Diff: chrome/common/metrics/metrics_log_base_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698