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

Side by Side Diff: components/precache/core/precache_database.cc

Issue 27047003: Precache tracking database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@precache
Patch Set: Reduced CL down to just the PrecacheDatabase Created 7 years, 1 month 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
OLDNEW
(Empty)
1 // Copyright 2013 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 "components/precache/core/precache_database.h"
6
7 #include "base/metrics/histogram.h"
8 #include "base/time/time.h"
9 #include "components/precache/core/precache_statistics_table.h"
10 #include "components/precache/core/precache_url_table.h"
11 #include "sql/connection.h"
12 #include "url/gurl.h"
13
14 namespace {
15
16 // The number of days old that an entry in the precache URL table can be before
17 // it is considered "old" and is removed from the table.
18 const int64 kPrecacheHistoryExpiryPeriodDays = 60;
19
20 void RecordSingleDayPrecacheUMA(
21 const precache::PrecacheStatisticsTable::PrecacheStatistics& stats) {
22 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedPrecacheMotivatedKB",
23 stats.downloaded_precache_motivated_bytes / 1024);
24 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedNonPrecacheKB",
25 stats.downloaded_non_precache_bytes / 1024);
26 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedNonPrecacheKB.Cellular",
27 stats.downloaded_non_precache_bytes_cellular / 1024);
28 UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB", stats.saved_bytes / 1024);
29 UMA_HISTOGRAM_COUNTS("Precache.DailySavedKB.Cellular",
30 stats.saved_bytes_cellular / 1024);
31
32 if (stats.saved_bytes + stats.downloaded_non_precache_bytes > 0) {
33 UMA_HISTOGRAM_PERCENTAGE(
34 "Precache.DailySavingsPercentage",
35 stats.saved_bytes * 100.0 /
36 (stats.saved_bytes + stats.downloaded_non_precache_bytes));
37 }
38
39 if (stats.saved_bytes_cellular +
40 stats.downloaded_non_precache_bytes_cellular > 0) {
41 UMA_HISTOGRAM_PERCENTAGE(
42 "Precache.DailySavingsPercentage.Cellular",
43 stats.saved_bytes_cellular * 100.0 /
44 (stats.saved_bytes_cellular +
45 stats.downloaded_non_precache_bytes_cellular));
46 }
47 }
48
49 } // namespace
50
51 namespace precache {
52
53 PrecacheDatabase::PrecacheDatabase()
54 : precache_url_table_(new PrecacheURLTable()),
55 precache_statistics_table_(new PrecacheStatisticsTable()) {
56 // A PrecacheDatabase can be constructed on any thread.
57 DetachFromThread();
58 }
59
60 PrecacheDatabase::~PrecacheDatabase() {
61 // Since the PrecacheDatabase is refcounted, it will only be deleted if there
62 // are no references remaining to it, meaning that it is not in use. Thus, it
63 // is safe to delete it, regardless of what thread we are on.
64 DetachFromThread();
65 }
66
67 void PrecacheDatabase::Init(scoped_ptr<sql::Connection> db) {
68 DCHECK(CalledOnValidThread());
69 DCHECK(!db_); // Init must only be called once.
70 DCHECK(db); // |db| must not be NULL.
71
72 db_.reset(db.release());
davidben 2013/11/06 23:50:53 Nit: You can write "db_ = db.Pass();" which is a s
sclittle 2013/11/09 01:07:58 Done.
73 if (!IsDatabaseAccessible()) {
74 // Don't initialize the URL table or statistics table if unable to access
75 // the database.
76 return;
77 }
78
79 precache_url_table_->Init(db_.get());
80 precache_statistics_table_->Init(db_.get());
81 }
82
83 void PrecacheDatabase::ReportAndDeleteOldStats(const base::Time& end_date) {
84 DCHECK(CalledOnValidThread());
85 DCHECK(db_);
86
87 if (!IsDatabaseAccessible()) {
88 // Do nothing if unable to access the database.
89 return;
90 }
91
92 // Delete old precache history that has expired.
93 precache_url_table_->DeleteAllPrecachedUntil(
94 end_date - base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays));
95
96 PrecacheStatisticsTable::PrecacheStatisticsMap stats_map;
97 precache_statistics_table_->GetAllStatsUntil(end_date, &stats_map);
98
99 // Report UMA for every row of old statistics in the statistics table. There
100 // won't be any rows in the statistics table for days when nothing was fetched
101 // or precached.
102 for (PrecacheStatisticsTable::PrecacheStatisticsMap::const_iterator it =
103 stats_map.begin();
104 it != stats_map.end(); ++it) {
105 RecordSingleDayPrecacheUMA(it->second);
106 }
107
108 precache_statistics_table_->DeleteAllStatsUntil(end_date);
109 }
110
111 void PrecacheDatabase::RecordURLFetched(const GURL& url,
112 const base::Time& fetch_time,
113 int64 size, bool was_cached,
114 bool is_precache,
115 bool is_connection_cellular) {
116 DCHECK(CalledOnValidThread());
117 DCHECK(db_);
118
119 if (!IsDatabaseAccessible()) {
120 // Don't track anything if unable to access the database.
121 return;
122 }
123
124 if (is_precache) {
125 if (was_cached && !precache_url_table_->HasURL(url)) {
126 // Since the precache came from the cache, and there's no entry in the
127 // URL table for the URL, this means that the resource was already in the
128 // cache because of user browsing. Thus, this precache had no effect, so
129 // ignore it.
130 return;
131 }
132
133 if (!was_cached) {
134 // The precache only counts as overhead if it was downloaded over the
135 // network.
136 PrecacheStatisticsTable::PrecacheStatistics stats;
137 stats.downloaded_precache_motivated_bytes = size;
138 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats);
139 }
140
141 // Use the URL table to keep track of URLs that are in the cache thanks to
142 // precaching. If a row for the URL already exists, than update the
143 // timestamp to |fetch_time|.
144 precache_url_table_->AddURL(url, fetch_time);
145 return;
146 }
147
148 if (!was_cached) {
149 // The fetch was served over the network during user browsing, so count it
150 // as downloaded non-precache bytes.
151 PrecacheStatisticsTable::PrecacheStatistics stats;
152 stats.downloaded_non_precache_bytes = size;
153 if (is_connection_cellular) {
154 stats.downloaded_non_precache_bytes_cellular = size;
155 }
156 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats);
157
158 // Since the fetch was over the network during user browsing, delete any
159 // record of it having been precached from the URL table. If it had been
160 // precached, the resource must have expired.
161 precache_url_table_->DeleteURL(url);
davidben 2013/11/06 23:50:53 I think there are cases where requests also just b
sclittle 2013/11/09 01:07:58 I'm not familiar with these cases; what does it me
davidben 2013/11/11 19:06:26 By external I mean the headers originated from out
sclittle 2013/11/19 04:03:41 Interesting, thanks for the info.
162 return;
163 }
164
165 if (was_cached && precache_url_table_->HasURL(url)) {
166 // The fetch was served from the cache, and since there's an entry for this
167 // URL in the URL table, this means that the resource was served from the
168 // cache only because precaching put it there. Thus, precaching was helpful,
169 // so count the fetch as saved bytes.
170 PrecacheStatisticsTable::PrecacheStatistics stats;
171 stats.saved_bytes = size;
172 if (is_connection_cellular) {
173 stats.saved_bytes_cellular = size;
174 }
175 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats);
176
177 // The current fetch would have put this resource in the cache, so precache
178 // can't take credit for subsequent fetches of this resource until the next
179 // time that the resource is precached over the network.
180 precache_url_table_->DeleteURL(url);
davidben 2013/11/06 23:50:53 You could probably move the IncreaseDailyStats and
sclittle 2013/11/09 01:07:58 Done.
181 }
182 }
183
184 bool PrecacheDatabase::IsDatabaseAccessible() const {
185 DCHECK(CalledOnValidThread());
186 DCHECK(db_);
187
188 return db_->is_open();
189 }
190
191 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698