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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" 5 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/metrics/histogram.h"
11 #include "base/metrics/histogram_samples.h" 10 #include "base/metrics/histogram_samples.h"
12 #include "base/metrics/statistics_recorder.h"
13 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/histogram_recorder.h"
14 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
15 14
16 using base::HistogramBase;
17 using base::HistogramSamples;
18 using base::StatisticsRecorder;
19
20 class SpellcheckHostMetricsTest : public testing::Test { 15 class SpellcheckHostMetricsTest : public testing::Test {
21 public: 16 public:
22 SpellcheckHostMetricsTest() : loop_(base::MessageLoop::TYPE_DEFAULT) { 17 SpellcheckHostMetricsTest() : loop_(base::MessageLoop::TYPE_DEFAULT) {
23 } 18 }
24 19
25 virtual void SetUp() OVERRIDE { 20 virtual void SetUp() OVERRIDE {
26 base::StatisticsRecorder::Initialize(); 21 ResetHistogramRecorder();
27 metrics_.reset(new SpellCheckHostMetrics); 22 metrics_.reset(new SpellCheckHostMetrics);
28 } 23 }
29 24
25 void ResetHistogramRecorder() {
26 histogram_recorder_.reset(new base::HistogramRecorder());
27 }
28
30 SpellCheckHostMetrics* metrics() { return metrics_.get(); } 29 SpellCheckHostMetrics* metrics() { return metrics_.get(); }
31 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); } 30 void RecordWordCountsForTesting() { metrics_->RecordWordCounts(); }
32 31
32 protected:
33 scoped_ptr<base::HistogramRecorder> histogram_recorder_;
34
33 private: 35 private:
34 base::MessageLoop loop_; 36 base::MessageLoop loop_;
35 scoped_ptr<SpellCheckHostMetrics> metrics_; 37 scoped_ptr<SpellCheckHostMetrics> metrics_;
36 }; 38 };
37 39
38 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) { 40 TEST_F(SpellcheckHostMetricsTest, RecordEnabledStats) {
39 scoped_ptr<HistogramSamples> baseline; 41 const char kMetricName[] = "SpellCheck.Enabled";
40 HistogramBase* histogram =
41 StatisticsRecorder::FindHistogram("SpellCheck.Enabled");
42 if (histogram)
43 baseline = histogram->SnapshotSamples();
44 42
45 metrics()->RecordEnabledStats(false); 43 metrics()->RecordEnabledStats(false);
46 44
47 histogram = 45 scoped_ptr<base::HistogramSamples> samples(
48 StatisticsRecorder::FindHistogram("SpellCheck.Enabled"); 46 histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName));
49 ASSERT_TRUE(histogram != NULL);
50 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
51 if (baseline.get())
52 samples->Subtract(*baseline);
53 EXPECT_EQ(1, samples->GetCount(0)); 47 EXPECT_EQ(1, samples->GetCount(0));
54 EXPECT_EQ(0, samples->GetCount(1)); 48 EXPECT_EQ(0, samples->GetCount(1));
55 49
56 baseline.reset(samples.release()); 50 ResetHistogramRecorder();
57 51
58 metrics()->RecordEnabledStats(true); 52 metrics()->RecordEnabledStats(true);
59 53
60 histogram = 54 samples =
61 StatisticsRecorder::FindHistogram("SpellCheck.Enabled"); 55 histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName);
62 ASSERT_TRUE(histogram != NULL);
63 samples = histogram->SnapshotSamples();
64 samples->Subtract(*baseline);
65 EXPECT_EQ(0, samples->GetCount(0)); 56 EXPECT_EQ(0, samples->GetCount(0));
66 EXPECT_EQ(1, samples->GetCount(1)); 57 EXPECT_EQ(1, samples->GetCount(1));
67 } 58 }
68 59
69 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { 60 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) {
70 SpellCheckHostMetrics::RecordCustomWordCountStats(123); 61 SpellCheckHostMetrics::RecordCustomWordCountStats(123);
71 62
72 HistogramBase* histogram = 63 ResetHistogramRecorder();
73 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords");
74 ASSERT_TRUE(histogram != NULL);
75 scoped_ptr<HistogramSamples> baseline = histogram->SnapshotSamples();
76 64
77 SpellCheckHostMetrics::RecordCustomWordCountStats(23); 65 SpellCheckHostMetrics::RecordCustomWordCountStats(23);
78 histogram =
79 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords");
80 ASSERT_TRUE(histogram != NULL);
81 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples();
82 66
83 samples->Subtract(*baseline); 67 scoped_ptr<base::HistogramSamples> samples(
84 EXPECT_EQ(23,samples->sum()); 68 histogram_recorder_->GetHistogramSamplesSinceCreation(
69 "SpellCheck.CustomWords"));
70 EXPECT_EQ(23, samples->sum());
85 } 71 }
86 72
87 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { 73 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) {
88 // This test ensures that RecordWordCounts only records metrics if they 74 // This test ensures that RecordWordCounts only records metrics if they
89 // have changed from the last invocation. 75 // have changed from the last invocation.
90 const char* histogramName[] = { 76 const char* histogramName[] = {
91 "SpellCheck.CheckedWords", 77 "SpellCheck.CheckedWords",
92 "SpellCheck.MisspelledWords", 78 "SpellCheck.MisspelledWords",
93 "SpellCheck.ReplacedWords", 79 "SpellCheck.ReplacedWords",
94 "SpellCheck.UniqueWords", 80 "SpellCheck.UniqueWords",
95 "SpellCheck.ShownSuggestions" 81 "SpellCheck.ShownSuggestions"
96 }; 82 };
97 83
98 // Ensure all histograms exist. 84 // Ensure all histograms exist.
99 metrics()->RecordCheckedWordStats(string16(ASCIIToUTF16("test")), false); 85 metrics()->RecordCheckedWordStats(string16(ASCIIToUTF16("test")), false);
100 RecordWordCountsForTesting(); 86 RecordWordCountsForTesting();
101 87
102 // Get baselines for all affected histograms. 88 // Restart the recorder.
103 scoped_ptr<HistogramSamples> baselines[arraysize(histogramName)]; 89 ResetHistogramRecorder();
104 for (size_t i = 0; i < arraysize(histogramName); ++i) {
105 HistogramBase* histogram =
106 StatisticsRecorder::FindHistogram(histogramName[i]);
107 if (histogram)
108 baselines[i] = histogram->SnapshotSamples();
109 }
110 90
111 // Nothing changed, so this invocation should not affect any histograms. 91 // Nothing changed, so this invocation should not affect any histograms.
112 RecordWordCountsForTesting(); 92 RecordWordCountsForTesting();
113 93
114 // Get samples for all affected histograms. 94 // Get samples for all affected histograms.
115 scoped_ptr<HistogramSamples> samples[arraysize(histogramName)]; 95 scoped_ptr<base::HistogramSamples> samples;
116 for (size_t i = 0; i < arraysize(histogramName); ++i) { 96 for (size_t i = 0; i < arraysize(histogramName); ++i) {
117 HistogramBase* histogram = 97 samples = histogram_recorder_->GetHistogramSamplesSinceCreation(
118 StatisticsRecorder::FindHistogram(histogramName[i]); 98 histogramName[i]);
119 ASSERT_TRUE(histogram != NULL); 99 EXPECT_EQ(0, samples->TotalCount());
120 samples[i] = histogram->SnapshotSamples();
121 if (baselines[i].get())
122 samples[i]->Subtract(*baselines[i]);
123
124 EXPECT_EQ(0, samples[i]->TotalCount());
125 } 100 }
126 } 101 }
127 102
128 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { 103 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) {
129 const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; 104 const char kMetricName[] = "SpellCheck.SpellingService.Enabled";
130 scoped_ptr<HistogramSamples> baseline;
131 HistogramBase* histogram = StatisticsRecorder::FindHistogram(kMetricName);
132 if (histogram)
133 baseline = histogram->SnapshotSamples();
134 105
135 metrics()->RecordSpellingServiceStats(false); 106 metrics()->RecordSpellingServiceStats(false);
136 107
137 histogram = 108 scoped_ptr<base::HistogramSamples> samples(
138 StatisticsRecorder::FindHistogram(kMetricName); 109 histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName));
139 ASSERT_TRUE(histogram != NULL);
140 scoped_ptr<HistogramSamples> samples(histogram->SnapshotSamples());
141 if (baseline.get())
142 samples->Subtract(*baseline);
143 EXPECT_EQ(1, samples->GetCount(0)); 110 EXPECT_EQ(1, samples->GetCount(0));
144 EXPECT_EQ(0, samples->GetCount(1)); 111 EXPECT_EQ(0, samples->GetCount(1));
145 112
146 baseline.reset(samples.release()); 113 ResetHistogramRecorder();
147 114
148 metrics()->RecordSpellingServiceStats(true); 115 metrics()->RecordSpellingServiceStats(true);
149 116
150 histogram = 117 samples =
151 StatisticsRecorder::FindHistogram(kMetricName); 118 histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName);
152 ASSERT_TRUE(histogram != NULL);
153 samples = histogram->SnapshotSamples();
154 samples->Subtract(*baseline);
155 EXPECT_EQ(0, samples->GetCount(0)); 119 EXPECT_EQ(0, samples->GetCount(0));
156 EXPECT_EQ(1, samples->GetCount(1)); 120 EXPECT_EQ(1, samples->GetCount(1));
157 } 121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698