OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include "base/md5.h" | |
10 #include "base/metrics/histogram.h" | |
11 | |
12 SpellCheckHostMetrics::SpellCheckHostMetrics() | |
13 : misspelled_word_count_(0), | |
14 last_misspelled_word_count_(-1), | |
15 spellchecked_word_count_(0), | |
16 last_spellchecked_word_count_(-1), | |
17 suggestion_show_count_(0), | |
18 last_suggestion_show_count_(-1), | |
19 replaced_word_count_(0), | |
20 last_replaced_word_count_(-1), | |
21 last_unique_word_count_(-1), | |
22 start_time_(base::TimeTicks::Now()) { | |
23 const uint64_t kHistogramTimerDurationInMinutes = 30; | |
24 recording_timer_.Start(FROM_HERE, | |
25 base::TimeDelta::FromMinutes(kHistogramTimerDurationInMinutes), | |
26 this, &SpellCheckHostMetrics::OnHistogramTimerExpired); | |
27 RecordWordCounts(); | |
28 } | |
29 | |
30 SpellCheckHostMetrics::~SpellCheckHostMetrics() { | |
31 } | |
32 | |
33 // static | |
34 void SpellCheckHostMetrics::RecordCustomWordCountStats(size_t count) { | |
35 UMA_HISTOGRAM_COUNTS("SpellCheck.CustomWords", count); | |
36 } | |
37 | |
38 void SpellCheckHostMetrics::RecordEnabledStats(bool enabled) { | |
39 UMA_HISTOGRAM_BOOLEAN("SpellCheck.Enabled", enabled); | |
40 // Because SpellCheckHost is instantiated lazily, the size of | |
41 // custom dictionary is unknown at this time. We mark it as -1 and | |
42 // record actual value later. See SpellCheckHost for more detail. | |
43 if (enabled) | |
44 RecordCustomWordCountStats(static_cast<size_t>(-1)); | |
45 } | |
46 | |
47 void SpellCheckHostMetrics::RecordCheckedWordStats(const base::string16& word, | |
48 bool misspell) { | |
49 spellchecked_word_count_++; | |
50 if (misspell) { | |
51 misspelled_word_count_++; | |
52 // If an user misspelled, that user should be counted as a part of | |
53 // the population. So we ensure to instantiate the histogram | |
54 // entries here at the first time. | |
55 if (misspelled_word_count_ == 1) | |
56 RecordReplacedWordStats(0); | |
57 } | |
58 | |
59 int percentage = (100 * misspelled_word_count_) / spellchecked_word_count_; | |
60 UMA_HISTOGRAM_PERCENTAGE("SpellCheck.MisspellRatio", percentage); | |
61 | |
62 // Collects actual number of checked words, excluding duplication. | |
63 base::MD5Digest digest; | |
64 base::MD5Sum(reinterpret_cast<const unsigned char*>(word.c_str()), | |
65 word.size() * sizeof(base::char16), &digest); | |
66 checked_word_hashes_.insert(base::MD5DigestToBase16(digest)); | |
67 | |
68 RecordWordCounts(); | |
69 } | |
70 | |
71 void SpellCheckHostMetrics::OnHistogramTimerExpired() { | |
72 if (0 < spellchecked_word_count_) { | |
73 // Collects word checking rate, which is represented | |
74 // as a word count per hour. | |
75 base::TimeDelta since_start = base::TimeTicks::Now() - start_time_; | |
76 // This shouldn't happen since OnHistogramTimerExpired() is called on | |
77 // a 30 minute interval. If the time was 0 we will end up dividing by zero. | |
78 CHECK_NE(0, since_start.InSeconds()); | |
79 size_t checked_words_per_hour = spellchecked_word_count_ * | |
80 base::TimeDelta::FromHours(1).InSeconds() / since_start.InSeconds(); | |
81 UMA_HISTOGRAM_COUNTS("SpellCheck.CheckedWordsPerHour", | |
82 checked_words_per_hour); | |
83 } | |
84 } | |
85 | |
86 void SpellCheckHostMetrics::RecordDictionaryCorruptionStats(bool corrupted) { | |
87 UMA_HISTOGRAM_BOOLEAN("SpellCheck.DictionaryCorrupted", corrupted); | |
88 } | |
89 | |
90 void SpellCheckHostMetrics::RecordSuggestionStats(int delta) { | |
91 suggestion_show_count_ += delta; | |
92 // RecordReplacedWordStats() Calls RecordWordCounts() eventually. | |
93 RecordReplacedWordStats(0); | |
94 } | |
95 | |
96 void SpellCheckHostMetrics::RecordReplacedWordStats(int delta) { | |
97 replaced_word_count_ += delta; | |
98 | |
99 if (misspelled_word_count_) { | |
100 // zero |misspelled_word_count_| is possible when an extension | |
101 // gives the misspelling, which is not recorded as a part of this | |
102 // metrics. | |
103 int percentage = (100 * replaced_word_count_) / misspelled_word_count_; | |
104 UMA_HISTOGRAM_PERCENTAGE("SpellCheck.ReplaceRatio", percentage); | |
105 } | |
106 | |
107 if (suggestion_show_count_) { | |
108 int percentage = (100 * replaced_word_count_) / suggestion_show_count_; | |
109 UMA_HISTOGRAM_PERCENTAGE("SpellCheck.SuggestionHitRatio", percentage); | |
110 } | |
111 | |
112 RecordWordCounts(); | |
113 } | |
114 | |
115 void SpellCheckHostMetrics::RecordWordCounts() { | |
116 if (spellchecked_word_count_ != last_spellchecked_word_count_) { | |
117 DCHECK(spellchecked_word_count_ > last_spellchecked_word_count_); | |
118 UMA_HISTOGRAM_COUNTS("SpellCheck.CheckedWords", spellchecked_word_count_); | |
119 last_spellchecked_word_count_ = spellchecked_word_count_; | |
120 } | |
121 | |
122 if (misspelled_word_count_ != last_misspelled_word_count_) { | |
123 DCHECK(misspelled_word_count_ > last_misspelled_word_count_); | |
124 UMA_HISTOGRAM_COUNTS("SpellCheck.MisspelledWords", misspelled_word_count_); | |
125 last_misspelled_word_count_ = misspelled_word_count_; | |
126 } | |
127 | |
128 if (replaced_word_count_ != last_replaced_word_count_) { | |
129 DCHECK(replaced_word_count_ > last_replaced_word_count_); | |
130 UMA_HISTOGRAM_COUNTS("SpellCheck.ReplacedWords", replaced_word_count_); | |
131 last_replaced_word_count_ = replaced_word_count_; | |
132 } | |
133 | |
134 if (((int)checked_word_hashes_.size()) != last_unique_word_count_) { | |
135 DCHECK((int)checked_word_hashes_.size() > last_unique_word_count_); | |
136 UMA_HISTOGRAM_COUNTS("SpellCheck.UniqueWords", checked_word_hashes_.size()); | |
137 last_unique_word_count_ = checked_word_hashes_.size(); | |
138 } | |
139 | |
140 if (suggestion_show_count_ != last_suggestion_show_count_) { | |
141 DCHECK(suggestion_show_count_ > last_suggestion_show_count_); | |
142 UMA_HISTOGRAM_COUNTS("SpellCheck.ShownSuggestions", suggestion_show_count_); | |
143 last_suggestion_show_count_ = suggestion_show_count_; | |
144 } | |
145 } | |
146 | |
147 void SpellCheckHostMetrics::RecordSpellingServiceStats(bool enabled) { | |
148 UMA_HISTOGRAM_BOOLEAN("SpellCheck.SpellingService.Enabled", enabled); | |
149 } | |
OLD | NEW |