Chromium Code Reviews| OLD | NEW |
|---|---|
| (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" | |
|
mmenke
2013/10/30 18:29:08
No need to include stuff here that's in the header
sclittle
2013/10/30 20:29:48
Removed it from the header.
| |
| 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.DailyPrecachedKB", | |
| 23 stats.precached_bytes / 1024); | |
| 24 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedKB", | |
| 25 stats.downloaded_bytes / 1024); | |
| 26 UMA_HISTOGRAM_COUNTS("Precache.DailyDownloadedKB.Cellular", | |
| 27 stats.downloaded_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_bytes > 0) { | |
| 33 UMA_HISTOGRAM_PERCENTAGE("Precache.DailySavingsPercentage", | |
| 34 stats.saved_bytes * 100.0 / | |
| 35 (stats.saved_bytes + stats.downloaded_bytes)); | |
| 36 } | |
| 37 | |
| 38 if (stats.saved_bytes_cellular + stats.downloaded_bytes_cellular > 0) { | |
| 39 UMA_HISTOGRAM_PERCENTAGE( | |
| 40 "Precache.DailySavingsPercentage.Cellular", | |
| 41 stats.saved_bytes_cellular * 100.0 / | |
| 42 (stats.saved_bytes_cellular + stats.downloaded_bytes_cellular)); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 namespace precache { | |
| 49 | |
| 50 PrecacheDatabase::PrecacheDatabase() | |
| 51 : precache_url_table_(new PrecacheURLTable()), | |
| 52 precache_statistics_table_(new PrecacheStatisticsTable()) { | |
| 53 // A PrecacheDatabase can be constructed on any thread. | |
| 54 DetachFromThread(); | |
| 55 } | |
| 56 | |
| 57 PrecacheDatabase::~PrecacheDatabase() { | |
| 58 // Since the PrecacheDatabase is refcounted, it will only be deleted if there | |
| 59 // are no references remaining to it, meaning that it is not in use. Thus, it | |
| 60 // is safe to delete it, regardless of what thread we are on. | |
| 61 DetachFromThread(); | |
| 62 } | |
| 63 | |
| 64 void PrecacheDatabase::Init(scoped_ptr<sql::Connection> db) { | |
| 65 DCHECK(CalledOnValidThread()); | |
| 66 DCHECK(!db_); // Init must only be called once. | |
| 67 DCHECK(db); // |db| must not be NULL. | |
| 68 | |
| 69 db_.reset(db.release()); | |
| 70 if (!IsDatabaseAccessible()) { | |
| 71 // Don't initialize the URL table or statistics table if unable to access | |
| 72 // the database. | |
| 73 return; | |
| 74 } | |
| 75 | |
| 76 precache_url_table_->Init(db_.get()); | |
| 77 precache_statistics_table_->Init(db_.get()); | |
| 78 } | |
| 79 | |
| 80 void PrecacheDatabase::ReportAndDeleteOldStats(base::Time end_date) { | |
| 81 DCHECK(CalledOnValidThread()); | |
| 82 DCHECK(db_); | |
| 83 | |
| 84 if (!IsDatabaseAccessible()) { | |
| 85 // Do nothing if unable to access the database. | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 // Delete old precache history that has expired. | |
| 90 precache_url_table_->DeleteAllPrecachedBetween( | |
| 91 base::Time(), | |
| 92 end_date - base::TimeDelta::FromDays(kPrecacheHistoryExpiryPeriodDays)); | |
| 93 | |
| 94 PrecacheStatisticsTable::PrecacheStatisticsMap stats_map; | |
| 95 precache_statistics_table_->GetAllStatsBetween(base::Time(), end_date, | |
| 96 &stats_map); | |
| 97 | |
| 98 // Report UMA for every row of old statistics in the statistics table. There | |
| 99 // won't be any rows in the statistics table for days when nothing was fetched | |
| 100 // or precached. | |
| 101 for (PrecacheStatisticsTable::PrecacheStatisticsMap::const_iterator it = | |
| 102 stats_map.begin(); | |
| 103 it != stats_map.end(); ++it) { | |
| 104 RecordSingleDayPrecacheUMA(it->second); | |
| 105 } | |
|
mmenke
2013/10/30 18:29:08
suggestion: Just report stats for the previous da
sclittle
2013/10/30 20:29:48
You have a good point, but we'd continue to get ol
mmenke
2013/10/31 16:44:59
I'm not so sure, though you are certainly correct
| |
| 106 | |
| 107 precache_statistics_table_->DeleteAllStatsBetween(base::Time(), end_date); | |
| 108 } | |
| 109 | |
| 110 void PrecacheDatabase::RecordURLFetched(GURL url, base::Time fetch_time, | |
| 111 int64 size, bool was_cached, | |
| 112 bool is_precaching, bool is_cellular) { | |
| 113 DCHECK(CalledOnValidThread()); | |
| 114 DCHECK(db_); | |
| 115 | |
| 116 if (!IsDatabaseAccessible()) { | |
| 117 // Don't track anything if unable to access the database. | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 if (is_precaching) { | |
| 122 if (was_cached && !precache_url_table_->HasURL(url)) { | |
| 123 // Since the fetch came from the cache, and there's no entry in the | |
| 124 // URL table for the URL, this means that the resource was in the cache | |
| 125 // because of user browsing. Thus, precaching this resource had no effect, | |
| 126 // so ignore it. | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 if (!was_cached) { | |
| 131 // The precache only counts as overhead if it was downloaded over the | |
| 132 // network. | |
| 133 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 134 stats.precached_bytes = size; | |
|
mmenke
2013/10/30 18:29:08
I'm still not following....These bytes were not in
sclittle
2013/10/30 20:29:48
Renamed "is_precaching" to "is_precache" to hopefu
| |
| 135 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats); | |
| 136 } | |
| 137 | |
| 138 precache_url_table_->AddURL(url, fetch_time); | |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 if (!was_cached) { | |
| 143 // The fetch was served over the network, so count it as downloaded bytes. | |
| 144 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 145 stats.downloaded_bytes = size; | |
| 146 if (is_cellular) { | |
| 147 stats.downloaded_bytes_cellular = size; | |
| 148 } | |
| 149 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats); | |
| 150 | |
| 151 // Since the fetch was over the network, delete any record of it having been | |
| 152 // precached from the URL table. If it had been precached, the resource must | |
| 153 // have expired. | |
| 154 precache_url_table_->DeleteURL(url); | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 if (was_cached && precache_url_table_->HasURL(url)) { | |
| 159 // The fetch was served from the cache, and since there's an entry for this | |
| 160 // URL in the URL table, this means that the resource was served from the | |
| 161 // cache only because precaching put it there. Thus, precaching was helpful, | |
| 162 // so count the fetch as saved bytes. | |
| 163 PrecacheStatisticsTable::PrecacheStatistics stats; | |
| 164 stats.saved_bytes = size; | |
| 165 if (is_cellular) { | |
| 166 stats.saved_bytes_cellular = size; | |
| 167 } | |
| 168 precache_statistics_table_->IncreaseDailyStats(fetch_time, stats); | |
| 169 | |
| 170 precache_url_table_->DeleteURL(url); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 bool PrecacheDatabase::IsDatabaseAccessible() const { | |
| 175 DCHECK(CalledOnValidThread()); | |
| 176 DCHECK(db_); | |
| 177 | |
| 178 return db_->is_open(); | |
| 179 } | |
| 180 | |
| 181 } // namespace precache | |
| OLD | NEW |