asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "system_wrappers/include/metrics.h" |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 12 | #include "test/gmock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "test/gtest.h" |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 14 | |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 15 | using ::testing::ElementsAre; |
| 16 | using ::testing::IsEmpty; |
| 17 | using ::testing::Pair; |
| 18 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 19 | namespace webrtc { |
| 20 | namespace { |
| 21 | const int kSample = 22; |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 22 | |
| 23 | void AddSparseSample(const std::string& name, int sample) { |
| 24 | RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample); |
| 25 | } |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 26 | void AddSampleWithVaryingName(int index, const std::string& name, int sample) { |
| 27 | RTC_HISTOGRAMS_COUNTS_100(index, name, sample); |
| 28 | } |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 29 | } // namespace |
| 30 | |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 31 | class MetricsTest : public ::testing::Test { |
| 32 | public: |
| 33 | MetricsTest() {} |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 34 | |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 35 | protected: |
Mirko Bonadei | c14d9bb | 2018-07-16 15:44:28 +0200 | [diff] [blame] | 36 | void SetUp() override { metrics::Reset(); } |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | TEST_F(MetricsTest, InitiallyNoSamples) { |
| 40 | EXPECT_EQ(0, metrics::NumSamples("NonExisting")); |
| 41 | EXPECT_EQ(0, metrics::NumEvents("NonExisting", kSample)); |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 42 | EXPECT_THAT(metrics::Samples("NonExisting"), IsEmpty()); |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 43 | } |
| 44 | |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 45 | TEST_F(MetricsTest, RtcHistogramPercent_AddSample) { |
| 46 | const std::string kName = "Percentage"; |
| 47 | RTC_HISTOGRAM_PERCENTAGE(kName, kSample); |
| 48 | EXPECT_EQ(1, metrics::NumSamples(kName)); |
| 49 | EXPECT_EQ(1, metrics::NumEvents(kName, kSample)); |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 50 | EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1))); |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | TEST_F(MetricsTest, RtcHistogramEnumeration_AddSample) { |
| 54 | const std::string kName = "Enumeration"; |
| 55 | RTC_HISTOGRAM_ENUMERATION(kName, kSample, kSample + 1); |
| 56 | EXPECT_EQ(1, metrics::NumSamples(kName)); |
| 57 | EXPECT_EQ(1, metrics::NumEvents(kName, kSample)); |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 58 | EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1))); |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 59 | } |
| 60 | |
asapersson | 1731c9c | 2016-11-30 00:29:09 -0800 | [diff] [blame] | 61 | TEST_F(MetricsTest, RtcHistogramBoolean_AddSample) { |
| 62 | const std::string kName = "Boolean"; |
| 63 | const int kSample = 0; |
| 64 | RTC_HISTOGRAM_BOOLEAN(kName, kSample); |
| 65 | EXPECT_EQ(1, metrics::NumSamples(kName)); |
| 66 | EXPECT_EQ(1, metrics::NumEvents(kName, kSample)); |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 67 | EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1))); |
asapersson | 1731c9c | 2016-11-30 00:29:09 -0800 | [diff] [blame] | 68 | } |
| 69 | |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 70 | TEST_F(MetricsTest, RtcHistogramCountsSparse_AddSample) { |
| 71 | const std::string kName = "CountsSparse100"; |
| 72 | RTC_HISTOGRAM_COUNTS_SPARSE_100(kName, kSample); |
| 73 | EXPECT_EQ(1, metrics::NumSamples(kName)); |
| 74 | EXPECT_EQ(1, metrics::NumEvents(kName, kSample)); |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 75 | EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1))); |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST_F(MetricsTest, RtcHistogramCounts_AddSample) { |
| 79 | const std::string kName = "Counts100"; |
| 80 | RTC_HISTOGRAM_COUNTS_100(kName, kSample); |
| 81 | EXPECT_EQ(1, metrics::NumSamples(kName)); |
| 82 | EXPECT_EQ(1, metrics::NumEvents(kName, kSample)); |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 83 | EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1))); |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST_F(MetricsTest, RtcHistogramCounts_AddMultipleSamples) { |
| 87 | const std::string kName = "Counts200"; |
| 88 | const int kNumSamples = 10; |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 89 | std::map<int, int> samples; |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 90 | for (int i = 1; i <= kNumSamples; ++i) { |
| 91 | RTC_HISTOGRAM_COUNTS_200(kName, i); |
| 92 | EXPECT_EQ(1, metrics::NumEvents(kName, i)); |
| 93 | EXPECT_EQ(i, metrics::NumSamples(kName)); |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 94 | samples[i] = 1; |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 95 | } |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 96 | EXPECT_EQ(samples, metrics::Samples(kName)); |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_F(MetricsTest, RtcHistogramsCounts_AddSample) { |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 100 | AddSampleWithVaryingName(0, "Name1", kSample); |
| 101 | AddSampleWithVaryingName(1, "Name2", kSample + 1); |
| 102 | AddSampleWithVaryingName(2, "Name3", kSample + 2); |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 103 | EXPECT_EQ(1, metrics::NumSamples("Name1")); |
| 104 | EXPECT_EQ(1, metrics::NumSamples("Name2")); |
| 105 | EXPECT_EQ(1, metrics::NumSamples("Name3")); |
| 106 | EXPECT_EQ(1, metrics::NumEvents("Name1", kSample + 0)); |
| 107 | EXPECT_EQ(1, metrics::NumEvents("Name2", kSample + 1)); |
| 108 | EXPECT_EQ(1, metrics::NumEvents("Name3", kSample + 2)); |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 109 | EXPECT_THAT(metrics::Samples("Name1"), ElementsAre(Pair(kSample + 0, 1))); |
| 110 | EXPECT_THAT(metrics::Samples("Name2"), ElementsAre(Pair(kSample + 1, 1))); |
| 111 | EXPECT_THAT(metrics::Samples("Name3"), ElementsAre(Pair(kSample + 2, 1))); |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 115 | TEST_F(MetricsTest, RtcHistogramsCounts_InvalidIndex) { |
| 116 | EXPECT_DEATH(RTC_HISTOGRAMS_COUNTS_1000(-1, "Name", kSample), ""); |
| 117 | EXPECT_DEATH(RTC_HISTOGRAMS_COUNTS_1000(3, "Name", kSample), ""); |
| 118 | EXPECT_DEATH(RTC_HISTOGRAMS_COUNTS_1000(3u, "Name", kSample), ""); |
asapersson | 040b79f | 2016-02-02 07:13:01 -0800 | [diff] [blame] | 119 | } |
| 120 | #endif |
| 121 | |
asapersson | 01d70a3 | 2016-05-20 06:29:46 -0700 | [diff] [blame] | 122 | TEST_F(MetricsTest, RtcHistogramSparse_NonConstantNameWorks) { |
| 123 | AddSparseSample("Sparse1", kSample); |
| 124 | AddSparseSample("Sparse2", kSample); |
| 125 | EXPECT_EQ(1, metrics::NumSamples("Sparse1")); |
| 126 | EXPECT_EQ(1, metrics::NumSamples("Sparse2")); |
Steve Anton | c1e6e86 | 2019-03-04 14:43:44 -0800 | [diff] [blame] | 127 | EXPECT_THAT(metrics::Samples("Sparse1"), ElementsAre(Pair(kSample, 1))); |
| 128 | EXPECT_THAT(metrics::Samples("Sparse2"), ElementsAre(Pair(kSample, 1))); |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 129 | } |
| 130 | |
asapersson | 1fe48a5 | 2016-01-07 01:02:42 -0800 | [diff] [blame] | 131 | } // namespace webrtc |