Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / metrics / histogram_encoder_unittest.cc
blobfb1d47d2d28a2b112d74c083c1b6e7ab49e3f7de
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/histogram_encoder.h"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "base/metrics/bucket_ranges.h"
11 #include "base/metrics/sample_vector.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace metrics {
16 TEST(HistogramEncoder, HistogramBucketFields) {
17 // Create buckets: 1-5, 5-7, 7-8, 8-9, 9-10, 10-11, 11-12.
18 base::BucketRanges ranges(8);
19 ranges.set_range(0, 1);
20 ranges.set_range(1, 5);
21 ranges.set_range(2, 7);
22 ranges.set_range(3, 8);
23 ranges.set_range(4, 9);
24 ranges.set_range(5, 10);
25 ranges.set_range(6, 11);
26 ranges.set_range(7, 12);
28 base::SampleVector samples(&ranges);
29 samples.Accumulate(3, 1); // Bucket 1-5.
30 samples.Accumulate(6, 1); // Bucket 5-7.
31 samples.Accumulate(8, 1); // Bucket 8-9. (7-8 skipped)
32 samples.Accumulate(10, 1); // Bucket 10-11. (9-10 skipped)
33 samples.Accumulate(11, 1); // Bucket 11-12.
35 ChromeUserMetricsExtension uma_proto;
36 EncodeHistogramDelta("Test", samples, &uma_proto);
38 const HistogramEventProto& histogram_proto =
39 uma_proto.histogram_event(uma_proto.histogram_event_size() - 1);
41 // Buckets with samples: 1-5, 5-7, 8-9, 10-11, 11-12.
42 // Should become: 1-/, 5-7, /-9, 10-/, /-12.
43 ASSERT_EQ(5, histogram_proto.bucket_size());
45 // 1-5 becomes 1-/ (max is same as next min).
46 EXPECT_TRUE(histogram_proto.bucket(0).has_min());
47 EXPECT_FALSE(histogram_proto.bucket(0).has_max());
48 EXPECT_EQ(1, histogram_proto.bucket(0).min());
50 // 5-7 stays 5-7 (no optimization possible).
51 EXPECT_TRUE(histogram_proto.bucket(1).has_min());
52 EXPECT_TRUE(histogram_proto.bucket(1).has_max());
53 EXPECT_EQ(5, histogram_proto.bucket(1).min());
54 EXPECT_EQ(7, histogram_proto.bucket(1).max());
56 // 8-9 becomes /-9 (min is same as max - 1).
57 EXPECT_FALSE(histogram_proto.bucket(2).has_min());
58 EXPECT_TRUE(histogram_proto.bucket(2).has_max());
59 EXPECT_EQ(9, histogram_proto.bucket(2).max());
61 // 10-11 becomes 10-/ (both optimizations apply, omit max is prioritized).
62 EXPECT_TRUE(histogram_proto.bucket(3).has_min());
63 EXPECT_FALSE(histogram_proto.bucket(3).has_max());
64 EXPECT_EQ(10, histogram_proto.bucket(3).min());
66 // 11-12 becomes /-12 (last record must keep max, min is same as max - 1).
67 EXPECT_FALSE(histogram_proto.bucket(4).has_min());
68 EXPECT_TRUE(histogram_proto.bucket(4).has_max());
69 EXPECT_EQ(12, histogram_proto.bucket(4).max());
72 } // namespace metrics