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/histogram_encoder.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/basictypes.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/metrics_log.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 HistogramEncoderTest : public testing::Test { | |
22 public: | |
23 HistogramEncoderTest() {} | |
24 | |
25 private: | |
26 DISALLOW_COPY_AND_ASSIGN(HistogramEncoderTest); | |
27 }; | |
28 | |
29 } // namespace | |
30 | |
31 TEST_F(HistogramEncoderTest, HistogramBucketFields) { | |
32 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12. | |
33 base::BucketRanges ranges(8); | |
34 ranges.set_range(0, 1); | |
35 ranges.set_range(1, 5); | |
36 ranges.set_range(2, 7); | |
37 ranges.set_range(3, 8); | |
38 ranges.set_range(4, 9); | |
39 ranges.set_range(5, 10); | |
40 ranges.set_range(6, 11); | |
41 ranges.set_range(7, 12); | |
42 | |
43 base::SampleVector samples(&ranges); | |
44 samples.Accumulate(3, 1); // Bucket 1-5. | |
45 samples.Accumulate(6, 1); // Bucket 5-7. | |
46 samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped) | |
47 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped) | |
48 samples.Accumulate(11, 1); // Bucket 11-12. | |
49 | |
50 TestMetricsServiceClient client; | |
51 TestingPrefServiceSimple prefs; | |
52 MetricsLog log("totally bogus client ID", 137, MetricsLog::ONGOING_LOG, | |
53 &client, &prefs); | |
Alexei Svitkine (slow)
2014/12/10 22:36:37
I think you missed a comment from earlier - you do
ramant (doing other things)
2014/12/11 02:21:53
Thanks much. Missed it.
Done.
| |
54 ChromeUserMetricsExtension* uma_proto = log.uma_proto(); | |
55 RecordHistogramDelta("Test", samples, uma_proto); | |
56 | |
57 const HistogramEventProto& histogram_proto = | |
58 uma_proto->histogram_event(uma_proto->histogram_event_size() - 1); | |
59 | |
60 // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12. | |
61 // Should become: 1-/, 5-7, /-9, 10-/, /-12. | |
62 ASSERT_EQ(5, histogram_proto.bucket_size()); | |
63 | |
64 // 1-5 becomes 1-/ (max is same as next min). | |
65 EXPECT_TRUE(histogram_proto.bucket(0).has_min()); | |
66 EXPECT_FALSE(histogram_proto.bucket(0).has_max()); | |
67 EXPECT_EQ(1, histogram_proto.bucket(0).min()); | |
68 | |
69 // 5-7 stays 5-7 (no optimization possible). | |
70 EXPECT_TRUE(histogram_proto.bucket(1).has_min()); | |
71 EXPECT_TRUE(histogram_proto.bucket(1).has_max()); | |
72 EXPECT_EQ(5, histogram_proto.bucket(1).min()); | |
73 EXPECT_EQ(7, histogram_proto.bucket(1).max()); | |
74 | |
75 // 8-9 becomes /-9 (min is same as max - 1). | |
76 EXPECT_FALSE(histogram_proto.bucket(2).has_min()); | |
77 EXPECT_TRUE(histogram_proto.bucket(2).has_max()); | |
78 EXPECT_EQ(9, histogram_proto.bucket(2).max()); | |
79 | |
80 // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized). | |
81 EXPECT_TRUE(histogram_proto.bucket(3).has_min()); | |
82 EXPECT_FALSE(histogram_proto.bucket(3).has_max()); | |
83 EXPECT_EQ(10, histogram_proto.bucket(3).min()); | |
84 | |
85 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1). | |
86 EXPECT_FALSE(histogram_proto.bucket(4).has_min()); | |
87 EXPECT_TRUE(histogram_proto.bucket(4).has_max()); | |
88 EXPECT_EQ(12, histogram_proto.bucket(4).max()); | |
89 } | |
90 | |
91 } // namespace metrics | |
OLD | NEW |