Commit 21211fb5 authored by Tom McKee's avatar Tom McKee Committed by Commit Bot

Make HistogramTester treat empty histograms uniformly.

Some of the Expect* methods of HistogramTester fail if the queried histogram
has no data. Other methods allow querying empty histograms and treat them as a
collection of empty buckets.

Failing while inspecting empty histograms can make it awkward to verify certain
test cases. For example, to check that a particular test case does not write to
a particular bucket with ExpectBucketCount, a write to a separate bucket would
be required. Alternatively, just asserting that the histogram is empty isn't
quite right either because the test case would fail if an unrelated bucket
happened to be updated.

This CL changes HistogramTester to be consistent about what is queryable by
treating empty histograms as collections of empty buckets.

Change-Id: Id97356edb0bf983ff9487a2b749e028147400a97
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1879608Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Commit-Queue: Tom McKee <tommckee@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710938}
parent 7c8d069c
......@@ -34,13 +34,14 @@ void HistogramTester::ExpectUniqueSample(
HistogramBase::Sample sample,
HistogramBase::Count expected_count) const {
HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
EXPECT_NE(nullptr, histogram)
<< "Histogram \"" << name << "\" does not exist.";
if (histogram) {
std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
CheckBucketCount(name, sample, expected_count, *samples);
CheckTotalCount(name, expected_count, *samples);
} else {
// No histogram means there were zero samples.
EXPECT_EQ(0, expected_count)
<< "Histogram \"" << name << "\" does not exist.";
}
}
......@@ -49,12 +50,13 @@ void HistogramTester::ExpectBucketCount(
HistogramBase::Sample sample,
HistogramBase::Count expected_count) const {
HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
EXPECT_NE(nullptr, histogram)
<< "Histogram \"" << name << "\" does not exist.";
if (histogram) {
std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
CheckBucketCount(name, sample, expected_count, *samples);
} else {
// No histogram means there were zero samples.
EXPECT_EQ(0, expected_count)
<< "Histogram \"" << name << "\" does not exist.";
}
}
......@@ -95,8 +97,6 @@ HistogramBase::Count HistogramTester::GetBucketCount(
StringPiece name,
HistogramBase::Sample sample) const {
HistogramBase* histogram = StatisticsRecorder::FindHistogram(name);
EXPECT_NE(nullptr, histogram)
<< "Histogram \"" << name << "\" does not exist.";
HistogramBase::Count count = 0;
if (histogram) {
std::unique_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
......
......@@ -155,4 +155,18 @@ TEST_F(HistogramTesterTest, TestGetAllChangedHistograms) {
results.find("Histogram: Test1.Test2.Test3 recorded 1 new samples"));
}
TEST_F(HistogramTesterTest, MissingHistogramMeansEmptyBuckets) {
// When a histogram hasn't been instantiated, expecting counts of zero should
// still succeed.
static const char kHistogram[] = "MissingHistogramMeansEmptyBucketsHistogram";
HistogramTester tester;
tester.ExpectBucketCount(kHistogram, 42, 0);
tester.ExpectTotalCount(kHistogram, 0);
EXPECT_TRUE(tester.GetAllSamples(kHistogram).empty());
EXPECT_EQ(0, tester.GetBucketCount(kHistogram, 42));
EXPECT_EQ(0,
tester.GetHistogramSamplesSinceCreation(kHistogram)->TotalCount());
}
} // namespace base
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment