• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2016 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 
11 #include <map>
12 #include <memory>
13 #include <string>
14 #include <utility>
15 
16 #include "rtc_base/checks.h"
17 #include "rtc_base/string_utils.h"
18 #include "system_wrappers/include/metrics.h"
19 #include "test/gtest.h"
20 
21 #if RTC_METRICS_ENABLED
22 namespace webrtc {
23 
24 namespace {
25 const int kSample = 22;
26 const char kName[] = "Name";
27 
NumSamples(absl::string_view name,const std::map<std::string,std::unique_ptr<metrics::SampleInfo>,rtc::AbslStringViewCmp> & histograms)28 int NumSamples(absl::string_view name,
29                const std::map<std::string,
30                               std::unique_ptr<metrics::SampleInfo>,
31                               rtc::AbslStringViewCmp>& histograms) {
32   const auto it = histograms.find(name);
33   if (it == histograms.end())
34     return 0;
35 
36   int num_samples = 0;
37   for (const auto& sample : it->second->samples)
38     num_samples += sample.second;
39 
40   return num_samples;
41 }
42 
NumEvents(absl::string_view name,int sample,const std::map<std::string,std::unique_ptr<metrics::SampleInfo>,rtc::AbslStringViewCmp> & histograms)43 int NumEvents(absl::string_view name,
44               int sample,
45               const std::map<std::string,
46                              std::unique_ptr<metrics::SampleInfo>,
47                              rtc::AbslStringViewCmp>& histograms) {
48   const auto it = histograms.find(name);
49   if (it == histograms.end())
50     return 0;
51 
52   const auto it_sample = it->second->samples.find(sample);
53   if (it_sample == it->second->samples.end())
54     return 0;
55 
56   return it_sample->second;
57 }
58 }  // namespace
59 
60 class MetricsDefaultTest : public ::testing::Test {
61  public:
MetricsDefaultTest()62   MetricsDefaultTest() {}
63 
64  protected:
SetUp()65   void SetUp() override { metrics::Reset(); }
66 };
67 
TEST_F(MetricsDefaultTest,Reset)68 TEST_F(MetricsDefaultTest, Reset) {
69   RTC_HISTOGRAM_PERCENTAGE(kName, kSample);
70   EXPECT_EQ(1, metrics::NumSamples(kName));
71   metrics::Reset();
72   EXPECT_EQ(0, metrics::NumSamples(kName));
73 }
74 
TEST_F(MetricsDefaultTest,NumSamples)75 TEST_F(MetricsDefaultTest, NumSamples) {
76   RTC_HISTOGRAM_PERCENTAGE(kName, 5);
77   RTC_HISTOGRAM_PERCENTAGE(kName, 5);
78   RTC_HISTOGRAM_PERCENTAGE(kName, 10);
79   EXPECT_EQ(3, metrics::NumSamples(kName));
80   EXPECT_EQ(0, metrics::NumSamples("NonExisting"));
81 }
82 
TEST_F(MetricsDefaultTest,NumEvents)83 TEST_F(MetricsDefaultTest, NumEvents) {
84   RTC_HISTOGRAM_PERCENTAGE(kName, 5);
85   RTC_HISTOGRAM_PERCENTAGE(kName, 5);
86   RTC_HISTOGRAM_PERCENTAGE(kName, 10);
87   EXPECT_EQ(2, metrics::NumEvents(kName, 5));
88   EXPECT_EQ(1, metrics::NumEvents(kName, 10));
89   EXPECT_EQ(0, metrics::NumEvents(kName, 11));
90   EXPECT_EQ(0, metrics::NumEvents("NonExisting", 5));
91 }
92 
TEST_F(MetricsDefaultTest,MinSample)93 TEST_F(MetricsDefaultTest, MinSample) {
94   RTC_HISTOGRAM_PERCENTAGE(kName, kSample);
95   RTC_HISTOGRAM_PERCENTAGE(kName, kSample + 1);
96   EXPECT_EQ(kSample, metrics::MinSample(kName));
97   EXPECT_EQ(-1, metrics::MinSample("NonExisting"));
98 }
99 
TEST_F(MetricsDefaultTest,Overflow)100 TEST_F(MetricsDefaultTest, Overflow) {
101   const std::string kName = "Overflow";
102   // Samples should end up in overflow bucket.
103   RTC_HISTOGRAM_PERCENTAGE(kName, 101);
104   EXPECT_EQ(1, metrics::NumSamples(kName));
105   EXPECT_EQ(1, metrics::NumEvents(kName, 101));
106   RTC_HISTOGRAM_PERCENTAGE(kName, 102);
107   EXPECT_EQ(2, metrics::NumSamples(kName));
108   EXPECT_EQ(2, metrics::NumEvents(kName, 101));
109 }
110 
TEST_F(MetricsDefaultTest,Underflow)111 TEST_F(MetricsDefaultTest, Underflow) {
112   const std::string kName = "Underflow";
113   // Samples should end up in underflow bucket.
114   RTC_HISTOGRAM_COUNTS_10000(kName, 0);
115   EXPECT_EQ(1, metrics::NumSamples(kName));
116   EXPECT_EQ(1, metrics::NumEvents(kName, 0));
117   RTC_HISTOGRAM_COUNTS_10000(kName, -1);
118   EXPECT_EQ(2, metrics::NumSamples(kName));
119   EXPECT_EQ(2, metrics::NumEvents(kName, 0));
120 }
121 
TEST_F(MetricsDefaultTest,GetAndReset)122 TEST_F(MetricsDefaultTest, GetAndReset) {
123   std::map<std::string, std::unique_ptr<metrics::SampleInfo>,
124            rtc::AbslStringViewCmp>
125       histograms;
126   metrics::GetAndReset(&histograms);
127   EXPECT_EQ(0u, histograms.size());
128   RTC_HISTOGRAM_PERCENTAGE("Histogram1", 4);
129   RTC_HISTOGRAM_PERCENTAGE("Histogram1", 5);
130   RTC_HISTOGRAM_PERCENTAGE("Histogram1", 5);
131   RTC_HISTOGRAM_PERCENTAGE("Histogram2", 10);
132   EXPECT_EQ(3, metrics::NumSamples("Histogram1"));
133   EXPECT_EQ(1, metrics::NumSamples("Histogram2"));
134 
135   metrics::GetAndReset(&histograms);
136   EXPECT_EQ(2u, histograms.size());
137   EXPECT_EQ(0, metrics::NumSamples("Histogram1"));
138   EXPECT_EQ(0, metrics::NumSamples("Histogram2"));
139 
140   EXPECT_EQ(3, NumSamples("Histogram1", histograms));
141   EXPECT_EQ(1, NumSamples("Histogram2", histograms));
142   EXPECT_EQ(1, NumEvents("Histogram1", 4, histograms));
143   EXPECT_EQ(2, NumEvents("Histogram1", 5, histograms));
144   EXPECT_EQ(1, NumEvents("Histogram2", 10, histograms));
145 
146   // Add samples after reset.
147   metrics::GetAndReset(&histograms);
148   EXPECT_EQ(0u, histograms.size());
149   RTC_HISTOGRAM_PERCENTAGE("Histogram1", 50);
150   RTC_HISTOGRAM_PERCENTAGE("Histogram2", 8);
151   EXPECT_EQ(1, metrics::NumSamples("Histogram1"));
152   EXPECT_EQ(1, metrics::NumSamples("Histogram2"));
153   EXPECT_EQ(1, metrics::NumEvents("Histogram1", 50));
154   EXPECT_EQ(1, metrics::NumEvents("Histogram2", 8));
155 }
156 
TEST_F(MetricsDefaultTest,TestMinMaxBucket)157 TEST_F(MetricsDefaultTest, TestMinMaxBucket) {
158   const std::string kName = "MinMaxCounts100";
159   RTC_HISTOGRAM_COUNTS_100(kName, 4);
160 
161   std::map<std::string, std::unique_ptr<metrics::SampleInfo>,
162            rtc::AbslStringViewCmp>
163       histograms;
164   metrics::GetAndReset(&histograms);
165   EXPECT_EQ(1u, histograms.size());
166   EXPECT_EQ(kName, histograms.begin()->second->name);
167   EXPECT_EQ(1, histograms.begin()->second->min);
168   EXPECT_EQ(100, histograms.begin()->second->max);
169   EXPECT_EQ(50u, histograms.begin()->second->bucket_count);
170   EXPECT_EQ(1u, histograms.begin()->second->samples.size());
171 }
172 
173 }  // namespace webrtc
174 #endif
175