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