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 173 174 175 176 177 178 179 180 181 182 183
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/metrics/histogram_macros.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
TEST(ScopedHistogramTimer, ThreeTimersOneScope) {
SCOPED_UMA_HISTOGRAM_TIMER_MICROS("TestShortTimer0");
SCOPED_UMA_HISTOGRAM_TIMER_MICROS("TestShortTimer1");
SCOPED_UMA_HISTOGRAM_TIMER("TestTimer0");
SCOPED_UMA_HISTOGRAM_TIMER("TestTimer1");
SCOPED_UMA_HISTOGRAM_LONG_TIMER("TestLongTimer0");
SCOPED_UMA_HISTOGRAM_LONG_TIMER("TestLongTimer1");
}
// Compile tests for UMA_HISTOGRAM_ENUMERATION with the three different types it
// accepts:
// - integral types
// - unscoped enums
// - scoped enums
TEST(HistogramMacro, IntegralPseudoEnumeration) {
UMA_HISTOGRAM_ENUMERATION("Test.FauxEnumeration", 1, 1000);
}
TEST(HistogramMacro, UnscopedEnumeration) {
enum TestEnum : char {
FIRST_VALUE,
SECOND_VALUE,
THIRD_VALUE,
MAX_ENTRIES,
};
UMA_HISTOGRAM_ENUMERATION("Test.UnscopedEnumeration", SECOND_VALUE,
MAX_ENTRIES);
}
TEST(HistogramMacro, ScopedEnumeration) {
enum class TestEnum {
FIRST_VALUE,
SECOND_VALUE,
THIRD_VALUE,
kMaxValue = THIRD_VALUE,
};
UMA_HISTOGRAM_ENUMERATION("Test.ScopedEnumeration", TestEnum::FIRST_VALUE);
enum class TestEnum2 {
FIRST_VALUE,
SECOND_VALUE,
THIRD_VALUE,
MAX_ENTRIES,
};
UMA_HISTOGRAM_ENUMERATION("Test.ScopedEnumeration2", TestEnum2::SECOND_VALUE,
TestEnum2::MAX_ENTRIES);
}
// Compile tests for UMA_HISTOGRAM_ENUMERATION when the value type is:
// - a const reference to an enum
// - a non-const reference to an enum
TEST(HistogramMacro, EnumerationConstRef) {
enum class TestEnum { kValue, kMaxValue = kValue };
const TestEnum& value_ref = TestEnum::kValue;
UMA_HISTOGRAM_ENUMERATION("Test.ScopedEnumeration3", value_ref);
}
TEST(HistogramMacro, EnumerationNonConstRef) {
enum class TestEnum { kValue, kMaxValue = kValue };
TestEnum value = TestEnum::kValue;
TestEnum& value_ref = value;
UMA_HISTOGRAM_ENUMERATION("Test.ScopedEnumeration4", value_ref);
}
TEST(HistogramMacro, SplitByProcessPriorityMacro) {
TimeTicks mock_now = TimeTicks::Now();
constexpr TimeDelta kMockIntervalBetweenSamples = Seconds(1);
{
// No BestEffort suffix by default (SetSharedLastForegroundTimeForMetrics
// not invoked yet in this process).
HistogramTester tester;
UMA_HISTOGRAM_SPLIT_BY_PROCESS_PRIORITY(UMA_HISTOGRAM_COUNTS_1000, mock_now,
Microseconds(0), "Test.MyCount",
123);
tester.ExpectUniqueSample("Test.MyCount", 123, 1);
tester.ExpectUniqueSample("Test.MyCount.BestEffort", 123, 0);
}
mock_now += kMockIntervalBetweenSamples;
std::atomic<base::TimeTicks> shared_last_foreground_time = mock_now;
internal::SetSharedLastForegroundTimeForMetrics(&shared_last_foreground_time);
{
// No BestEffort suffix while foreground.
HistogramTester tester;
UMA_HISTOGRAM_SPLIT_BY_PROCESS_PRIORITY(UMA_HISTOGRAM_COUNTS_1000, mock_now,
Microseconds(0), "Test.MyCount",
123);
tester.ExpectUniqueSample("Test.MyCount", 123, 1);
tester.ExpectUniqueSample("Test.MyCount.BestEffort", 123, 0);
}
mock_now += kMockIntervalBetweenSamples;
shared_last_foreground_time.store(TimeTicks(), std::memory_order_relaxed);
{
// BestEffort suffix while in BestEffort.
HistogramTester tester;
UMA_HISTOGRAM_SPLIT_BY_PROCESS_PRIORITY(UMA_HISTOGRAM_COUNTS_1000, mock_now,
Microseconds(0), "Test.MyCount",
123);
tester.ExpectUniqueSample("Test.MyCount", 123, 0);
tester.ExpectUniqueSample("Test.MyCount.BestEffort", 123, 1);
}
mock_now += kMockIntervalBetweenSamples;
shared_last_foreground_time.store(mock_now, std::memory_order_relaxed);
{
// No BestEffort suffix once back to normal priority.
HistogramTester tester;
UMA_HISTOGRAM_SPLIT_BY_PROCESS_PRIORITY(UMA_HISTOGRAM_COUNTS_1000, mock_now,
Microseconds(0), "Test.MyCount",
123);
tester.ExpectUniqueSample("Test.MyCount", 123, 1);
tester.ExpectUniqueSample("Test.MyCount.BestEffort", 123, 0);
}
{
// BestEffort suffix while normal priority if the sample_interval overlaps
// into a BestEffort range.
HistogramTester tester;
UMA_HISTOGRAM_SPLIT_BY_PROCESS_PRIORITY(UMA_HISTOGRAM_COUNTS_1000, mock_now,
kMockIntervalBetweenSamples * 1.5,
"Test.MyCount", 123);
tester.ExpectUniqueSample("Test.MyCount", 123, 0);
tester.ExpectUniqueSample("Test.MyCount.BestEffort", 123, 1);
}
{
HistogramTester tester;
// ... and also if the sample was taken at a time before the last foreground
// time:
UMA_HISTOGRAM_SPLIT_BY_PROCESS_PRIORITY(
UMA_HISTOGRAM_COUNTS_1000, mock_now - kMockIntervalBetweenSamples * 1.5,
Microseconds(0), "Test.MyCount", 123);
tester.ExpectUniqueSample("Test.MyCount", 123, 0);
tester.ExpectUniqueSample("Test.MyCount.BestEffort", 123, 1);
}
// Reset the global state at the end of the test.
internal::SetSharedLastForegroundTimeForMetrics(nullptr);
{
// No BestEffort suffix by default, again.
HistogramTester tester;
UMA_HISTOGRAM_SPLIT_BY_PROCESS_PRIORITY(UMA_HISTOGRAM_COUNTS_1000, mock_now,
Microseconds(0), "Test.MyCount",
123);
tester.ExpectUniqueSample("Test.MyCount", 123, 1);
tester.ExpectUniqueSample("Test.MyCount.BestEffort", 123, 0);
}
}
TEST(HistogramMacro, TimerSubsamplingMacro) {
HistogramTester tester;
// Sampling the histogram should increase the total count.
{
SCOPED_UMA_HISTOGRAM_TIMER_MICROS_SUBSAMPLED("Test.SubsampledTimer", true);
}
tester.ExpectTotalCount("Test.SubsampledTimer", 1);
{
SCOPED_UMA_HISTOGRAM_TIMER_MICROS_SUBSAMPLED("Test.SubsampledTimer", false);
}
// Inversely, not sampling the histogram should not increase the total count.
tester.ExpectTotalCount("Test.SubsampledTimer", 1);
}
} // namespace base
|