| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/metrics/metrics_log_base.h" | 5 #include "components/metrics/metrics_log_base.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/metrics/bucket_ranges.h" | 10 #include "base/metrics/bucket_ranges.h" |
| 11 #include "base/metrics/sample_vector.h" | 11 #include "base/metrics/sample_vector.h" |
| 12 #include "chrome/common/metrics/proto/chrome_user_metrics_extension.pb.h" | 12 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 namespace metrics { |
| 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 class TestMetricsLogBase : public MetricsLogBase { | 19 class TestMetricsLogBase : public MetricsLogBase { |
| 18 public: | 20 public: |
| 19 TestMetricsLogBase() | 21 TestMetricsLogBase() |
| 20 : MetricsLogBase("client_id", 1, MetricsLogBase::ONGOING_LOG, "1.2.3.4") { | 22 : MetricsLogBase("client_id", 1, MetricsLogBase::ONGOING_LOG, "1.2.3.4") { |
| 21 } | 23 } |
| 22 virtual ~TestMetricsLogBase() {} | 24 virtual ~TestMetricsLogBase() {} |
| 23 | 25 |
| 24 using MetricsLogBase::uma_proto; | 26 using MetricsLogBase::uma_proto; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 50 // for the expected output. | 52 // for the expected output. |
| 51 metrics::ChromeUserMetricsExtension parsed; | 53 metrics::ChromeUserMetricsExtension parsed; |
| 52 ASSERT_TRUE(parsed.ParseFromString(encoded)); | 54 ASSERT_TRUE(parsed.ParseFromString(encoded)); |
| 53 | 55 |
| 54 metrics::ChromeUserMetricsExtension expected; | 56 metrics::ChromeUserMetricsExtension expected; |
| 55 expected.set_client_id(5217101509553811875); // Hashed bogus client ID | 57 expected.set_client_id(5217101509553811875); // Hashed bogus client ID |
| 56 expected.set_session_id(137); | 58 expected.set_session_id(137); |
| 57 expected.mutable_system_profile()->set_build_timestamp( | 59 expected.mutable_system_profile()->set_build_timestamp( |
| 58 parsed.system_profile().build_timestamp()); | 60 parsed.system_profile().build_timestamp()); |
| 59 expected.mutable_system_profile()->set_app_version("bogus version"); | 61 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( | 62 expected.mutable_system_profile()->mutable_hardware()->set_hardware_class( |
| 63 "sample-class"); | 63 "sample-class"); |
| 64 | 64 |
| 65 EXPECT_EQ(expected.SerializeAsString(), encoded); | 65 EXPECT_EQ(expected.SerializeAsString(), encoded); |
| 66 } | 66 } |
| 67 | 67 |
| 68 TEST(MetricsLogBaseTest, HistogramBucketFields) { | 68 TEST(MetricsLogBaseTest, HistogramBucketFields) { |
| 69 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. | 69 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. |
| 70 base::BucketRanges ranges(8); | 70 base::BucketRanges ranges(8); |
| 71 ranges.set_range(0, 1); | 71 ranges.set_range(0, 1); |
| 72 ranges.set_range(1, 5); | 72 ranges.set_range(1, 5); |
| 73 ranges.set_range(2, 7); | 73 ranges.set_range(2, 7); |
| 74 ranges.set_range(3, 8); | 74 ranges.set_range(3, 8); |
| 75 ranges.set_range(4, 9); | 75 ranges.set_range(4, 9); |
| 76 ranges.set_range(5, 10); | 76 ranges.set_range(5, 10); |
| 77 ranges.set_range(6, 11); | 77 ranges.set_range(6, 11); |
| 78 ranges.set_range(7, 12); | 78 ranges.set_range(7, 12); |
| 79 | 79 |
| 80 base::SampleVector samples(&ranges); | 80 base::SampleVector samples(&ranges); |
| 81 samples.Accumulate(3, 1); // Bucket 1-5. | 81 samples.Accumulate(3, 1); // Bucket 1-5. |
| 82 samples.Accumulate(6, 1); // Bucket 5-7. | 82 samples.Accumulate(6, 1); // Bucket 5-7. |
| 83 samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped) | 83 samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped) |
| 84 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped) | 84 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped) |
| 85 samples.Accumulate(11, 1); // Bucket 11-12. | 85 samples.Accumulate(11, 1); // Bucket 11-12. |
| 86 | 86 |
| 87 TestMetricsLogBase log; | 87 TestMetricsLogBase log; |
| 88 log.RecordHistogramDelta("Test", samples); | 88 log.RecordHistogramDelta("Test", samples); |
| 89 | 89 |
| 90 const metrics::ChromeUserMetricsExtension* uma_proto = log.uma_proto(); | 90 const metrics::ChromeUserMetricsExtension* uma_proto = log.uma_proto(); |
| 91 const metrics::HistogramEventProto& histogram_proto = | 91 const metrics::HistogramEventProto& histogram_proto = |
| 92 uma_proto->histogram_event(uma_proto->histogram_event_size() - 1); | 92 uma_proto->histogram_event(uma_proto->histogram_event_size() - 1); |
| 93 | 93 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 114 // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized). | 114 // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized). |
| 115 EXPECT_TRUE(histogram_proto.bucket(3).has_min()); | 115 EXPECT_TRUE(histogram_proto.bucket(3).has_min()); |
| 116 EXPECT_FALSE(histogram_proto.bucket(3).has_max()); | 116 EXPECT_FALSE(histogram_proto.bucket(3).has_max()); |
| 117 EXPECT_EQ(10, histogram_proto.bucket(3).min()); | 117 EXPECT_EQ(10, histogram_proto.bucket(3).min()); |
| 118 | 118 |
| 119 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1). | 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()); | 120 EXPECT_FALSE(histogram_proto.bucket(4).has_min()); |
| 121 EXPECT_TRUE(histogram_proto.bucket(4).has_max()); | 121 EXPECT_TRUE(histogram_proto.bucket(4).has_max()); |
| 122 EXPECT_EQ(12, histogram_proto.bucket(4).max()); | 122 EXPECT_EQ(12, histogram_proto.bucket(4).max()); |
| 123 } | 123 } |
| 124 |
| 125 } // namespace metrics |
| OLD | NEW |