Index: base/test/histogram_tester.cc |
diff --git a/chrome/test/base/uma_histogram_helper.cc b/base/test/histogram_tester.cc |
similarity index 53% |
rename from chrome/test/base/uma_histogram_helper.cc |
rename to base/test/histogram_tester.cc |
index 21ff6f7adc1431c0fe674519c1474bbd33a796a6..ba01b26238061ac1f97a6e228aadb4dbc8660f40 100644 |
--- a/chrome/test/base/uma_histogram_helper.cc |
+++ b/base/test/histogram_tester.cc |
@@ -1,55 +1,35 @@ |
-// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "chrome/test/base/uma_histogram_helper.h" |
+#include "base/test/histogram_tester.h" |
-#include "base/bind.h" |
+#include "base/metrics/histogram.h" |
+#include "base/metrics/histogram_samples.h" |
#include "base/metrics/statistics_recorder.h" |
-#include "base/test/test_timeouts.h" |
-#include "chrome/test/base/ui_test_utils.h" |
-#include "content/public/browser/histogram_fetcher.h" |
+#include "base/stl_util.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
-UMAHistogramHelper::UMAHistogramHelper() { |
- base::StatisticsRecorder::Initialize(); |
-} |
- |
-UMAHistogramHelper::~UMAHistogramHelper() { |
-} |
+namespace base { |
-void UMAHistogramHelper::PrepareSnapshot(const char* const histogram_names[], |
- size_t num_histograms) { |
- for (size_t i = 0; i < num_histograms; ++i) { |
- std::string histogram_name = histogram_names[i]; |
+HistogramTester::HistogramTester() { |
+ StatisticsRecorder::Initialize(); // Safe to call multiple times. |
- base::HistogramBase* histogram = |
- base::StatisticsRecorder::FindHistogram(histogram_name); |
- // If there is no histogram present, then don't record a snapshot. The logic |
- // in the Expect* methods will act to treat no histogram equivalent to |
- // samples with zeros. |
- if (histogram) { |
- histogram_snapshots[histogram_name] = |
- make_linked_ptr(histogram->SnapshotSamples().release()); |
- } |
+ // Record any histogram data that exists when the object is created so it can |
+ // be subtracted later. |
+ StatisticsRecorder::Histograms histograms; |
+ StatisticsRecorder::GetSnapshot(std::string(), &histograms); |
+ for (size_t i = 0; i < histograms.size(); ++i) { |
+ histograms_snapshot_[histograms[i]->histogram_name()] = |
+ histograms[i]->SnapshotSamples().release(); |
} |
} |
-void UMAHistogramHelper::Fetch() { |
- base::Closure callback = base::Bind(&UMAHistogramHelper::FetchCallback, |
- base::Unretained(this)); |
- |
- content::FetchHistogramsAsynchronously( |
- base::MessageLoop::current(), |
- callback, |
- // If this call times out, it means that a child process is not |
- // responding, which is something we should not ignore. The timeout is |
- // set to be longer than the normal browser test timeout so that it will |
- // be prempted by the normal timeout. |
- TestTimeouts::action_max_timeout() * 2); |
- content::RunMessageLoop(); |
+HistogramTester::~HistogramTester() { |
+ STLDeleteValues(&histograms_snapshot_); |
} |
-void UMAHistogramHelper::ExpectUniqueSample( |
+const void HistogramTester::ExpectUniqueSample( |
const std::string& name, |
base::HistogramBase::Sample sample, |
base::HistogramBase::Count expected_count) { |
@@ -65,7 +45,7 @@ void UMAHistogramHelper::ExpectUniqueSample( |
} |
} |
-void UMAHistogramHelper::ExpectBucketCount( |
+const void HistogramTester::ExpectBucketCount( |
const std::string& name, |
base::HistogramBase::Sample sample, |
base::HistogramBase::Count expected_count) { |
@@ -80,7 +60,7 @@ void UMAHistogramHelper::ExpectBucketCount( |
} |
} |
-void UMAHistogramHelper::ExpectTotalCount( |
+const void HistogramTester::ExpectTotalCount( |
const std::string& name, |
base::HistogramBase::Count count) { |
base::HistogramBase* histogram = |
@@ -94,18 +74,27 @@ void UMAHistogramHelper::ExpectTotalCount( |
} |
} |
-void UMAHistogramHelper::FetchCallback() { |
- base::MessageLoopForUI::current()->Quit(); |
+scoped_ptr<HistogramSamples> HistogramTester::GetHistogramSamplesSinceCreation( |
+ const std::string& histogram_name) { |
+ HistogramBase* histogram = StatisticsRecorder::FindHistogram(histogram_name); |
+ if (!histogram) |
+ return scoped_ptr<HistogramSamples>(); |
+ scoped_ptr<HistogramSamples> named_samples(histogram->SnapshotSamples()); |
+ HistogramSamples* named_original_samples = |
+ histograms_snapshot_[histogram_name]; |
+ if (named_original_samples) |
+ named_samples->Subtract(*named_original_samples); |
+ return named_samples.Pass(); |
} |
-void UMAHistogramHelper::CheckBucketCount( |
+const void HistogramTester::CheckBucketCount( |
const std::string& name, |
base::HistogramBase::Sample sample, |
base::HistogramBase::Count expected_count, |
base::HistogramSamples& samples) { |
int actual_count = samples.GetCount(sample); |
- if (histogram_snapshots.count(name)) |
- actual_count -= histogram_snapshots[name]->GetCount(sample); |
+ if (histograms_snapshot_.count(name)) |
+ actual_count -= histograms_snapshot_[name]->GetCount(sample); |
EXPECT_EQ(expected_count, actual_count) |
<< "Histogram \"" << name |
<< "\" does not have the right number of samples (" << expected_count |
@@ -113,15 +102,17 @@ void UMAHistogramHelper::CheckBucketCount( |
<< ")."; |
} |
-void UMAHistogramHelper::CheckTotalCount( |
+const void HistogramTester::CheckTotalCount( |
const std::string& name, |
base::HistogramBase::Count expected_count, |
base::HistogramSamples& samples) { |
int actual_count = samples.TotalCount(); |
- if (histogram_snapshots.count(name)) |
- actual_count -= histogram_snapshots[name]->TotalCount(); |
+ if (histograms_snapshot_.count(name)) |
+ actual_count -= histograms_snapshot_[name]->TotalCount(); |
EXPECT_EQ(expected_count, actual_count) |
<< "Histogram \"" << name |
<< "\" does not have the right total number of samples (" |
<< expected_count << "). It has (" << actual_count << ")."; |
} |
+ |
+} // namespace base |