Add helper macros for calling a histogram with different names.

To be used when a metric is used in different modes such as real-time vs screenshare (will be done in https://codereview.webrtc.org/1564923008/).

BUG=webrtc:5283

Review URL: https://codereview.webrtc.org/1567013004

Cr-Commit-Position: refs/heads/master@{#11461}
This commit is contained in:
asapersson 2016-02-02 07:13:01 -08:00 committed by Commit bot
parent ed3277bf14
commit 040b79ff7e
2 changed files with 76 additions and 0 deletions

View File

@ -153,6 +153,58 @@
webrtc::metrics::HistogramAdd(histogram_pointer, name, sample); \
} while (0)
// Helper macros.
// Macros for calling a histogram with varying name (e.g. when using a metric
// in different modes such as real-time vs screenshare).
#define RTC_HISTOGRAMS_COUNTS_100(index, name, sample) \
RTC_HISTOGRAMS_COMMON(index, name, sample, \
RTC_HISTOGRAM_COUNTS(name, sample, 1, 100, 50))
#define RTC_HISTOGRAMS_COUNTS_200(index, name, sample) \
RTC_HISTOGRAMS_COMMON(index, name, sample, \
RTC_HISTOGRAM_COUNTS(name, sample, 1, 200, 50))
#define RTC_HISTOGRAMS_COUNTS_1000(index, name, sample) \
RTC_HISTOGRAMS_COMMON(index, name, sample, \
RTC_HISTOGRAM_COUNTS(name, sample, 1, 1000, 50))
#define RTC_HISTOGRAMS_COUNTS_10000(index, name, sample) \
RTC_HISTOGRAMS_COMMON(index, name, sample, \
RTC_HISTOGRAM_COUNTS(name, sample, 1, 10000, 50))
#define RTC_HISTOGRAMS_COUNTS_100000(index, name, sample) \
RTC_HISTOGRAMS_COMMON(index, name, sample, \
RTC_HISTOGRAM_COUNTS(name, sample, 1, 100000, 50))
#define RTC_HISTOGRAMS_ENUMERATION(index, name, sample, boundary) \
RTC_HISTOGRAMS_COMMON(index, name, sample, \
RTC_HISTOGRAM_ENUMERATION(name, sample, boundary))
#define RTC_HISTOGRAMS_PERCENTAGE(index, name, sample) \
RTC_HISTOGRAMS_COMMON(index, name, sample, \
RTC_HISTOGRAM_PERCENTAGE(name, sample))
#define RTC_HISTOGRAMS_COMMON(index, name, sample, macro_invocation) \
do { \
RTC_DCHECK(index >= 0); \
RTC_DCHECK(index <= 2); \
switch (index) { \
case 0: \
macro_invocation; \
break; \
case 1: \
macro_invocation; \
break; \
case 2: \
macro_invocation; \
break; \
default: \
RTC_NOTREACHED(); \
} \
} while (0)
namespace webrtc {
namespace metrics {

View File

@ -21,6 +21,9 @@ const std::string kName = "Name";
void AddSparseSample(const std::string& name, int sample) {
RTC_HISTOGRAM_COUNTS_SPARSE_100(name, sample);
}
void AddSampleWithVaryingName(int index, const std::string& name, int sample) {
RTC_HISTOGRAMS_COUNTS_100(index, name, sample);
}
#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
void AddSample(const std::string& name, int sample) {
RTC_HISTOGRAM_COUNTS_100(name, sample);
@ -72,6 +75,27 @@ TEST(MetricsTest, RtcHistogramCounts_AddMultipleSamples) {
EXPECT_EQ(kNumSamples - 1, test::LastHistogramSample(kName));
}
TEST(MetricsTest, RtcHistogramsCounts_AddSample) {
test::ClearHistograms();
AddSampleWithVaryingName(0, "Name1", kSample);
AddSampleWithVaryingName(1, "Name2", kSample + 1);
AddSampleWithVaryingName(2, "Name3", kSample + 2);
EXPECT_EQ(1, test::NumHistogramSamples("Name1"));
EXPECT_EQ(1, test::NumHistogramSamples("Name2"));
EXPECT_EQ(1, test::NumHistogramSamples("Name3"));
EXPECT_EQ(kSample + 0, test::LastHistogramSample("Name1"));
EXPECT_EQ(kSample + 1, test::LastHistogramSample("Name2"));
EXPECT_EQ(kSample + 2, test::LastHistogramSample("Name3"));
}
#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(MetricsTest, RtcHistogramsCounts_InvalidIndex) {
EXPECT_DEATH(RTC_HISTOGRAMS_COUNTS_1000(-1, kName, kSample), "");
EXPECT_DEATH(RTC_HISTOGRAMS_COUNTS_1000(3, kName, kSample), "");
EXPECT_DEATH(RTC_HISTOGRAMS_COUNTS_1000(3u, kName, kSample), "");
}
#endif
TEST(MetricsTest, RtcHistogramSparse_NonConstantNameWorks) {
test::ClearHistograms();
AddSparseSample("Name1", kSample);