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 
11 #include "system_wrappers/include/metrics.h"
12 
13 #include "test/gmock.h"
14 #include "test/gtest.h"
15 
16 using ::testing::ElementsAre;
17 using ::testing::IsEmpty;
18 using ::testing::Pair;
19 
20 #if RTC_METRICS_ENABLED
21 namespace webrtc {
22 namespace {
23 const int kSample = 22;
24 
AddSparseSample(const std::string & name,int sample)25 void AddSparseSample(const std::string& name, int sample) {
26   RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample);
27 }
AddSampleWithVaryingName(int index,const std::string & name,int sample)28 void AddSampleWithVaryingName(int index, const std::string& name, int sample) {
29   RTC_HISTOGRAMS_COUNTS_100(index, name, sample);
30 }
31 }  // namespace
32 
33 class MetricsTest : public ::testing::Test {
34  public:
MetricsTest()35   MetricsTest() {}
36 
37  protected:
SetUp()38   void SetUp() override { metrics::Reset(); }
39 };
40 
TEST_F(MetricsTest,InitiallyNoSamples)41 TEST_F(MetricsTest, InitiallyNoSamples) {
42   EXPECT_EQ(0, metrics::NumSamples("NonExisting"));
43   EXPECT_EQ(0, metrics::NumEvents("NonExisting", kSample));
44   EXPECT_THAT(metrics::Samples("NonExisting"), IsEmpty());
45 }
46 
TEST_F(MetricsTest,RtcHistogramPercent_AddSample)47 TEST_F(MetricsTest, RtcHistogramPercent_AddSample) {
48   const std::string kName = "Percentage";
49   RTC_HISTOGRAM_PERCENTAGE(kName, kSample);
50   EXPECT_EQ(1, metrics::NumSamples(kName));
51   EXPECT_EQ(1, metrics::NumEvents(kName, kSample));
52   EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1)));
53 }
54 
TEST_F(MetricsTest,RtcHistogramEnumeration_AddSample)55 TEST_F(MetricsTest, RtcHistogramEnumeration_AddSample) {
56   const std::string kName = "Enumeration";
57   RTC_HISTOGRAM_ENUMERATION(kName, kSample, kSample + 1);
58   EXPECT_EQ(1, metrics::NumSamples(kName));
59   EXPECT_EQ(1, metrics::NumEvents(kName, kSample));
60   EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1)));
61 }
62 
TEST_F(MetricsTest,RtcHistogramBoolean_AddSample)63 TEST_F(MetricsTest, RtcHistogramBoolean_AddSample) {
64   const std::string kName = "Boolean";
65   const int kSample = 0;
66   RTC_HISTOGRAM_BOOLEAN(kName, kSample);
67   EXPECT_EQ(1, metrics::NumSamples(kName));
68   EXPECT_EQ(1, metrics::NumEvents(kName, kSample));
69   EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1)));
70 }
71 
TEST_F(MetricsTest,RtcHistogramCountsSparse_AddSample)72 TEST_F(MetricsTest, RtcHistogramCountsSparse_AddSample) {
73   const std::string kName = "CountsSparse100";
74   RTC_HISTOGRAM_COUNTS_SPARSE_100(kName, kSample);
75   EXPECT_EQ(1, metrics::NumSamples(kName));
76   EXPECT_EQ(1, metrics::NumEvents(kName, kSample));
77   EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1)));
78 }
79 
TEST_F(MetricsTest,RtcHistogramCounts_AddSample)80 TEST_F(MetricsTest, RtcHistogramCounts_AddSample) {
81   const std::string kName = "Counts100";
82   RTC_HISTOGRAM_COUNTS_100(kName, kSample);
83   EXPECT_EQ(1, metrics::NumSamples(kName));
84   EXPECT_EQ(1, metrics::NumEvents(kName, kSample));
85   EXPECT_THAT(metrics::Samples(kName), ElementsAre(Pair(kSample, 1)));
86 }
87 
TEST_F(MetricsTest,RtcHistogramCounts_AddMultipleSamples)88 TEST_F(MetricsTest, RtcHistogramCounts_AddMultipleSamples) {
89   const std::string kName = "Counts200";
90   const int kNumSamples = 10;
91   std::map<int, int> samples;
92   for (int i = 1; i <= kNumSamples; ++i) {
93     RTC_HISTOGRAM_COUNTS_200(kName, i);
94     EXPECT_EQ(1, metrics::NumEvents(kName, i));
95     EXPECT_EQ(i, metrics::NumSamples(kName));
96     samples[i] = 1;
97   }
98   EXPECT_EQ(samples, metrics::Samples(kName));
99 }
100 
TEST_F(MetricsTest,RtcHistogramsCounts_AddSample)101 TEST_F(MetricsTest, RtcHistogramsCounts_AddSample) {
102   AddSampleWithVaryingName(0, "Name1", kSample);
103   AddSampleWithVaryingName(1, "Name2", kSample + 1);
104   AddSampleWithVaryingName(2, "Name3", kSample + 2);
105   EXPECT_EQ(1, metrics::NumSamples("Name1"));
106   EXPECT_EQ(1, metrics::NumSamples("Name2"));
107   EXPECT_EQ(1, metrics::NumSamples("Name3"));
108   EXPECT_EQ(1, metrics::NumEvents("Name1", kSample + 0));
109   EXPECT_EQ(1, metrics::NumEvents("Name2", kSample + 1));
110   EXPECT_EQ(1, metrics::NumEvents("Name3", kSample + 2));
111   EXPECT_THAT(metrics::Samples("Name1"), ElementsAre(Pair(kSample + 0, 1)));
112   EXPECT_THAT(metrics::Samples("Name2"), ElementsAre(Pair(kSample + 1, 1)));
113   EXPECT_THAT(metrics::Samples("Name3"), ElementsAre(Pair(kSample + 2, 1)));
114 }
115 
116 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
117 using MetricsDeathTest = MetricsTest;
TEST_F(MetricsDeathTest,RtcHistogramsCounts_InvalidIndex)118 TEST_F(MetricsDeathTest, RtcHistogramsCounts_InvalidIndex) {
119   EXPECT_DEATH(RTC_HISTOGRAMS_COUNTS_1000(-1, "Name", kSample), "");
120   EXPECT_DEATH(RTC_HISTOGRAMS_COUNTS_1000(3, "Name", kSample), "");
121   EXPECT_DEATH(RTC_HISTOGRAMS_COUNTS_1000(3u, "Name", kSample), "");
122 }
123 #endif
124 
TEST_F(MetricsTest,RtcHistogramSparse_NonConstantNameWorks)125 TEST_F(MetricsTest, RtcHistogramSparse_NonConstantNameWorks) {
126   AddSparseSample("Sparse1", kSample);
127   AddSparseSample("Sparse2", kSample);
128   EXPECT_EQ(1, metrics::NumSamples("Sparse1"));
129   EXPECT_EQ(1, metrics::NumSamples("Sparse2"));
130   EXPECT_THAT(metrics::Samples("Sparse1"), ElementsAre(Pair(kSample, 1)));
131   EXPECT_THAT(metrics::Samples("Sparse2"), ElementsAre(Pair(kSample, 1)));
132 }
133 
134 }  // namespace webrtc
135 #endif
136