Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Unified Diff: chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc

Issue 18337014: Add a HistogramRecorder class and use cases. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Synced. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc
diff --git a/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc b/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc
index 3812d4b245d2b5eead60bc45bb89114500bca6ed..dda14174ed6473e76798438c9b26b91717b646d5 100644
--- a/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc
+++ b/chrome/browser/spellchecker/spellcheck_host_metrics_unittest.cc
@@ -7,61 +7,52 @@
#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
-#include "base/metrics/histogram.h"
#include "base/metrics/histogram_samples.h"
-#include "base/metrics/statistics_recorder.h"
#include "base/strings/utf_string_conversions.h"
+#include "base/test/histogram_recorder.h"
#include "testing/gtest/include/gtest/gtest.h"
-using base::HistogramBase;
-using base::HistogramSamples;
-using base::StatisticsRecorder;
-
class SpellcheckHostMetricsTest : public testing::Test {
public:
SpellcheckHostMetricsTest() : loop_(base::MessageLoop::TYPE_DEFAULT) {
}
virtual void SetUp() OVERRIDE {
- base::StatisticsRecorder::Initialize();
+ ResetHistogramRecorder();
metrics_.reset(new SpellCheckHostMetrics);
}
+ void ResetHistogramRecorder() {
+ histogram_recorder_.reset(new base::HistogramRecorder());
+ }
+
SpellCheckHostMetrics* metrics() { return metrics_.get(); }
void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); }
+ protected:
+ scoped_ptr<base::HistogramRecorder> histogram_recorder_;
+
private:
base::MessageLoop loop_;
scoped_ptr<SpellCheckHostMetrics> metrics_;
};
TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) {
- scoped_ptr<HistogramSamples> baseline;
- HistogramBase* histogram =
- StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
- if (histogram)
- baseline = histogram->SnapshotSamples();
+ const char kMetricName[] = "SpellCheck.Enabled";
metrics()->RecordEnabledStats(false);
- histogram =
- StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
- ASSERT_TRUE(histogram != NULL);
- scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
- if (baseline.get())
- samples->Subtract(*baseline);
+ scoped_ptr<base::HistogramSamples> samples(
+ histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName));
EXPECT_EQ(1, samples->GetCount(0));
EXPECT_EQ(0, samples->GetCount(1));
- baseline.reset(samples.release());
+ ResetHistogramRecorder();
metrics()->RecordEnabledStats(true);
- histogram =
- StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
- ASSERT_TRUE(histogram != NULL);
- samples = histogram->SnapshotSamples();
- samples->Subtract(*baseline);
+ samples =
+ histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName);
EXPECT_EQ(0, samples->GetCount(0));
EXPECT_EQ(1, samples->GetCount(1));
}
@@ -69,19 +60,14 @@ TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) {
TEST_F(SpellcheckHostMetricsTest, CustomWordStats) {
SpellCheckHostMetrics::RecordCustomWordCountStats(123);
- HistogramBase* histogram =
- StatisticsRecorder::FindHistogram("SpellCheck.CustomWords");
- ASSERT_TRUE(histogram != NULL);
- scoped_ptr<HistogramSamples> baseline = histogram->SnapshotSamples();
+ ResetHistogramRecorder();
SpellCheckHostMetrics::RecordCustomWordCountStats(23);
- histogram =
- StatisticsRecorder::FindHistogram("SpellCheck.CustomWords");
- ASSERT_TRUE(histogram != NULL);
- scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
- samples->Subtract(*baseline);
- EXPECT_EQ(23,samples->sum());
+ scoped_ptr<base::HistogramSamples> samples(
+ histogram_recorder_->GetHistogramSamplesSinceCreation(
+ "SpellCheck.CustomWords"));
+ EXPECT_EQ(23, samples->sum());
}
TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
@@ -99,59 +85,37 @@ TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
metrics()->RecordCheckedWordStats(string16(ASCIIToUTF16("test")), false);
RecordWordCountsForTesting();
- // Get baselines for all affected histograms.
- scoped_ptr<HistogramSamples> baselines[arraysize(histogramName)];
- for (size_t i = 0; i < arraysize(histogramName); ++i) {
- HistogramBase* histogram =
- StatisticsRecorder::FindHistogram(histogramName[i]);
- if (histogram)
- baselines[i] = histogram->SnapshotSamples();
- }
+ // Restart the recorder.
+ ResetHistogramRecorder();
// Nothing changed, so this invocation should not affect any histograms.
RecordWordCountsForTesting();
// Get samples for all affected histograms.
- scoped_ptr<HistogramSamples> samples[arraysize(histogramName)];
+ scoped_ptr<base::HistogramSamples> samples;
for (size_t i = 0; i < arraysize(histogramName); ++i) {
- HistogramBase* histogram =
- StatisticsRecorder::FindHistogram(histogramName[i]);
- ASSERT_TRUE(histogram != NULL);
- samples[i] = histogram->SnapshotSamples();
- if (baselines[i].get())
- samples[i]->Subtract(*baselines[i]);
-
- EXPECT_EQ(0, samples[i]->TotalCount());
+ samples = histogram_recorder_->GetHistogramSamplesSinceCreation(
+ histogramName[i]);
+ EXPECT_EQ(0, samples->TotalCount());
}
}
TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) {
const char kMetricName[] = "SpellCheck.SpellingService.Enabled";
- scoped_ptr<HistogramSamples> baseline;
- HistogramBase* histogram = StatisticsRecorder::FindHistogram(kMetricName);
- if (histogram)
- baseline = histogram->SnapshotSamples();
metrics()->RecordSpellingServiceStats(false);
- histogram =
- StatisticsRecorder::FindHistogram(kMetricName);
- ASSERT_TRUE(histogram != NULL);
- scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
- if (baseline.get())
- samples->Subtract(*baseline);
+ scoped_ptr<base::HistogramSamples> samples(
+ histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName));
EXPECT_EQ(1, samples->GetCount(0));
EXPECT_EQ(0, samples->GetCount(1));
- baseline.reset(samples.release());
+ ResetHistogramRecorder();
metrics()->RecordSpellingServiceStats(true);
- histogram =
- StatisticsRecorder::FindHistogram(kMetricName);
- ASSERT_TRUE(histogram != NULL);
- samples = histogram->SnapshotSamples();
- samples->Subtract(*baseline);
+ samples =
+ histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName);
EXPECT_EQ(0, samples->GetCount(0));
EXPECT_EQ(1, samples->GetCount(1));
}

Powered by Google App Engine
This is Rietveld 408576698