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

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

Issue 27047003: Precache tracking database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@precache
Patch Set: Changed to report metrics per-resource, and addressed comments Created 7 years 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 <map>
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/metrics/histogram.h"
14 #include "base/metrics/histogram_samples.h"
15 #include "base/metrics/statistics_recorder.h"
16 #include "base/time/time.h"
17 #include "components/precache/core/precache_url_table.h"
18 #include "sql/connection.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h"
21
22 namespace {
23
24 const GURL kURL("http://url.com");
25 const base::Time kFetchTime = base::Time() + base::TimeDelta::FromHours(1000);
26 const base::Time kOldFetchTime = kFetchTime - base::TimeDelta::FromDays(1);
27 const int64 kSize = 5000;
28
29 const char* kHistogramNames[] = {"Precache.DownloadedPrecacheMotivated",
30 "Precache.DownloadedNonPrecache",
31 "Precache.DownloadedNonPrecache.Cellular",
32 "Precache.Saved",
33 "Precache.Saved.Cellular"};
34
35 scoped_ptr<base::HistogramSamples> GetHistogramSamples(
36 const char* histogram_name) {
37 base::HistogramBase* histogram =
38 base::StatisticsRecorder::FindHistogram(histogram_name);
39
40 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram);
41
42 return histogram->SnapshotSamples().Pass();
43 }
44
45 std::map<GURL, base::Time> BuildURLTableMap(const GURL& url,
46 const base::Time& precache_time) {
47 std::map<GURL, base::Time> url_table_map;
48 url_table_map[url] = precache_time;
49 return url_table_map;
50 }
51
52 } // namespace
53
54 namespace precache {
55
56 class PrecacheDatabaseTest : public testing::Test {
57 public:
58 PrecacheDatabaseTest() {}
59 virtual ~PrecacheDatabaseTest() {}
60
61 protected:
62 virtual void SetUp() OVERRIDE {
63 base::StatisticsRecorder::Initialize();
64 precache_database_ = new PrecacheDatabase();
65
66 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
67 base::FilePath db_path = scoped_temp_dir_.path().Append(
68 base::FilePath(FILE_PATH_LITERAL("precache_database")));
69 precache_database_->Init(db_path);
70
71 // Log a sample for each histogram, to ensure that they are all created.
72 // This has to be done here, and not in the for loop below, because of the
73 // way that UMA_HISTOGRAM_COUNTS uses static variables.
74 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", 0);
75 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", 0);
76 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", 0);
77 UMA_HISTOGRAM_COUNTS("Precache.Saved", 0);
78 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", 0);
79
80 for (size_t i = 0; i < arraysize(kHistogramNames); i++) {
81 initial_histogram_samples_[i] =
82 GetHistogramSamples(kHistogramNames[i]).Pass();
83 initial_histogram_samples_map_[kHistogramNames[i]] =
84 initial_histogram_samples_[i].get();
85 }
86 }
87
88 std::map<GURL, base::Time> GetActualURLTableMap() {
89 std::map<GURL, base::Time> url_table_map;
90 precache_database_->precache_url_table_->GetAllDataForTesting(
Scott Hess - ex-Googler 2013/12/03 20:55:38 precache_url_table()-> seems reasonable for this.
sclittle 2013/12/04 19:07:10 Done.
91 &url_table_map);
92 return url_table_map;
93 }
94
95 PrecacheURLTable* precache_url_table() {
96 return precache_database_->precache_url_table_.get();
97 }
98
99 scoped_ptr<base::HistogramSamples> GetHistogramSamplesDelta(
100 const char* histogram_name) {
101 scoped_ptr<base::HistogramSamples> delta_samples(
102 GetHistogramSamples(histogram_name));
103 delta_samples->Subtract(*initial_histogram_samples_map_[histogram_name]);
104
105 return delta_samples.Pass();
106 }
107
108 void ExpectNewSample(const char* histogram_name,
109 base::HistogramBase::Sample sample) {
110 scoped_ptr<base::HistogramSamples> delta_samples(
111 GetHistogramSamplesDelta(histogram_name));
112 EXPECT_EQ(1, delta_samples->TotalCount());
113 EXPECT_EQ(1, delta_samples->GetCount(sample));
114 }
115
116 void ExpectNoNewSamples(const char* histogram_name) {
117 scoped_ptr<base::HistogramSamples> delta_samples(
118 GetHistogramSamplesDelta(histogram_name));
119 EXPECT_EQ(0, delta_samples->TotalCount());
120 }
121
122 // Convenience methods for recording different types of URL fetches. These
123 // exist to improve the readability of the tests.
124 void RecordPrecacheFromNetwork(const GURL& url, const base::Time& fetch_time,
125 int64 size);
126 void RecordPrecacheFromCache(const GURL& url, const base::Time& fetch_time,
127 int64 size);
128 void RecordFetchFromNetwork(const GURL& url, const base::Time& fetch_time,
129 int64 size);
130 void RecordFetchFromNetworkCellular(const GURL& url,
131 const base::Time& fetch_time, int64 size);
132 void RecordFetchFromCache(const GURL& url, const base::Time& fetch_time,
133 int64 size);
134 void RecordFetchFromCacheCellular(const GURL& url,
135 const base::Time& fetch_time, int64 size);
136
137 scoped_refptr<PrecacheDatabase> precache_database_;
138
139 base::ScopedTempDir scoped_temp_dir_;
140
141 scoped_ptr<base::HistogramSamples> initial_histogram_samples_
142 [arraysize(kHistogramNames)];
143 std::map<std::string, base::HistogramSamples*> initial_histogram_samples_map_;
144 };
145
146 void PrecacheDatabaseTest::RecordPrecacheFromNetwork(
147 const GURL& url, const base::Time& fetch_time, int64 size) {
148 precache_database_->RecordURLPrecached(url, fetch_time, size,
149 false /* was_cached */);
150 }
151
152 void PrecacheDatabaseTest::RecordPrecacheFromCache(const GURL& url,
153 const base::Time& fetch_time,
154 int64 size) {
155 precache_database_->RecordURLPrecached(url, fetch_time, size,
156 true /* was_cached */);
157 }
158
159 void PrecacheDatabaseTest::RecordFetchFromNetwork(const GURL& url,
160 const base::Time& fetch_time,
161 int64 size) {
162 precache_database_->RecordURLFetched(url, fetch_time, size,
163 false /* was_cached */,
164 false /* is_connection_cellular */);
165 }
166
167 void PrecacheDatabaseTest::RecordFetchFromNetworkCellular(
168 const GURL& url, const base::Time& fetch_time, int64 size) {
169 precache_database_->RecordURLFetched(url, fetch_time, size,
170 false /* was_cached */,
171 true /* is_connection_cellular */);
172 }
173
174 void PrecacheDatabaseTest::RecordFetchFromCache(const GURL& url,
175 const base::Time& fetch_time,
176 int64 size) {
177 precache_database_->RecordURLFetched(url, fetch_time, size,
178 true /* was_cached */,
179 false /* is_connection_cellular */);
Scott Hess - ex-Googler 2013/12/03 20:55:38 I was going to comment on this, but then decided n
sclittle 2013/12/04 19:07:10 You have a point, but I think I'll just keep them
180 }
181
182 void PrecacheDatabaseTest::RecordFetchFromCacheCellular(
183 const GURL& url, const base::Time& fetch_time, int64 size) {
184 precache_database_->RecordURLFetched(url, fetch_time, size,
185 true /* was_cached */,
186 true /* is_connection_cellular */);
187 }
188
189 namespace {
190
191 TEST_F(PrecacheDatabaseTest, PrecacheOverNetwork) {
192 RecordPrecacheFromNetwork(kURL, kFetchTime, kSize);
193
194 EXPECT_EQ(BuildURLTableMap(kURL, kFetchTime), GetActualURLTableMap());
195
196 ExpectNewSample("Precache.DownloadedPrecacheMotivated", kSize);
197 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
198 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
199 ExpectNoNewSamples("Precache.Saved");
200 ExpectNoNewSamples("Precache.Saved.Cellular");
201 }
202
203 TEST_F(PrecacheDatabaseTest, PrecacheFromCacheWithURLTableEntry) {
204 precache_url_table()->AddURL(kURL, kOldFetchTime);
205 RecordPrecacheFromCache(kURL, kFetchTime, kSize);
206
207 // The URL table entry should have been updated to have |kFetchTime| as the
208 // timestamp.
209 EXPECT_EQ(BuildURLTableMap(kURL, kFetchTime), GetActualURLTableMap());
210
211 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
212 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
213 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
214 ExpectNoNewSamples("Precache.Saved");
215 ExpectNoNewSamples("Precache.Saved.Cellular");
216 }
217
218 TEST_F(PrecacheDatabaseTest, PrecacheFromCacheWithoutURLTableEntry) {
219 RecordPrecacheFromCache(kURL, kFetchTime, kSize);
220
221 EXPECT_TRUE(GetActualURLTableMap().empty());
222
223 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
224 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
225 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
226 ExpectNoNewSamples("Precache.Saved");
227 ExpectNoNewSamples("Precache.Saved.Cellular");
228 }
229
230 TEST_F(PrecacheDatabaseTest, FetchOverNetwork_NonCellular) {
231 RecordFetchFromNetwork(kURL, kFetchTime, kSize);
232
233 EXPECT_TRUE(GetActualURLTableMap().empty());
234
235 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
236 ExpectNewSample("Precache.DownloadedNonPrecache", kSize);
237 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
238 ExpectNoNewSamples("Precache.Saved");
239 ExpectNoNewSamples("Precache.Saved.Cellular");
240 }
241
242 TEST_F(PrecacheDatabaseTest, FetchOverNetwork_Cellular) {
243 RecordFetchFromNetworkCellular(kURL, kFetchTime, kSize);
244
245 EXPECT_TRUE(GetActualURLTableMap().empty());
246
247 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
248 ExpectNewSample("Precache.DownloadedNonPrecache", kSize);
249 ExpectNewSample("Precache.DownloadedNonPrecache.Cellular", kSize);
250 ExpectNoNewSamples("Precache.Saved");
251 ExpectNoNewSamples("Precache.Saved.Cellular");
252 }
253
254 TEST_F(PrecacheDatabaseTest, FetchOverNetworkWithURLTableEntry) {
255 precache_url_table()->AddURL(kURL, kOldFetchTime);
256 RecordFetchFromNetwork(kURL, kFetchTime, kSize);
257
258 // The URL table entry should have been deleted.
259 EXPECT_TRUE(GetActualURLTableMap().empty());
260
261 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
262 ExpectNewSample("Precache.DownloadedNonPrecache", kSize);
263 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
264 ExpectNoNewSamples("Precache.Saved");
265 ExpectNoNewSamples("Precache.Saved.Cellular");
266 }
267
268 TEST_F(PrecacheDatabaseTest, FetchFromCacheWithURLTableEntry_NonCellular) {
269 precache_url_table()->AddURL(kURL, kOldFetchTime);
270 RecordFetchFromCache(kURL, kFetchTime, kSize);
271
272 // The URL table entry should have been deleted.
273 EXPECT_TRUE(GetActualURLTableMap().empty());
274
275 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
276 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
277 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
278 ExpectNewSample("Precache.Saved", kSize);
279 ExpectNoNewSamples("Precache.Saved.Cellular");
280 }
281
282 TEST_F(PrecacheDatabaseTest, FetchFromCacheWithURLTableEntry_Cellular) {
283 precache_url_table()->AddURL(kURL, kOldFetchTime);
284 RecordFetchFromCacheCellular(kURL, kFetchTime, kSize);
285
286 // The URL table entry should have been deleted.
287 EXPECT_TRUE(GetActualURLTableMap().empty());
288
289 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
290 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
291 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
292 ExpectNewSample("Precache.Saved", kSize);
293 ExpectNewSample("Precache.Saved.Cellular", kSize);
294 }
295
296 TEST_F(PrecacheDatabaseTest, FetchFromCacheWithoutURLTableEntry) {
297 RecordFetchFromCache(kURL, kFetchTime, kSize);
298
299 EXPECT_TRUE(GetActualURLTableMap().empty());
300
301 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
302 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
303 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
304 ExpectNoNewSamples("Precache.Saved");
305 ExpectNoNewSamples("Precache.Saved.Cellular");
306 }
307
308 TEST_F(PrecacheDatabaseTest, DeleteExpiredPrecacheHistory) {
309 const base::Time kToday = base::Time() + base::TimeDelta::FromDays(1000);
310 const base::Time k59DaysAgo = kToday - base::TimeDelta::FromDays(59);
311 const base::Time k61DaysAgo = kToday - base::TimeDelta::FromDays(61);
312
313 precache_url_table()->AddURL(GURL("http://expired-precache.com"), k61DaysAgo);
314 precache_url_table()->AddURL(GURL("http://old-precache.com"), k59DaysAgo);
315
316 precache_database_->DeleteExpiredPrecacheHistory(kToday);
317
318 EXPECT_EQ(BuildURLTableMap(GURL("http://old-precache.com"), k59DaysAgo),
319 GetActualURLTableMap());
320 }
321
322 TEST_F(PrecacheDatabaseTest, SampleInteraction) {
323 const GURL kURL1("http://url1.com");
324 const int64 kSize1 = 1000;
325 const GURL kURL2("http://url2.com");
326 const int64 kSize2 = 2000;
327 const GURL kURL3("http://url3.com");
328 const int64 kSize3 = 3000;
329 const GURL kURL4("http://url4.com");
330 const int64 kSize4 = 4000;
331 const GURL kURL5("http://url5.com");
332 const int64 kSize5 = 5000;
333
334 RecordPrecacheFromNetwork(kURL1, kFetchTime, kSize1);
335 RecordPrecacheFromNetwork(kURL2, kFetchTime, kSize2);
336 RecordPrecacheFromNetwork(kURL3, kFetchTime, kSize3);
337 RecordPrecacheFromNetwork(kURL4, kFetchTime, kSize4);
338
339 RecordFetchFromCacheCellular(kURL1, kFetchTime, kSize1);
340 RecordFetchFromCacheCellular(kURL1, kFetchTime, kSize1);
341 RecordFetchFromNetworkCellular(kURL2, kFetchTime, kSize2);
342 RecordFetchFromNetworkCellular(kURL5, kFetchTime, kSize5);
343 RecordFetchFromCacheCellular(kURL5, kFetchTime, kSize5);
344
345 RecordPrecacheFromCache(kURL1, kFetchTime, kSize1);
346 RecordPrecacheFromNetwork(kURL2, kFetchTime, kSize2);
347 RecordPrecacheFromCache(kURL3, kFetchTime, kSize3);
348 RecordPrecacheFromCache(kURL4, kFetchTime, kSize4);
349
350 RecordFetchFromCache(kURL1, kFetchTime, kSize1);
351 RecordFetchFromNetwork(kURL2, kFetchTime, kSize2);
352 RecordFetchFromCache(kURL3, kFetchTime, kSize3);
353 RecordFetchFromCache(kURL5, kFetchTime, kSize5);
354
355 scoped_ptr<base::HistogramSamples> downloaded_precache_motivated_bytes(
356 GetHistogramSamplesDelta("Precache.DownloadedPrecacheMotivated"));
357 EXPECT_EQ(5, downloaded_precache_motivated_bytes->TotalCount());
358 EXPECT_EQ(1, downloaded_precache_motivated_bytes->GetCount(kSize1));
359 EXPECT_EQ(2, downloaded_precache_motivated_bytes->GetCount(kSize2));
360 EXPECT_EQ(1, downloaded_precache_motivated_bytes->GetCount(kSize3));
361 EXPECT_EQ(1, downloaded_precache_motivated_bytes->GetCount(kSize4));
362
363 scoped_ptr<base::HistogramSamples> downloaded_non_precache_bytes(
364 GetHistogramSamplesDelta("Precache.DownloadedNonPrecache"));
365 EXPECT_EQ(3, downloaded_non_precache_bytes->TotalCount());
366 EXPECT_EQ(2, downloaded_non_precache_bytes->GetCount(kSize2));
367 EXPECT_EQ(1, downloaded_non_precache_bytes->GetCount(kSize5));
368
369 scoped_ptr<base::HistogramSamples> downloaded_non_precache_bytes_cellular(
370 GetHistogramSamplesDelta("Precache.DownloadedNonPrecache.Cellular"));
371 EXPECT_EQ(2, downloaded_non_precache_bytes_cellular->TotalCount());
372 EXPECT_EQ(1, downloaded_non_precache_bytes_cellular->GetCount(kSize2));
373 EXPECT_EQ(1, downloaded_non_precache_bytes_cellular->GetCount(kSize5));
374
375 scoped_ptr<base::HistogramSamples> saved_bytes(
376 GetHistogramSamplesDelta("Precache.Saved"));
377 EXPECT_EQ(2, saved_bytes->TotalCount());
378 EXPECT_EQ(1, saved_bytes->GetCount(kSize1));
379 EXPECT_EQ(1, saved_bytes->GetCount(kSize3));
380
381 scoped_ptr<base::HistogramSamples> saved_bytes_cellular(
382 GetHistogramSamplesDelta("Precache.Saved.Cellular"));
383 EXPECT_EQ(1, saved_bytes_cellular->TotalCount());
384 EXPECT_EQ(1, saved_bytes_cellular->GetCount(kSize1));
385 }
386
387 } // namespace
388
389 } // namespace precache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698