summaryrefslogtreecommitdiffstats
path: root/base/metrics
diff options
context:
space:
mode:
authorkaiwang@chromium.org <kaiwang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-23 04:12:17 +0000
committerkaiwang@chromium.org <kaiwang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-01-23 04:12:17 +0000
commitde415556289c07f6a28bec79405d413460b186d2 (patch)
tree6664e471f3383e96c9f768750ad25d75cab567dc /base/metrics
parent042b62dec29c560ccf3e5bfdbf658c11adb56f0b (diff)
downloadchromium_src-de415556289c07f6a28bec79405d413460b186d2.zip
chromium_src-de415556289c07f6a28bec79405d413460b186d2.tar.gz
chromium_src-de415556289c07f6a28bec79405d413460b186d2.tar.bz2
Only HistogramBase is used outside of base/metrics.
So client code of histogram will see a simpler interface. This also makes adding SparseHistogram to existing metrics framework possible. This CL depends on https://codereview.chromium.org/11682003/ So please review that one first. TBR=sky@chromium.org,erikwright@chromium.org BUG=139612 Review URL: https://chromiumcodereview.appspot.com/11615008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@178242 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/metrics')
-rw-r--r--base/metrics/histogram.cc58
-rw-r--r--base/metrics/histogram.h67
-rw-r--r--base/metrics/histogram_base.cc8
-rw-r--r--base/metrics/histogram_base.h5
-rw-r--r--base/metrics/histogram_unittest.cc114
-rw-r--r--base/metrics/statistics_recorder_unittest.cc88
6 files changed, 177 insertions, 163 deletions
diff --git a/base/metrics/histogram.cc b/base/metrics/histogram.cc
index d5da371..2e94841 100644
--- a/base/metrics/histogram.cc
+++ b/base/metrics/histogram.cc
@@ -107,11 +107,11 @@ void CheckCorruption(const Histogram& histogram, bool new_histogram) {
CHECK(histogram.bucket_ranges()->HasValidChecksum());
}
-Histogram* Histogram::FactoryGet(const string& name,
- Sample minimum,
- Sample maximum,
- size_t bucket_count,
- int32 flags) {
+HistogramBase* Histogram::FactoryGet(const string& name,
+ Sample minimum,
+ Sample maximum,
+ size_t bucket_count,
+ int32 flags) {
bool valid_arguments =
InspectConstructionArguments(name, &minimum, &maximum, &bucket_count);
DCHECK(valid_arguments);
@@ -140,11 +140,11 @@ Histogram* Histogram::FactoryGet(const string& name,
return histogram;
}
-Histogram* Histogram::FactoryTimeGet(const string& name,
- TimeDelta minimum,
- TimeDelta maximum,
- size_t bucket_count,
- int32 flags) {
+HistogramBase* Histogram::FactoryTimeGet(const string& name,
+ TimeDelta minimum,
+ TimeDelta maximum,
+ size_t bucket_count,
+ int32 flags) {
return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
bucket_count, flags);
}
@@ -197,10 +197,6 @@ void Histogram::InitializeBucketRanges(Sample minimum,
ranges->ResetChecksum();
}
-void Histogram::AddBoolean(bool value) {
- DCHECK(false);
-}
-
// static
const int Histogram::kCommonRaceBasedCountMismatch = 5;
@@ -574,25 +570,25 @@ void Histogram::GetCountAndBucketData(Count* count, ListValue* buckets) const {
LinearHistogram::~LinearHistogram() {}
-Histogram* LinearHistogram::FactoryGet(const string& name,
- Sample minimum,
- Sample maximum,
- size_t bucket_count,
- int32 flags) {
+HistogramBase* LinearHistogram::FactoryGet(const string& name,
+ Sample minimum,
+ Sample maximum,
+ size_t bucket_count,
+ int32 flags) {
return FactoryGetWithRangeDescription(
name, minimum, maximum, bucket_count, flags, NULL);
}
-Histogram* LinearHistogram::FactoryTimeGet(const string& name,
- TimeDelta minimum,
- TimeDelta maximum,
- size_t bucket_count,
- int32 flags) {
+HistogramBase* LinearHistogram::FactoryTimeGet(const string& name,
+ TimeDelta minimum,
+ TimeDelta maximum,
+ size_t bucket_count,
+ int32 flags) {
return FactoryGet(name, minimum.InMilliseconds(), maximum.InMilliseconds(),
bucket_count, flags);
}
-Histogram* LinearHistogram::FactoryGetWithRangeDescription(
+HistogramBase* LinearHistogram::FactoryGetWithRangeDescription(
const std::string& name,
Sample minimum,
Sample maximum,
@@ -713,7 +709,7 @@ HistogramBase* LinearHistogram::DeserializeInfoImpl(PickleIterator* iter) {
// This section provides implementation for BooleanHistogram.
//------------------------------------------------------------------------------
-Histogram* BooleanHistogram::FactoryGet(const string& name, int32 flags) {
+HistogramBase* BooleanHistogram::FactoryGet(const string& name, int32 flags) {
Histogram* histogram = StatisticsRecorder::FindHistogram(name);
if (!histogram) {
// To avoid racy destruction at shutdown, the following will be leaked.
@@ -741,10 +737,6 @@ HistogramType BooleanHistogram::GetHistogramType() const {
return BOOLEAN_HISTOGRAM;
}
-void BooleanHistogram::AddBoolean(bool value) {
- Add(value ? 1 : 0);
-}
-
BooleanHistogram::BooleanHistogram(const string& name,
const BucketRanges* ranges)
: LinearHistogram(name, 1, 2, 3, ranges) {}
@@ -775,9 +767,9 @@ HistogramBase* BooleanHistogram::DeserializeInfoImpl(PickleIterator* iter) {
// CustomHistogram:
//------------------------------------------------------------------------------
-Histogram* CustomHistogram::FactoryGet(const string& name,
- const vector<Sample>& custom_ranges,
- int32 flags) {
+HistogramBase* CustomHistogram::FactoryGet(const string& name,
+ const vector<Sample>& custom_ranges,
+ int32 flags) {
CHECK(ValidateCustomRanges(custom_ranges));
Histogram* histogram = StatisticsRecorder::FindHistogram(name);
diff --git a/base/metrics/histogram.h b/base/metrics/histogram.h
index 2c832fd..47db608 100644
--- a/base/metrics/histogram.h
+++ b/base/metrics/histogram.h
@@ -147,8 +147,9 @@ class Lock;
histogram_factory_get_invocation) \
do { \
static base::subtle::AtomicWord atomic_histogram_pointer = 0; \
- base::Histogram* histogram_pointer(reinterpret_cast<base::Histogram*>( \
- base::subtle::Acquire_Load(&atomic_histogram_pointer))); \
+ base::HistogramBase* histogram_pointer( \
+ reinterpret_cast<base::HistogramBase*>( \
+ base::subtle::Acquire_Load(&atomic_histogram_pointer))); \
if (!histogram_pointer) { \
histogram_pointer = histogram_factory_get_invocation; \
base::subtle::Release_Store(&atomic_histogram_pointer, \
@@ -309,7 +310,7 @@ class Lock;
#define UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, min, max, bucket_count) \
STATIC_HISTOGRAM_POINTER_BLOCK(name, AddTime(sample), \
base::Histogram::FactoryTimeGet(name, min, max, bucket_count, \
- base::Histogram::kUmaTargetedHistogramFlag))
+ base::HistogramBase::kUmaTargetedHistogramFlag))
#define UMA_HISTOGRAM_COUNTS(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
name, sample, 1, 1000000, 50)
@@ -323,7 +324,7 @@ class Lock;
#define UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, min, max, bucket_count) \
STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
base::Histogram::FactoryGet(name, min, max, bucket_count, \
- base::Histogram::kUmaTargetedHistogramFlag))
+ base::HistogramBase::kUmaTargetedHistogramFlag))
#define UMA_HISTOGRAM_MEMORY_KB(name, sample) UMA_HISTOGRAM_CUSTOM_COUNTS( \
name, sample, 1000, 500000, 50)
@@ -337,19 +338,19 @@ class Lock;
#define UMA_HISTOGRAM_BOOLEAN(name, sample) \
STATIC_HISTOGRAM_POINTER_BLOCK(name, AddBoolean(sample), \
base::BooleanHistogram::FactoryGet(name, \
- base::Histogram::kUmaTargetedHistogramFlag))
+ base::HistogramBase::kUmaTargetedHistogramFlag))
// The samples should always be strictly less than |boundary_value|. For more
// details, see the comment for the |HISTOGRAM_ENUMERATION| macro, above.
#define UMA_HISTOGRAM_ENUMERATION(name, sample, boundary_value) \
STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
base::LinearHistogram::FactoryGet(name, 1, boundary_value, \
- boundary_value + 1, base::Histogram::kUmaTargetedHistogramFlag))
+ boundary_value + 1, base::HistogramBase::kUmaTargetedHistogramFlag))
#define UMA_HISTOGRAM_CUSTOM_ENUMERATION(name, sample, custom_ranges) \
STATIC_HISTOGRAM_POINTER_BLOCK(name, Add(sample), \
base::CustomHistogram::FactoryGet(name, custom_ranges, \
- base::Histogram::kUmaTargetedHistogramFlag))
+ base::HistogramBase::kUmaTargetedHistogramFlag))
//------------------------------------------------------------------------------
@@ -388,16 +389,16 @@ class BASE_EXPORT Histogram : public HistogramBase {
// buckets <= (maximum - minimum + 2) - this is to ensure that we don't have
// more buckets than the range of numbers; having more buckets than 1 per
// value in the range would be nonsensical.
- static Histogram* FactoryGet(const std::string& name,
- Sample minimum,
- Sample maximum,
- size_t bucket_count,
- int32 flags);
- static Histogram* FactoryTimeGet(const std::string& name,
- base::TimeDelta minimum,
- base::TimeDelta maximum,
+ static HistogramBase* FactoryGet(const std::string& name,
+ Sample minimum,
+ Sample maximum,
size_t bucket_count,
int32 flags);
+ static HistogramBase* FactoryTimeGet(const std::string& name,
+ base::TimeDelta minimum,
+ base::TimeDelta maximum,
+ size_t bucket_count,
+ int32 flags);
// Time call for use with DHISTOGRAM*.
// Returns TimeTicks::Now() in debug and TimeTicks() in release build.
@@ -408,14 +409,6 @@ class BASE_EXPORT Histogram : public HistogramBase {
size_t bucket_count,
BucketRanges* ranges);
- // This method is an interface, used only by BooleanHistogram.
- virtual void AddBoolean(bool value);
-
- // Accept a TimeDelta to increment.
- void AddTime(TimeDelta time) {
- Add(static_cast<int>(time.InMilliseconds()));
- }
-
// This constant if for FindCorruption. Since snapshots of histograms are
// taken asynchronously relative to sampling, and our counting code currently
// does not prevent race conditions, it is pretty likely that we'll catch a
@@ -567,16 +560,16 @@ class BASE_EXPORT LinearHistogram : public Histogram {
/* minimum should start from 1. 0 is as minimum is invalid. 0 is an implicit
default underflow bucket. */
- static Histogram* FactoryGet(const std::string& name,
- Sample minimum,
- Sample maximum,
- size_t bucket_count,
- int32 flags);
- static Histogram* FactoryTimeGet(const std::string& name,
- TimeDelta minimum,
- TimeDelta maximum,
+ static HistogramBase* FactoryGet(const std::string& name,
+ Sample minimum,
+ Sample maximum,
size_t bucket_count,
int32 flags);
+ static HistogramBase* FactoryTimeGet(const std::string& name,
+ TimeDelta minimum,
+ TimeDelta maximum,
+ size_t bucket_count,
+ int32 flags);
struct DescriptionPair {
Sample sample;
@@ -588,7 +581,7 @@ class BASE_EXPORT LinearHistogram : public Histogram {
// |descriptions| can be NULL, which means no special descriptions to set. If
// it's not NULL, the last element in the array must has a NULL in its
// "description" field.
- static Histogram* FactoryGetWithRangeDescription(
+ static HistogramBase* FactoryGetWithRangeDescription(
const std::string& name,
Sample minimum,
Sample maximum,
@@ -640,12 +633,10 @@ class BASE_EXPORT LinearHistogram : public Histogram {
// BooleanHistogram is a histogram for booleans.
class BASE_EXPORT BooleanHistogram : public LinearHistogram {
public:
- static Histogram* FactoryGet(const std::string& name, int32 flags);
+ static HistogramBase* FactoryGet(const std::string& name, int32 flags);
virtual HistogramType GetHistogramType() const OVERRIDE;
- virtual void AddBoolean(bool value) OVERRIDE;
-
private:
BooleanHistogram(const std::string& name, const BucketRanges* ranges);
@@ -665,9 +656,9 @@ class BASE_EXPORT CustomHistogram : public Histogram {
// > 0 and < kSampleType_MAX. (Currently 0 is still accepted for backward
// compatibility). The limits can be unordered or contain duplication, but
// client should not depend on this.
- static Histogram* FactoryGet(const std::string& name,
- const std::vector<Sample>& custom_ranges,
- int32 flags);
+ static HistogramBase* FactoryGet(const std::string& name,
+ const std::vector<Sample>& custom_ranges,
+ int32 flags);
// Overridden from Histogram:
virtual HistogramType GetHistogramType() const OVERRIDE;
diff --git a/base/metrics/histogram_base.cc b/base/metrics/histogram_base.cc
index 6396132..403303a 100644
--- a/base/metrics/histogram_base.cc
+++ b/base/metrics/histogram_base.cc
@@ -86,6 +86,14 @@ void HistogramBase::ClearFlags(int32 flags) {
flags_ &= ~flags;
}
+void HistogramBase::AddTime(const TimeDelta& time) {
+ Add(static_cast<Sample>(time.InMilliseconds()));
+}
+
+void HistogramBase::AddBoolean(bool value) {
+ Add(value ? 1 : 0);
+}
+
bool HistogramBase::SerializeInfo(Pickle* pickle) const {
if (!pickle->WriteInt(GetHistogramType()))
return false;
diff --git a/base/metrics/histogram_base.h b/base/metrics/histogram_base.h
index 949e864..729670c 100644
--- a/base/metrics/histogram_base.h
+++ b/base/metrics/histogram_base.h
@@ -10,6 +10,7 @@
#include "base/base_export.h"
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
+#include "base/time.h"
class Pickle;
class PickleIterator;
@@ -90,6 +91,10 @@ class BASE_EXPORT HistogramBase {
virtual void Add(Sample value) = 0;
+ // 2 convenient functions that call Add(Sample).
+ void AddTime(const TimeDelta& time);
+ void AddBoolean(bool value);
+
virtual void AddSamples(const HistogramSamples& samples) = 0;
virtual bool AddSamplesFromPickle(PickleIterator* iter) = 0;
diff --git a/base/metrics/histogram_unittest.cc b/base/metrics/histogram_unittest.cc
index ee4e024..27c0faf 100644
--- a/base/metrics/histogram_unittest.cc
+++ b/base/metrics/histogram_unittest.cc
@@ -49,20 +49,20 @@ class HistogramTest : public testing::Test {
// Check for basic syntax and use.
TEST_F(HistogramTest, BasicTest) {
// Try basic construction
- Histogram* histogram(Histogram::FactoryGet(
- "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags));
- EXPECT_NE(reinterpret_cast<Histogram*>(NULL), histogram);
+ HistogramBase* histogram = Histogram::FactoryGet(
+ "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
+ EXPECT_TRUE(histogram);
- Histogram* linear_histogram(LinearHistogram::FactoryGet(
- "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags));
- EXPECT_NE(reinterpret_cast<Histogram*>(NULL), linear_histogram);
+ HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
+ "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
+ EXPECT_TRUE(linear_histogram);
vector<int> custom_ranges;
custom_ranges.push_back(1);
custom_ranges.push_back(5);
- Histogram* custom_histogram(CustomHistogram::FactoryGet(
- "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags));
- EXPECT_NE(reinterpret_cast<Histogram*>(NULL), custom_histogram);
+ HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
+ "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
+ EXPECT_TRUE(custom_histogram);
// Use standard macros (but with fixed samples)
HISTOGRAM_TIMES("Test2Histogram", TimeDelta::FromDays(1));
@@ -79,11 +79,12 @@ TEST_F(HistogramTest, BasicTest) {
TEST_F(HistogramTest, NameMatchTest) {
HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10);
HISTOGRAM_PERCENTAGE("DuplicatedHistogram", 10);
- Histogram* histogram(LinearHistogram::FactoryGet(
- "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags));
- scoped_ptr<SampleVector> samples = histogram->SnapshotSampleVector();
+ HistogramBase* histogram = LinearHistogram::FactoryGet(
+ "DuplicatedHistogram", 1, 101, 102, HistogramBase::kNoFlags);
+
+ scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
EXPECT_EQ(2, samples->TotalCount());
- EXPECT_EQ(2, samples->GetCountAtIndex(10));
+ EXPECT_EQ(2, samples->GetCount(10));
}
TEST_F(HistogramTest, ExponentialRangesTest) {
@@ -99,8 +100,8 @@ TEST_F(HistogramTest, ExponentialRangesTest) {
EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges.range(8));
// Check the corresponding Histogram will use the correct ranges.
- Histogram* histogram(Histogram::FactoryGet(
- "Histogram", 1, 64, 8, HistogramBase::kNoFlags));
+ Histogram* histogram = static_cast<Histogram*>(
+ Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
EXPECT_TRUE(ranges.Equals(histogram->bucket_ranges()));
// When bucket count is limited, exponential ranges will partially look like
@@ -126,8 +127,8 @@ TEST_F(HistogramTest, ExponentialRangesTest) {
EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges2.range(15));
// Check the corresponding Histogram will use the correct ranges.
- Histogram* histogram2(Histogram::FactoryGet(
- "Histogram2", 1, 32, 15, HistogramBase::kNoFlags));
+ Histogram* histogram2 = static_cast<Histogram*>(
+ Histogram::FactoryGet("Histogram2", 1, 32, 15, HistogramBase::kNoFlags));
EXPECT_TRUE(ranges2.Equals(histogram2->bucket_ranges()));
}
@@ -138,8 +139,9 @@ TEST_F(HistogramTest, LinearRangesTest) {
for (int i = 0; i < 8; i++)
EXPECT_EQ(i, ranges.range(i));
EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges.range(8));
+
// The correspoding LinearHistogram should use the correct ranges.
- Histogram* histogram(
+ Histogram* histogram = static_cast<Histogram*>(
LinearHistogram::FactoryGet("Linear", 1, 7, 8, HistogramBase::kNoFlags));
EXPECT_TRUE(ranges.Equals(histogram->bucket_ranges()));
@@ -153,7 +155,7 @@ TEST_F(HistogramTest, LinearRangesTest) {
EXPECT_EQ(6, ranges2.range(4));
EXPECT_EQ(HistogramBase::kSampleType_MAX, ranges2.range(5));
// The correspoding LinearHistogram should use the correct ranges.
- Histogram* histogram2(
+ Histogram* histogram2 = static_cast<Histogram*>(
LinearHistogram::FactoryGet("Linear2", 1, 6, 5, HistogramBase::kNoFlags));
EXPECT_TRUE(ranges2.Equals(histogram2->bucket_ranges()));
}
@@ -176,8 +178,10 @@ TEST_F(HistogramTest, CustomHistogramTest) {
vector<HistogramBase::Sample> custom_ranges;
custom_ranges.push_back(1);
custom_ranges.push_back(2);
- Histogram* histogram = CustomHistogram::FactoryGet(
- "TestCustomHistogram1", custom_ranges, HistogramBase::kNoFlags);
+
+ Histogram* histogram = static_cast<Histogram*>(
+ CustomHistogram::FactoryGet("TestCustomHistogram1", custom_ranges,
+ HistogramBase::kNoFlags));
const BucketRanges* ranges = histogram->bucket_ranges();
ASSERT_EQ(4u, ranges->size());
EXPECT_EQ(0, ranges->range(0)); // Auto added.
@@ -189,8 +193,9 @@ TEST_F(HistogramTest, CustomHistogramTest) {
custom_ranges.clear();
custom_ranges.push_back(2);
custom_ranges.push_back(1);
- histogram = CustomHistogram::FactoryGet(
- "TestCustomHistogram2", custom_ranges, HistogramBase::kNoFlags);
+ histogram = static_cast<Histogram*>(
+ CustomHistogram::FactoryGet("TestCustomHistogram2", custom_ranges,
+ HistogramBase::kNoFlags));
ranges = histogram->bucket_ranges();
ASSERT_EQ(4u, ranges->size());
EXPECT_EQ(0, ranges->range(0));
@@ -203,8 +208,9 @@ TEST_F(HistogramTest, CustomHistogramTest) {
custom_ranges.push_back(4);
custom_ranges.push_back(1);
custom_ranges.push_back(4);
- histogram = CustomHistogram::FactoryGet(
- "TestCustomHistogram3", custom_ranges, HistogramBase::kNoFlags);
+ histogram = static_cast<Histogram*>(
+ CustomHistogram::FactoryGet("TestCustomHistogram3", custom_ranges,
+ HistogramBase::kNoFlags));
ranges = histogram->bucket_ranges();
ASSERT_EQ(4u, ranges->size());
EXPECT_EQ(0, ranges->range(0));
@@ -222,8 +228,9 @@ TEST_F(HistogramTest, CustomHistogramWithOnly2Buckets) {
vector<HistogramBase::Sample> custom_ranges;
custom_ranges.push_back(4);
- Histogram* histogram = CustomHistogram::FactoryGet(
- "2BucketsCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
+ Histogram* histogram = static_cast<Histogram*>(
+ CustomHistogram::FactoryGet("2BucketsCustomHistogram", custom_ranges,
+ HistogramBase::kNoFlags));
const BucketRanges* ranges = histogram->bucket_ranges();
ASSERT_EQ(3u, ranges->size());
EXPECT_EQ(0, ranges->range(0));
@@ -234,8 +241,9 @@ TEST_F(HistogramTest, CustomHistogramWithOnly2Buckets) {
// Make sure histogram handles out-of-bounds data gracefully.
TEST_F(HistogramTest, BoundsTest) {
const size_t kBucketCount = 50;
- Histogram* histogram(Histogram::FactoryGet(
- "Bounded", 10, 100, kBucketCount, HistogramBase::kNoFlags));
+ Histogram* histogram = static_cast<Histogram*>(
+ Histogram::FactoryGet("Bounded", 10, 100, kBucketCount,
+ HistogramBase::kNoFlags));
// Put two samples "out of bounds" above and below.
histogram->Add(5);
@@ -257,9 +265,9 @@ TEST_F(HistogramTest, BoundsTest) {
custom_ranges.push_back(10);
custom_ranges.push_back(50);
custom_ranges.push_back(100);
- Histogram* test_custom_histogram = CustomHistogram::FactoryGet(
- "TestCustomRangeBoundedHistogram", custom_ranges,
- HistogramBase::kNoFlags);
+ Histogram* test_custom_histogram = static_cast<Histogram*>(
+ CustomHistogram::FactoryGet("TestCustomRangeBoundedHistogram",
+ custom_ranges, HistogramBase::kNoFlags));
// Put two samples "out of bounds" above and below.
test_custom_histogram->Add(5);
@@ -280,8 +288,8 @@ TEST_F(HistogramTest, BoundsTest) {
// Check to be sure samples land as expected is "correct" buckets.
TEST_F(HistogramTest, BucketPlacementTest) {
- Histogram* histogram(Histogram::FactoryGet(
- "Histogram", 1, 64, 8, HistogramBase::kNoFlags));
+ Histogram* histogram = static_cast<Histogram*>(
+ Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
// Add i+1 samples to the i'th bucket.
histogram->Add(0);
@@ -299,8 +307,8 @@ TEST_F(HistogramTest, BucketPlacementTest) {
}
TEST_F(HistogramTest, CorruptSampleCounts) {
- Histogram* histogram(Histogram::FactoryGet(
- "Histogram", 1, 64, 8, HistogramBase::kNoFlags)); // As per header file.
+ Histogram* histogram = static_cast<Histogram*>(
+ Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
// Add some samples.
histogram->Add(20);
@@ -326,8 +334,8 @@ TEST_F(HistogramTest, CorruptSampleCounts) {
}
TEST_F(HistogramTest, CorruptBucketBounds) {
- Histogram* histogram(Histogram::FactoryGet(
- "Histogram", 1, 64, 8, HistogramBase::kNoFlags)); // As per header file.
+ Histogram* histogram = static_cast<Histogram*>(
+ Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags));
scoped_ptr<SampleVector> snapshot = histogram->SnapshotSampleVector();
EXPECT_EQ(Histogram::NO_INCONSISTENCIES,
@@ -360,9 +368,9 @@ TEST_F(HistogramTest, CorruptBucketBounds) {
}
TEST_F(HistogramTest, HistogramSerializeInfo) {
- Histogram* histogram = Histogram::FactoryGet(
- "Histogram", 1, 64, 8, HistogramBase::kIPCSerializationSourceFlag);
-
+ Histogram* histogram = static_cast<Histogram*>(
+ Histogram::FactoryGet("Histogram", 1, 64, 8,
+ HistogramBase::kIPCSerializationSourceFlag));
Pickle pickle;
histogram->SerializeInfo(&pickle);
@@ -405,7 +413,7 @@ TEST_F(HistogramTest, CustomHistogramSerializeInfo) {
custom_ranges.push_back(10);
custom_ranges.push_back(100);
- Histogram* custom_histogram = CustomHistogram::FactoryGet(
+ HistogramBase* custom_histogram = CustomHistogram::FactoryGet(
"TestCustomRangeBoundedHistogram",
custom_ranges,
HistogramBase::kNoFlags);
@@ -440,25 +448,27 @@ TEST_F(HistogramTest, CustomHistogramSerializeInfo) {
// 1). But we accept ranges exceeding those limits, and silently clamped to
// those limits. This is for backwards compatibility.
TEST(HistogramDeathTest, BadRangesTest) {
- Histogram* histogram = Histogram::FactoryGet(
+ HistogramBase* histogram = Histogram::FactoryGet(
"BadRanges", 0, HistogramBase::kSampleType_MAX, 8,
HistogramBase::kNoFlags);
- EXPECT_EQ(1, histogram->declared_min());
- EXPECT_EQ(HistogramBase::kSampleType_MAX - 1, histogram->declared_max());
+ EXPECT_TRUE(
+ histogram->HasConstructionArguments(
+ 1, HistogramBase::kSampleType_MAX - 1, 8));
- Histogram* linear_histogram = LinearHistogram::FactoryGet(
+ HistogramBase* linear_histogram = LinearHistogram::FactoryGet(
"BadRangesLinear", 0, HistogramBase::kSampleType_MAX, 8,
HistogramBase::kNoFlags);
- EXPECT_EQ(1, linear_histogram->declared_min());
- EXPECT_EQ(HistogramBase::kSampleType_MAX - 1,
- linear_histogram->declared_max());
+ EXPECT_TRUE(
+ linear_histogram->HasConstructionArguments(
+ 1, HistogramBase::kSampleType_MAX - 1, 8));
vector<int> custom_ranges;
custom_ranges.push_back(0);
custom_ranges.push_back(5);
- Histogram* custom_histogram1 = CustomHistogram::FactoryGet(
- "BadRangesCustom", custom_ranges, HistogramBase::kNoFlags);
- const BucketRanges* ranges = custom_histogram1->bucket_ranges();
+ Histogram* custom_histogram = static_cast<Histogram*>(
+ CustomHistogram::FactoryGet(
+ "BadRangesCustom", custom_ranges, HistogramBase::kNoFlags));
+ const BucketRanges* ranges = custom_histogram->bucket_ranges();
ASSERT_EQ(3u, ranges->size());
EXPECT_EQ(0, ranges->range(0));
EXPECT_EQ(5, ranges->range(1));
diff --git a/base/metrics/statistics_recorder_unittest.cc b/base/metrics/statistics_recorder_unittest.cc
index 95759c1..0779c99 100644
--- a/base/metrics/statistics_recorder_unittest.cc
+++ b/base/metrics/statistics_recorder_unittest.cc
@@ -32,7 +32,14 @@ class StatisticsRecorderTest : public testing::Test {
statistics_recorder_ = NULL;
}
- void DeleteHistogram(Histogram* histogram) {
+ Histogram* CreateHistogram(const std::string& name,
+ HistogramBase::Sample min,
+ HistogramBase::Sample max,
+ size_t bucket_count) {
+ return new Histogram(name, min, max, bucket_count, NULL);
+ }
+
+ void DeleteHistogram(HistogramBase* histogram) {
delete histogram;
}
@@ -47,14 +54,17 @@ TEST_F(StatisticsRecorderTest, NotInitialized) {
StatisticsRecorder::Histograms registered_histograms;
std::vector<const BucketRanges*> registered_ranges;
- // We can still create histograms, but it's not registered.
- // TODO(kaiwang): Do not depend on Histogram FactoryGet implementation.
- Histogram* histogram(
- Histogram::FactoryGet("StatisticsRecorderTest_NotInitialized",
- 1, 1000, 10, Histogram::kNoFlags));
StatisticsRecorder::GetHistograms(&registered_histograms);
EXPECT_EQ(0u, registered_histograms.size());
+ Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10);
+
+ // When StatisticsRecorder is not initialized, register is a noop.
+ EXPECT_EQ(histogram,
+ StatisticsRecorder::RegisterOrDeleteDuplicate(histogram));
+ // Manually delete histogram that was not registered.
+ DeleteHistogram(histogram);
+
// RegisterOrDeleteDuplicateRanges is a no-op.
BucketRanges* ranges = new BucketRanges(3);;
ranges->ResetChecksum();
@@ -62,8 +72,6 @@ TEST_F(StatisticsRecorderTest, NotInitialized) {
StatisticsRecorder::RegisterOrDeleteDuplicateRanges(ranges));
StatisticsRecorder::GetBucketRanges(&registered_ranges);
EXPECT_EQ(0u, registered_ranges.size());
-
- DeleteHistogram(histogram);
}
TEST_F(StatisticsRecorderTest, RegisterBucketRanges) {
@@ -106,13 +114,8 @@ TEST_F(StatisticsRecorderTest, RegisterBucketRanges) {
TEST_F(StatisticsRecorderTest, RegisterHistogram) {
// Create a Histogram that was not registered.
- // TODO(kaiwang): Do not depend on Histogram FactoryGet implementation.
- UninitializeStatisticsRecorder();
- Histogram* histogram = Histogram::FactoryGet(
- "TestHistogram", 1, 1000, 10, Histogram::kNoFlags);
+ Histogram* histogram = CreateHistogram("TestHistogram", 1, 1000, 10);
- // Clean StatisticsRecorder.
- InitializeStatisticsRecorder();
StatisticsRecorder::Histograms registered_histograms;
StatisticsRecorder::GetHistograms(&registered_histograms);
EXPECT_EQ(0u, registered_histograms.size());
@@ -129,13 +132,15 @@ TEST_F(StatisticsRecorderTest, RegisterHistogram) {
registered_histograms.clear();
StatisticsRecorder::GetHistograms(&registered_histograms);
EXPECT_EQ(1u, registered_histograms.size());
+
+ DeleteHistogram(histogram);
}
TEST_F(StatisticsRecorderTest, FindHistogram) {
- Histogram* histogram1 = Histogram::FactoryGet(
- "TestHistogram1", 1, 1000, 10, Histogram::kNoFlags);
- Histogram* histogram2 = Histogram::FactoryGet(
- "TestHistogram2", 1, 1000, 10, Histogram::kNoFlags);
+ HistogramBase* histogram1 = Histogram::FactoryGet(
+ "TestHistogram1", 1, 1000, 10, HistogramBase::kNoFlags);
+ HistogramBase* histogram2 = Histogram::FactoryGet(
+ "TestHistogram2", 1, 1000, 10, HistogramBase::kNoFlags);
EXPECT_EQ(histogram1, StatisticsRecorder::FindHistogram("TestHistogram1"));
EXPECT_EQ(histogram2, StatisticsRecorder::FindHistogram("TestHistogram2"));
@@ -166,16 +171,16 @@ TEST_F(StatisticsRecorderTest, RegisterHistogramWithFactoryGet) {
StatisticsRecorder::GetHistograms(&registered_histograms);
ASSERT_EQ(0u, registered_histograms.size());
- // Create a Histogram.
- Histogram* histogram = Histogram::FactoryGet(
- "TestHistogram", 1, 1000, 10, Histogram::kNoFlags);
+ // Create a histogram.
+ HistogramBase* histogram = Histogram::FactoryGet(
+ "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
registered_histograms.clear();
StatisticsRecorder::GetHistograms(&registered_histograms);
EXPECT_EQ(1u, registered_histograms.size());
// Get an existing histogram.
- Histogram* histogram2 = Histogram::FactoryGet(
- "TestHistogram", 1, 1000, 10, Histogram::kNoFlags);
+ HistogramBase* histogram2 = Histogram::FactoryGet(
+ "TestHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
registered_histograms.clear();
StatisticsRecorder::GetHistograms(&registered_histograms);
EXPECT_EQ(1u, registered_histograms.size());
@@ -183,14 +188,14 @@ TEST_F(StatisticsRecorderTest, RegisterHistogramWithFactoryGet) {
// Create a LinearHistogram.
histogram = LinearHistogram::FactoryGet(
- "TestLinearHistogram", 1, 1000, 10, Histogram::kNoFlags);
+ "TestLinearHistogram", 1, 1000, 10, HistogramBase::kNoFlags);
registered_histograms.clear();
StatisticsRecorder::GetHistograms(&registered_histograms);
EXPECT_EQ(2u, registered_histograms.size());
// Create a BooleanHistogram.
histogram = BooleanHistogram::FactoryGet(
- "TestBooleanHistogram", Histogram::kNoFlags);
+ "TestBooleanHistogram", HistogramBase::kNoFlags);
registered_histograms.clear();
StatisticsRecorder::GetHistograms(&registered_histograms);
EXPECT_EQ(3u, registered_histograms.size());
@@ -200,7 +205,7 @@ TEST_F(StatisticsRecorderTest, RegisterHistogramWithFactoryGet) {
custom_ranges.push_back(1);
custom_ranges.push_back(5);
histogram = CustomHistogram::FactoryGet(
- "TestCustomHistogram", custom_ranges, Histogram::kNoFlags);
+ "TestCustomHistogram", custom_ranges, HistogramBase::kNoFlags);
registered_histograms.clear();
StatisticsRecorder::GetHistograms(&registered_histograms);
EXPECT_EQ(4u, registered_histograms.size());
@@ -209,8 +214,8 @@ TEST_F(StatisticsRecorderTest, RegisterHistogramWithFactoryGet) {
TEST_F(StatisticsRecorderTest, RegisterHistogramWithMacros) {
StatisticsRecorder::Histograms registered_histograms;
- Histogram* histogram = Histogram::FactoryGet(
- "TestHistogramCounts", 1, 1000000, 50, Histogram::kNoFlags);
+ HistogramBase* histogram = Histogram::FactoryGet(
+ "TestHistogramCounts", 1, 1000000, 50, HistogramBase::kNoFlags);
// The histogram we got from macro is the same as from FactoryGet.
HISTOGRAM_COUNTS("TestHistogramCounts", 30);
@@ -239,18 +244,21 @@ TEST_F(StatisticsRecorderTest, RegisterHistogramWithMacros) {
}
TEST_F(StatisticsRecorderTest, BucketRangesSharing) {
- Histogram* histogram1(Histogram::FactoryGet(
- "Histogram", 1, 64, 8, Histogram::kNoFlags));
- Histogram* histogram2(Histogram::FactoryGet(
- "Histogram2", 1, 64, 8, Histogram::kNoFlags));
- Histogram* histogram3(Histogram::FactoryGet(
- "Histogram3", 1, 64, 16, Histogram::kNoFlags));
-
- const BucketRanges* bucket_ranges1 = histogram1->bucket_ranges();
- const BucketRanges* bucket_ranges2 = histogram2->bucket_ranges();
- const BucketRanges* bucket_ranges3 = histogram3->bucket_ranges();
- EXPECT_EQ(bucket_ranges1, bucket_ranges2);
- EXPECT_FALSE(bucket_ranges1->Equals(bucket_ranges3));
+ std::vector<const BucketRanges*> ranges;
+ StatisticsRecorder::GetBucketRanges(&ranges);
+ EXPECT_EQ(0u, ranges.size());
+
+ Histogram::FactoryGet("Histogram", 1, 64, 8, HistogramBase::kNoFlags);
+ Histogram::FactoryGet("Histogram2", 1, 64, 8, HistogramBase::kNoFlags);
+
+ StatisticsRecorder::GetBucketRanges(&ranges);
+ EXPECT_EQ(1u, ranges.size());
+
+ Histogram::FactoryGet("Histogram3", 1, 64, 16, HistogramBase::kNoFlags);
+
+ ranges.clear();
+ StatisticsRecorder::GetBucketRanges(&ranges);
+ EXPECT_EQ(2u, ranges.size());
}
} // namespace base