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.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 "base/prefs/testing_pref_service.h" |
| 13 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
| 14 #include "components/metrics/test_metrics_service_client.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace metrics { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class TestMetricsLog : public MetricsLog { |
| 22 public: |
| 23 TestMetricsLog(TestMetricsServiceClient* client, |
| 24 TestingPrefServiceSimple* prefs) |
| 25 : MetricsLog("client_id", 1, MetricsLog::ONGOING_LOG, client, prefs) { |
| 26 } |
| 27 virtual ~TestMetricsLog() {} |
| 28 |
| 29 using MetricsLog::uma_proto; |
| 30 |
| 31 private: |
| 32 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
| 33 }; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 TEST(MetricsLogTest, LogType) { |
| 38 TestMetricsServiceClient client; |
| 39 TestingPrefServiceSimple prefs; |
| 40 |
| 41 MetricsLog log1("id", 0, MetricsLog::ONGOING_LOG, &client, &prefs); |
| 42 EXPECT_EQ(MetricsLog::ONGOING_LOG, log1.log_type()); |
| 43 |
| 44 MetricsLog log2("id", 0, MetricsLog::INITIAL_STABILITY_LOG, &client, &prefs); |
| 45 EXPECT_EQ(MetricsLog::INITIAL_STABILITY_LOG, log2.log_type()); |
| 46 } |
| 47 |
| 48 TEST(MetricsLogTest, EmptyRecord) { |
| 49 TestMetricsServiceClient client; |
| 50 client.set_version_string("bogus version"); |
| 51 TestingPrefServiceSimple prefs; |
| 52 MetricsLog log("totally bogus client ID", 137, MetricsLog::ONGOING_LOG, |
| 53 &client, &prefs); |
| 54 log.CloseLog(); |
| 55 |
| 56 std::string encoded; |
| 57 log.GetEncodedLog(&encoded); |
| 58 |
| 59 // A couple of fields are hard to mock, so these will be copied over directly |
| 60 // for the expected output. |
| 61 ChromeUserMetricsExtension parsed; |
| 62 ASSERT_TRUE(parsed.ParseFromString(encoded)); |
| 63 |
| 64 ChromeUserMetricsExtension expected; |
| 65 expected.set_client_id(5217101509553811875); // Hashed bogus client ID |
| 66 expected.set_session_id(137); |
| 67 expected.mutable_system_profile()->set_build_timestamp( |
| 68 parsed.system_profile().build_timestamp()); |
| 69 expected.mutable_system_profile()->set_app_version("bogus version"); |
| 70 expected.mutable_system_profile()->set_channel(client.GetChannel()); |
| 71 |
| 72 EXPECT_EQ(expected.SerializeAsString(), encoded); |
| 73 } |
| 74 |
| 75 TEST(MetricsLogTest, HistogramBucketFields) { |
| 76 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. |
| 77 base::BucketRanges ranges(8); |
| 78 ranges.set_range(0, 1); |
| 79 ranges.set_range(1, 5); |
| 80 ranges.set_range(2, 7); |
| 81 ranges.set_range(3, 8); |
| 82 ranges.set_range(4, 9); |
| 83 ranges.set_range(5, 10); |
| 84 ranges.set_range(6, 11); |
| 85 ranges.set_range(7, 12); |
| 86 |
| 87 base::SampleVector samples(&ranges); |
| 88 samples.Accumulate(3, 1); // Bucket 1-5. |
| 89 samples.Accumulate(6, 1); // Bucket 5-7. |
| 90 samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped) |
| 91 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped) |
| 92 samples.Accumulate(11, 1); // Bucket 11-12. |
| 93 |
| 94 TestMetricsServiceClient client; |
| 95 TestingPrefServiceSimple prefs; |
| 96 TestMetricsLog log(&client, &prefs); |
| 97 log.RecordHistogramDelta("Test", samples); |
| 98 |
| 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); |
| 102 |
| 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()); |
| 106 |
| 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()); |
| 111 |
| 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()); |
| 117 |
| 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()); |
| 122 |
| 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()); |
| 127 |
| 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()); |
| 132 } |
| 133 |
| 134 } // namespace metrics |
OLD | NEW |