OLD | NEW |
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.h" | 9 #include "base/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 = histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName); |
61 StatisticsRecorder::FindHistogram("SpellCheck.Enabled"); | |
62 ASSERT_TRUE(histogram != NULL); | |
63 samples = histogram->SnapshotSamples(); | |
64 samples->Subtract(*baseline); | |
65 EXPECT_EQ(0, samples->GetCount(0)); | 55 EXPECT_EQ(0, samples->GetCount(0)); |
66 EXPECT_EQ(1, samples->GetCount(1)); | 56 EXPECT_EQ(1, samples->GetCount(1)); |
67 } | 57 } |
68 | 58 |
69 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { | 59 TEST_F(SpellcheckHostMetricsTest, CustomWordStats) { |
70 SpellCheckHostMetrics::RecordCustomWordCountStats(123); | 60 SpellCheckHostMetrics::RecordCustomWordCountStats(123); |
71 | 61 |
72 HistogramBase* histogram = | 62 ResetHistogramRecorder(); |
73 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); | |
74 ASSERT_TRUE(histogram != NULL); | |
75 scoped_ptr<HistogramSamples> baseline = histogram->SnapshotSamples(); | |
76 | 63 |
77 SpellCheckHostMetrics::RecordCustomWordCountStats(23); | 64 SpellCheckHostMetrics::RecordCustomWordCountStats(23); |
78 histogram = | |
79 StatisticsRecorder::FindHistogram("SpellCheck.CustomWords"); | |
80 ASSERT_TRUE(histogram != NULL); | |
81 scoped_ptr<HistogramSamples> samples = histogram->SnapshotSamples(); | |
82 | 65 |
83 samples->Subtract(*baseline); | 66 scoped_ptr<base::HistogramSamples> samples( |
84 EXPECT_EQ(23,samples->sum()); | 67 histogram_recorder_->GetHistogramSamplesSinceCreation( |
| 68 "SpellCheck.CustomWords")); |
| 69 EXPECT_EQ(23, samples->sum()); |
85 } | 70 } |
86 | 71 |
87 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { | 72 TEST_F(SpellcheckHostMetricsTest, RecordWordCountsDiscardsDuplicates) { |
88 // This test ensures that RecordWordCounts only records metrics if they | 73 // This test ensures that RecordWordCounts only records metrics if they |
89 // have changed from the last invocation. | 74 // have changed from the last invocation. |
90 const char* histogramName[] = { | 75 const char* histogramName[] = { |
91 "SpellCheck.CheckedWords", | 76 "SpellCheck.CheckedWords", |
92 "SpellCheck.MisspelledWords", | 77 "SpellCheck.MisspelledWords", |
93 "SpellCheck.ReplacedWords", | 78 "SpellCheck.ReplacedWords", |
94 "SpellCheck.UniqueWords", | 79 "SpellCheck.UniqueWords", |
95 "SpellCheck.ShownSuggestions" | 80 "SpellCheck.ShownSuggestions" |
96 }; | 81 }; |
97 | 82 |
98 // Ensure all histograms exist. | 83 // Ensure all histograms exist. |
99 metrics()->RecordCheckedWordStats(string16(ASCIIToUTF16("test")), false); | 84 metrics()->RecordCheckedWordStats(string16(ASCIIToUTF16("test")), false); |
100 RecordWordCountsForTesting(); | 85 RecordWordCountsForTesting(); |
101 | 86 |
102 // Get baselines for all affected histograms. | 87 // Restart the recorder. |
103 scoped_ptr<HistogramSamples> baselines[arraysize(histogramName)]; | 88 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 | 89 |
111 // Nothing changed, so this invocation should not affect any histograms. | 90 // Nothing changed, so this invocation should not affect any histograms. |
112 RecordWordCountsForTesting(); | 91 RecordWordCountsForTesting(); |
113 | 92 |
114 // Get samples for all affected histograms. | 93 // Get samples for all affected histograms. |
115 scoped_ptr<HistogramSamples> samples[arraysize(histogramName)]; | 94 scoped_ptr<base::HistogramSamples> samples; |
116 for (size_t i = 0; i < arraysize(histogramName); ++i) { | 95 for (size_t i = 0; i < arraysize(histogramName); ++i) { |
117 HistogramBase* histogram = | 96 samples = histogram_recorder_->GetHistogramSamplesSinceCreation( |
118 StatisticsRecorder::FindHistogram(histogramName[i]); | 97 histogramName[i]); |
119 ASSERT_TRUE(histogram != NULL); | 98 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 } | 99 } |
126 } | 100 } |
127 | 101 |
128 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { | 102 TEST_F(SpellcheckHostMetricsTest, RecordSpellingServiceStats) { |
129 const char kMetricName[] = "SpellCheck.SpellingService.Enabled"; | 103 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 | 104 |
135 metrics()->RecordSpellingServiceStats(false); | 105 metrics()->RecordSpellingServiceStats(false); |
136 | 106 |
137 histogram = | 107 scoped_ptr<base::HistogramSamples> samples( |
138 StatisticsRecorder::FindHistogram(kMetricName); | 108 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)); | 109 EXPECT_EQ(1, samples->GetCount(0)); |
144 EXPECT_EQ(0, samples->GetCount(1)); | 110 EXPECT_EQ(0, samples->GetCount(1)); |
145 | 111 |
146 baseline.reset(samples.release()); | 112 ResetHistogramRecorder(); |
147 | 113 |
148 metrics()->RecordSpellingServiceStats(true); | 114 metrics()->RecordSpellingServiceStats(true); |
149 | 115 |
150 histogram = | 116 samples = histogram_recorder_->GetHistogramSamplesSinceCreation(kMetricName); |
151 StatisticsRecorder::FindHistogram(kMetricName); | |
152 ASSERT_TRUE(histogram != NULL); | |
153 samples = histogram->SnapshotSamples(); | |
154 samples->Subtract(*baseline); | |
155 EXPECT_EQ(0, samples->GetCount(0)); | 117 EXPECT_EQ(0, samples->GetCount(0)); |
156 EXPECT_EQ(1, samples->GetCount(1)); | 118 EXPECT_EQ(1, samples->GetCount(1)); |
157 } | 119 } |
OLD | NEW |