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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
// Copyright 2012 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_base.h"
#include <limits>
#include <string_view>
#include <vector>
#include "base/metrics/histogram.h"
#include "base/metrics/sample_vector.h"
#include "base/metrics/sparse_histogram.h"
#include "base/metrics/statistics_recorder.h"
#include "base/pickle.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
class HistogramBaseTest : public testing::Test {
public:
HistogramBaseTest() {
// Each test will have a clean state (no Histogram / BucketRanges
// registered).
ResetStatisticsRecorder();
}
HistogramBaseTest(const HistogramBaseTest&) = delete;
HistogramBaseTest& operator=(const HistogramBaseTest&) = delete;
~HistogramBaseTest() override = default;
protected:
void ResetStatisticsRecorder() {
// It is necessary to fully destruct any existing StatisticsRecorder
// before creating a new one.
statistics_recorder_.reset();
statistics_recorder_ = StatisticsRecorder::CreateTemporaryForTesting();
}
private:
std::unique_ptr<StatisticsRecorder> statistics_recorder_;
};
TEST_F(HistogramBaseTest, DeserializeHistogram) {
HistogramBase* histogram =
Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
(HistogramBase::kUmaTargetedHistogramFlag |
HistogramBase::kIPCSerializationSourceFlag));
Pickle pickle;
histogram->SerializeInfo(&pickle);
PickleIterator iter(pickle);
HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
EXPECT_EQ(histogram, deserialized);
ResetStatisticsRecorder();
PickleIterator iter2(pickle);
deserialized = DeserializeHistogramInfo(&iter2);
EXPECT_TRUE(deserialized);
EXPECT_NE(histogram, deserialized);
EXPECT_EQ("TestHistogram", deserialized->histogram_name());
EXPECT_TRUE(deserialized->HasConstructionArguments(1, 1000, 10));
// kIPCSerializationSourceFlag will be cleared.
EXPECT_EQ(HistogramBase::kUmaTargetedHistogramFlag, deserialized->flags());
}
TEST_F(HistogramBaseTest, DeserializeLinearHistogram) {
HistogramBase* histogram = LinearHistogram::FactoryGet(
"TestHistogram", 1, 1000, 10, HistogramBase::kIPCSerializationSourceFlag);
Pickle pickle;
histogram->SerializeInfo(&pickle);
PickleIterator iter(pickle);
HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
EXPECT_EQ(histogram, deserialized);
ResetStatisticsRecorder();
PickleIterator iter2(pickle);
deserialized = DeserializeHistogramInfo(&iter2);
EXPECT_TRUE(deserialized);
EXPECT_NE(histogram, deserialized);
EXPECT_EQ("TestHistogram", deserialized->histogram_name());
EXPECT_TRUE(deserialized->HasConstructionArguments(1, 1000, 10));
EXPECT_EQ(0, deserialized->flags());
}
TEST_F(HistogramBaseTest, DeserializeBooleanHistogram) {
HistogramBase* histogram = BooleanHistogram::FactoryGet(
"TestHistogram", HistogramBase::kIPCSerializationSourceFlag);
Pickle pickle;
histogram->SerializeInfo(&pickle);
PickleIterator iter(pickle);
HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
EXPECT_EQ(histogram, deserialized);
ResetStatisticsRecorder();
PickleIterator iter2(pickle);
deserialized = DeserializeHistogramInfo(&iter2);
EXPECT_TRUE(deserialized);
EXPECT_NE(histogram, deserialized);
EXPECT_EQ("TestHistogram", deserialized->histogram_name());
EXPECT_TRUE(deserialized->HasConstructionArguments(1, 2, 3));
EXPECT_EQ(0, deserialized->flags());
}
TEST_F(HistogramBaseTest, DeserializeCustomHistogram) {
std::vector<HistogramBase::Sample32> ranges;
ranges.push_back(13);
ranges.push_back(5);
ranges.push_back(9);
HistogramBase* histogram = CustomHistogram::FactoryGet(
"TestHistogram", ranges, HistogramBase::kIPCSerializationSourceFlag);
Pickle pickle;
histogram->SerializeInfo(&pickle);
PickleIterator iter(pickle);
HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
EXPECT_EQ(histogram, deserialized);
ResetStatisticsRecorder();
PickleIterator iter2(pickle);
deserialized = DeserializeHistogramInfo(&iter2);
EXPECT_TRUE(deserialized);
EXPECT_NE(histogram, deserialized);
EXPECT_EQ("TestHistogram", deserialized->histogram_name());
EXPECT_TRUE(deserialized->HasConstructionArguments(5, 13, 4));
EXPECT_EQ(0, deserialized->flags());
}
TEST_F(HistogramBaseTest, DeserializeSparseHistogram) {
HistogramBase* histogram = SparseHistogram::FactoryGet(
"TestHistogram", HistogramBase::kIPCSerializationSourceFlag);
Pickle pickle;
histogram->SerializeInfo(&pickle);
PickleIterator iter(pickle);
HistogramBase* deserialized = DeserializeHistogramInfo(&iter);
EXPECT_EQ(histogram, deserialized);
ResetStatisticsRecorder();
PickleIterator iter2(pickle);
deserialized = DeserializeHistogramInfo(&iter2);
EXPECT_TRUE(deserialized);
EXPECT_NE(histogram, deserialized);
EXPECT_EQ("TestHistogram", deserialized->histogram_name());
EXPECT_EQ(0, deserialized->flags());
}
TEST_F(HistogramBaseTest, AddKilo) {
HistogramBase* histogram =
LinearHistogram::FactoryGet("TestAddKiloHistogram", 1, 1000, 100, 0);
histogram->AddKilo(100, 1000);
histogram->AddKilo(200, 2000);
histogram->AddKilo(300, 1500);
std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
EXPECT_EQ(1, samples->GetCount(100));
EXPECT_EQ(2, samples->GetCount(200));
EXPECT_LE(1, samples->GetCount(300));
EXPECT_GE(2, samples->GetCount(300));
}
TEST_F(HistogramBaseTest, AddKiB) {
HistogramBase* histogram =
LinearHistogram::FactoryGet("TestAddKiBHistogram", 1, 1000, 100, 0);
histogram->AddKiB(100, 1024);
histogram->AddKiB(200, 2048);
histogram->AddKiB(300, 1536);
std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
EXPECT_EQ(1, samples->GetCount(100));
EXPECT_EQ(2, samples->GetCount(200));
EXPECT_LE(1, samples->GetCount(300));
EXPECT_GE(2, samples->GetCount(300));
}
TEST_F(HistogramBaseTest, AddTimeMillisecondsGranularityOverflow) {
const HistogramBase::Sample32 sample_max =
std::numeric_limits<HistogramBase::Sample32>::max() / 2;
HistogramBase* histogram = LinearHistogram::FactoryGet(
"TestAddTimeMillisecondsGranularity1", 1, sample_max, 100, 0);
int64_t large_positive = std::numeric_limits<int64_t>::max();
// |add_count| is the number of large values that have been added to the
// histogram. We consider a number to be 'large' if it cannot be represented
// in a HistogramBase::Sample32.
int add_count = 0;
while (large_positive > std::numeric_limits<HistogramBase::Sample32>::max()) {
// Add the TimeDelta corresponding to |large_positive| milliseconds to the
// histogram.
histogram->AddTimeMillisecondsGranularity(Milliseconds(large_positive));
++add_count;
// Reduce the value of |large_positive|. The choice of 7 here is
// arbitrary.
large_positive /= 7;
}
std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
// All of the reported values must have gone into the max overflow bucket.
EXPECT_EQ(add_count, samples->GetCount(sample_max));
// We now perform the analoguous operations, now with negative values with a
// large absolute value.
histogram = LinearHistogram::FactoryGet("TestAddTimeMillisecondsGranularity2",
1, sample_max, 100, 0);
int64_t large_negative = std::numeric_limits<int64_t>::min();
add_count = 0;
while (large_negative < std::numeric_limits<HistogramBase::Sample32>::min()) {
histogram->AddTimeMillisecondsGranularity(Milliseconds(large_negative));
++add_count;
large_negative /= 7;
}
samples = histogram->SnapshotSamples();
// All of the reported values must have gone into the min overflow bucket.
EXPECT_EQ(add_count, samples->GetCount(0));
}
TEST_F(HistogramBaseTest, AddTimeMicrosecondsGranularityOverflow) {
// Nothing to test if we don't have a high resolution clock.
if (!TimeTicks::IsHighResolution()) {
return;
}
const HistogramBase::Sample32 sample_max =
std::numeric_limits<HistogramBase::Sample32>::max() / 2;
HistogramBase* histogram = LinearHistogram::FactoryGet(
"TestAddTimeMicrosecondsGranularity1", 1, sample_max, 100, 0);
int64_t large_positive = std::numeric_limits<int64_t>::max();
// |add_count| is the number of large values that have been added to the
// histogram. We consider a number to be 'large' if it cannot be represented
// in a HistogramBase::Sample32.
int add_count = 0;
while (large_positive > std::numeric_limits<HistogramBase::Sample32>::max()) {
// Add the TimeDelta corresponding to |large_positive| microseconds to the
// histogram.
histogram->AddTimeMicrosecondsGranularity(Microseconds(large_positive));
++add_count;
// Reduce the value of |large_positive|. The choice of 7 here is
// arbitrary.
large_positive /= 7;
}
std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
// All of the reported values must have gone into the max overflow bucket.
EXPECT_EQ(add_count, samples->GetCount(sample_max));
// We now perform the analoguous operations, now with negative values with a
// large absolute value.
histogram = LinearHistogram::FactoryGet("TestAddTimeMicrosecondsGranularity2",
1, sample_max, 100, 0);
int64_t large_negative = std::numeric_limits<int64_t>::min();
add_count = 0;
while (large_negative < std::numeric_limits<HistogramBase::Sample32>::min()) {
histogram->AddTimeMicrosecondsGranularity(Microseconds(large_negative));
++add_count;
large_negative /= 7;
}
samples = histogram->SnapshotSamples();
// All of the reported values must have gone into the min overflow bucket.
EXPECT_EQ(add_count, samples->GetCount(0));
}
} // namespace base
|