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