rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 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 | |
mef | c2bd1aa | 2015-04-09 03:26:53 | [diff] [blame] | 5 | #include "components/cronet/histogram_manager.h" |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 6 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 9 | #include <string> |
| 10 | |
asvitkine | 8341e907 | 2017-01-20 01:27:16 | [diff] [blame] | 11 | #include "base/metrics/histogram_macros.h" |
mef | c2bd1aa | 2015-04-09 03:26:53 | [diff] [blame] | 12 | #include "base/metrics/statistics_recorder.h" |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
mef | c2bd1aa | 2015-04-09 03:26:53 | [diff] [blame] | 15 | namespace cronet { |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 16 | |
mef | c2bd1aa | 2015-04-09 03:26:53 | [diff] [blame] | 17 | using metrics::ChromeUserMetricsExtension; |
| 18 | using metrics::HistogramEventProto; |
| 19 | |
| 20 | TEST(HistogramManager, HistogramBucketFields) { |
| 21 | base::StatisticsRecorder::Initialize(); |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 22 | // Capture histograms at the start of the test to avoid later GetDeltas() |
| 23 | // calls picking them up. |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 24 | std::vector<uint8_t> data_init; |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 25 | HistogramManager::GetInstance()->GetDeltas(&data_init); |
| 26 | |
| 27 | // kNoFlags filter should record all histograms. |
| 28 | UMA_HISTOGRAM_ENUMERATION("UmaHistogramManager", 1, 2); |
| 29 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 30 | std::vector<uint8_t> data; |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 31 | EXPECT_TRUE(HistogramManager::GetInstance()->GetDeltas(&data)); |
| 32 | EXPECT_FALSE(data.empty()); |
| 33 | ChromeUserMetricsExtension uma_proto; |
| 34 | EXPECT_TRUE(uma_proto.ParseFromArray( |
| 35 | reinterpret_cast<const char*>(&data[0]), data.size())); |
| 36 | EXPECT_FALSE(data.empty()); |
| 37 | |
| 38 | const HistogramEventProto& histogram_proto = |
| 39 | uma_proto.histogram_event(uma_proto.histogram_event_size() - 1); |
| 40 | ASSERT_EQ(1, histogram_proto.bucket_size()); |
| 41 | EXPECT_LE(0, histogram_proto.bucket(0).min()); |
| 42 | EXPECT_LE(2, histogram_proto.bucket(0).max()); |
| 43 | EXPECT_EQ(1, histogram_proto.bucket(0).count()); |
| 44 | |
| 45 | UMA_HISTOGRAM_ENUMERATION("UmaHistogramManager2", 2, 3); |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 46 | std::vector<uint8_t> data2; |
rtenneti | 7254696 | 2014-12-15 21:41:59 | [diff] [blame] | 47 | EXPECT_TRUE(HistogramManager::GetInstance()->GetDeltas(&data2)); |
| 48 | EXPECT_FALSE(data2.empty()); |
| 49 | ChromeUserMetricsExtension uma_proto2; |
| 50 | EXPECT_TRUE(uma_proto2.ParseFromArray( |
| 51 | reinterpret_cast<const char*>(&data2[0]), data2.size())); |
| 52 | EXPECT_FALSE(data2.empty()); |
| 53 | |
| 54 | const HistogramEventProto& histogram_proto2 = |
| 55 | uma_proto2.histogram_event(uma_proto2.histogram_event_size() - 1); |
| 56 | ASSERT_EQ(1, histogram_proto2.bucket_size()); |
| 57 | EXPECT_LE(0, histogram_proto2.bucket(0).min()); |
| 58 | EXPECT_LE(3, histogram_proto2.bucket(0).max()); |
| 59 | EXPECT_EQ(1, histogram_proto2.bucket(0).count()); |
| 60 | } |
| 61 | |
mef | c2bd1aa | 2015-04-09 03:26:53 | [diff] [blame] | 62 | } // namespace cronet |