Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / components / metrics / metrics_log_base_unittest.cc
blobcc7a1737e2a1339a1b8b6f4c0a9c0e1f08fdc19c
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.
5 #include "components/metrics/metrics_log_base.h"
7 #include <string>
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"
15 namespace metrics {
17 namespace {
19 class TestMetricsLogBase : public MetricsLogBase {
20 public:
21 TestMetricsLogBase()
22 : MetricsLogBase("client_id", 1, MetricsLogBase::ONGOING_LOG, "1.2.3.4") {
24 virtual ~TestMetricsLogBase() {}
26 using MetricsLogBase::uma_proto;
28 private:
29 DISALLOW_COPY_AND_ASSIGN(TestMetricsLogBase);
32 } // namespace
34 TEST(MetricsLogBaseTest, LogType) {
35 MetricsLogBase log1("id", 0, MetricsLogBase::ONGOING_LOG, "1.2.3");
36 EXPECT_EQ(MetricsLogBase::ONGOING_LOG, log1.log_type());
38 MetricsLogBase log2("id", 0, MetricsLogBase::INITIAL_STABILITY_LOG, "1.2.3");
39 EXPECT_EQ(MetricsLogBase::INITIAL_STABILITY_LOG, log2.log_type());
42 TEST(MetricsLogBaseTest, EmptyRecord) {
43 MetricsLogBase log("totally bogus client ID", 137,
44 MetricsLogBase::ONGOING_LOG, "bogus version");
45 log.set_hardware_class("sample-class");
46 log.CloseLog();
48 std::string encoded;
49 log.GetEncodedLog(&encoded);
51 // A couple of fields are hard to mock, so these will be copied over directly
52 // for the expected output.
53 metrics::ChromeUserMetricsExtension parsed;
54 ASSERT_TRUE(parsed.ParseFromString(encoded));
56 metrics::ChromeUserMetricsExtension expected;
57 expected.set_client_id(5217101509553811875); // Hashed bogus client ID
58 expected.set_session_id(137);
59 expected.mutable_system_profile()->set_build_timestamp(
60 parsed.system_profile().build_timestamp());
61 expected.mutable_system_profile()->set_app_version("bogus version");
62 expected.mutable_system_profile()->mutable_hardware()->set_hardware_class(
63 "sample-class");
65 EXPECT_EQ(expected.SerializeAsString(), encoded);
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);
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.
87 TestMetricsLogBase log;
88 log.RecordHistogramDelta("Test", samples);
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);
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());
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());
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());
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());
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());
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());
125 } // namespace metrics