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

Side by Side Diff: tools/resource_prefetch_predictor/prefetch_predictor_tool.py

Issue 2168083002: tools: Add a tool to dump the resource_prefetch_predictor database. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 months 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
« no previous file with comments | « tools/resource_prefetch_predictor/OWNERS ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/python
2 # Copyright 2016 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Inspection of the prefetch predictor database.
7
8 On Android, the database can be extracted using:
9 adb pull \
10 '/data/user/0/$package_name/app_chrome/Default/Network Action Predictor'
11 predictor_db
12 """
13
14 import argparse
15 import operator
16 import sqlite3
17
18
19 class ResourceType(object):
20 STYLESHEET = 2
21 SCRIPT = 3
22
23
24 class Entry(object):
25 """Represents an entry in the predictor database."""
26 HEADER = (
27 'score,main_page_url,resource_type,number_of_hits,number_of_misses,'
28 'consecutive_misses,average_position,confidence,resource_url')
29
30 def __init__(
31 self, main_page_url, resource_url, resource_type, number_of_hits,
32 number_of_misses, consecutive_misses, average_position):
33 self.main_page_url = main_page_url
34 self.resource_url = resource_url
35 self.resource_type = resource_type
36 self.number_of_hits = int(number_of_hits)
37 self.number_of_misses = int(number_of_misses)
38 self.consecutive_misses = int(consecutive_misses)
39 self.average_position = int(average_position)
40 self.confidence = float(number_of_hits) / (
41 number_of_hits + number_of_misses)
42 self.score = self._Score()
43
44 def _Score(self):
45 """Mirrors ResourcePrefetchPredictorTables::ResourceRow::UpdateScore."""
46 multiplier = 1
47 if self.resource_type in (ResourceType.STYLESHEET, ResourceType.SCRIPT):
48 multiplier = 2
49 return multiplier * 100 - self.average_position
50
51 @classmethod
52 def FromRow(cls, row):
53 """Builds an entry from a database row."""
54 return Entry(*row)
55
56 def __str__(self):
57 return '%f,%s,%d,%d,%d,%d,%d,%f\t%s' % (
58 self.score, self.main_page_url, self.resource_type,
59 self.number_of_hits, self.number_of_misses, self.consecutive_misses,
60 self.average_position, self.confidence, self.resource_url)
61
62
63 def FilterAndSort(entries, domain):
64 """Filters and sorts the entries to be prefetched for a given domain.
65
66 Uses the default thresholds defined in resource_prefetch_common.cc.
67 """
68 result = filter(
69 lambda x: ((domain is None or x.main_page_url == domain)
70 and x.confidence > .7
71 and x.number_of_hits >= 2), entries)
72 return sorted(result, key=operator.attrgetter('score'), reverse=True)
73
74
75 def DatabaseStats(filename, domain):
76 connection = sqlite3.connect(filename)
77 c = connection.cursor()
78 query = ('SELECT main_page_url, resource_url, resource_type, number_of_hits, '
79 'number_of_misses, consecutive_misses, average_position '
80 'FROM resource_prefetch_predictor_host')
81 entries = [Entry.FromRow(row) for row in c.execute(query)]
82 prefetched = FilterAndSort(entries, domain)
83 print Entry.HEADER
84 for x in prefetched:
85 print x
86
87
88 def main():
89 parser = argparse.ArgumentParser()
90 parser.add_argument('-f', dest='database_filename', required=True,
91 help='Path to the database')
92 parser.add_argument('-d', dest='domain', default=None, help='Domain')
93 args = parser.parse_args()
94 DatabaseStats(args.database_filename, args.domain)
95
96
97 if __name__ == '__main__':
98 main()
OLDNEW
« no previous file with comments | « tools/resource_prefetch_predictor/OWNERS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698