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