Chromium Code Reviews| 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 from resource_prefetch_predictor_pb2 import ResourceData |
| 19 class ResourceType(object): | |
| 20 """Partially mirrors content::ResourceType.""" | |
| 21 STYLESHEET = 2 | |
| 22 SCRIPT = 3 | |
| 23 FONT_RESOURCE = 5 | |
| 24 | 19 |
| 25 | 20 |
| 26 class Entry(object): | 21 class Entry(object): |
| 27 """Represents an entry in the predictor database.""" | 22 """Represents an entry in the predictor database.""" |
| 28 HEADER = ( | |
| 29 'score,main_page_url,resource_type,number_of_hits,number_of_misses,' | |
| 30 'consecutive_misses,average_position,confidence,has_validators,' | |
| 31 'always_revalidate,resource_url') | |
| 32 | |
| 33 def __init__( | 23 def __init__( |
| 34 self, main_page_url, resource_url, resource_type, number_of_hits, | 24 self, main_page_url, resource_url, proto_buffer): |
| 35 number_of_misses, consecutive_misses, average_position, priority, | |
| 36 has_validators, always_revalidate): | |
| 37 self.main_page_url = main_page_url | 25 self.main_page_url = main_page_url |
| 38 self.resource_url = resource_url | 26 self.resource_url = resource_url |
| 39 self.resource_type = resource_type | 27 self.proto = ResourceData() |
| 40 self.number_of_hits = int(number_of_hits) | 28 self.proto.ParseFromString(proto_buffer) |
| 41 self.number_of_misses = int(number_of_misses) | 29 self.confidence = float(self.proto.number_of_hits / ( |
| 42 self.consecutive_misses = int(consecutive_misses) | 30 self.proto.number_of_hits + self.proto.number_of_misses)) |
| 43 self.average_position = int(average_position) | |
| 44 self.priority = int(priority) | |
| 45 self.has_validators = bool(int(has_validators)) | |
| 46 self.always_revalidate = bool(int(always_revalidate)) | |
| 47 self.confidence = float(number_of_hits) / ( | |
| 48 number_of_hits + number_of_misses) | |
| 49 self.score = self._Score() | 31 self.score = self._Score() |
| 50 | 32 |
| 51 def _Score(self): | 33 def _Score(self): |
| 52 """Mirrors ResourcePrefetchPredictorTables::ResourceRow::UpdateScore.""" | 34 """Mirrors ResourcePrefetchPredictorTables::ResourceRow::UpdateScore.""" |
| 53 multiplier = 1 | 35 multiplier = 1 |
| 54 if self.resource_type in (ResourceType.STYLESHEET, ResourceType.SCRIPT, | 36 if self.proto.resource_type in (ResourceData.RESOURCE_TYPE_STYLESHEET, |
| 55 ResourceType.FONT_RESOURCE): | 37 ResourceData.RESOURCE_TYPE_SCRIPT, |
| 38 ResourceData.RESOURCE_TYPE_FONT_RESOURCE): | |
| 56 multiplier = 2 | 39 multiplier = 2 |
| 57 return multiplier * 100 - self.average_position | 40 return multiplier * 100 - self.proto.average_position |
| 58 | 41 |
| 59 @classmethod | 42 @classmethod |
| 60 def FromRow(cls, row): | 43 def FromRow(cls, row): |
| 61 """Builds an entry from a database row.""" | 44 """Builds an entry from a database row.""" |
| 62 return Entry(*row) | 45 return Entry(*row) |
| 63 | 46 |
| 64 def __str__(self): | 47 def __str__(self): |
| 65 return '%f,%s,%d,%d,%d,%d,%d,%f,%d,%d,%d\t%s' % ( | 48 return 'score: %s\nmain_page_url: %s\nconfidence: %f"\n%s' % ( |
| 66 self.score, self.main_page_url, self.resource_type, | 49 self.score, self.main_page_url, self.confidence, self.proto) |
| 67 self.number_of_hits, self.number_of_misses, self.consecutive_misses, | |
| 68 self.average_position, self.confidence, self.priority, | |
| 69 self.has_validators, self.always_revalidate, self.resource_url) | |
| 70 | 50 |
| 71 | 51 |
| 72 def FilterAndSort(entries, domain): | 52 def FilterAndSort(entries, domain): |
| 73 """Filters and sorts the entries to be prefetched for a given domain. | 53 """Filters and sorts the entries to be prefetched for a given domain. |
| 74 | 54 |
| 75 Uses the default thresholds defined in resource_prefetch_common.cc. | 55 Uses the default thresholds defined in resource_prefetch_common.cc. |
| 76 """ | 56 """ |
| 77 result = filter( | 57 result = filter( |
| 78 lambda x: ((domain is None or x.main_page_url == domain) | 58 lambda x: ((domain is None or x.main_page_url == domain) |
| 79 and x.confidence > .7 | 59 and x.confidence > .7 |
| 80 and x.number_of_hits >= 2), entries) | 60 and x.proto.number_of_hits >= 2), entries) |
| 81 return sorted(result, key=operator.attrgetter('score'), reverse=True) | 61 return sorted(result, key=operator.attrgetter('score'), reverse=True) |
| 82 | 62 |
| 83 | 63 |
| 84 def DatabaseStats(filename, domain): | 64 def DatabaseStats(filename, domain): |
| 85 connection = sqlite3.connect(filename) | 65 connection = sqlite3.connect(filename) |
| 86 c = connection.cursor() | 66 c = connection.cursor() |
| 87 query = ('SELECT main_page_url, resource_url, resource_type, number_of_hits, ' | 67 query = ('SELECT main_page_url, resource_url, proto ' |
| 88 'number_of_misses, consecutive_misses, average_position, priority, ' | |
| 89 'has_validators, always_revalidate ' | |
| 90 'FROM resource_prefetch_predictor_host') | 68 'FROM resource_prefetch_predictor_host') |
| 91 entries = [Entry.FromRow(row) for row in c.execute(query)] | 69 entries = [Entry.FromRow(row) for row in c.execute(query)] |
| 92 prefetched = FilterAndSort(entries, domain) | 70 prefetched = FilterAndSort(entries, domain) |
| 93 print Entry.HEADER | |
|
pasko
2016/08/22 15:51:28
it was in CSV format, so I guess there was value o
Benoit L
2016/08/23 09:46:05
The previous format was just there because it was
| |
| 94 for x in prefetched: | 71 for x in prefetched: |
| 95 print x | 72 print x |
| 96 | 73 |
| 97 | 74 |
| 98 def main(): | 75 def main(): |
| 99 parser = argparse.ArgumentParser() | 76 parser = argparse.ArgumentParser() |
| 100 parser.add_argument('-f', dest='database_filename', required=True, | 77 parser.add_argument('-f', dest='database_filename', required=True, |
| 101 help='Path to the database') | 78 help='Path to the database') |
| 102 parser.add_argument('-d', dest='domain', default=None, help='Domain') | 79 parser.add_argument('-d', dest='domain', default=None, help='Domain') |
| 103 args = parser.parse_args() | 80 args = parser.parse_args() |
| 104 DatabaseStats(args.database_filename, args.domain) | 81 DatabaseStats(args.database_filename, args.domain) |
| 105 | 82 |
| 106 | 83 |
| 107 if __name__ == '__main__': | 84 if __name__ == '__main__': |
| 108 main() | 85 main() |
| OLD | NEW |