Chromium Code Reviews| Index: tools/resource_prefetch_predictor/prefetch_predictor_tool.py |
| diff --git a/tools/resource_prefetch_predictor/prefetch_predictor_tool.py b/tools/resource_prefetch_predictor/prefetch_predictor_tool.py |
| index 0e9657a897f1623b61c235c7f60e59ff082728d3..b31e88d985f808e2554cd5c7d173f7a75ad64917 100755 |
| --- a/tools/resource_prefetch_predictor/prefetch_predictor_tool.py |
| +++ b/tools/resource_prefetch_predictor/prefetch_predictor_tool.py |
| @@ -12,74 +12,68 @@ adb pull \ |
| """ |
| import argparse |
| -import operator |
| import sqlite3 |
| -from resource_prefetch_predictor_pb2 import ResourceData |
| +from resource_prefetch_predictor_pb2 import (PrefetchData, ResourceData) |
| class Entry(object): |
| """Represents an entry in the predictor database.""" |
| def __init__( |
| - self, main_page_url, resource_url, proto_buffer): |
| - self.main_page_url = main_page_url |
| - self.resource_url = resource_url |
| - self.proto = ResourceData() |
| - self.proto.ParseFromString(proto_buffer) |
| - self.confidence = float(self.proto.number_of_hits / ( |
| - self.proto.number_of_hits + self.proto.number_of_misses)) |
| - self.score = self._Score() |
| - |
| - def _Score(self): |
| + self, primary_key, proto_buffer): |
| + self.primary_key = primary_key |
| + self.prefetch_data = PrefetchData() |
| + self.prefetch_data.ParseFromString(proto_buffer) |
| + |
| + @classmethod |
| + def _ComputeResourceScore(cls, resource): |
|
pasko
2016/10/07 13:55:25
Please mention the type of |resource| in the docst
Benoit L
2016/10/07 14:03:10
Done.
|
| """Mirrors ResourcePrefetchPredictorTables::ComputeResourceScore.""" |
| priority_multiplier = 1 |
| type_multiplier = 1 |
| - if self.proto.priority == ResourceData.REQUEST_PRIORITY_HIGHEST: |
| + if resource.priority == ResourceData.REQUEST_PRIORITY_HIGHEST: |
| priority_multiplier = 3 |
| - elif self.proto.priority == ResourceData.REQUEST_PRIORITY_MEDIUM: |
| + elif resource.priority == ResourceData.REQUEST_PRIORITY_MEDIUM: |
| priority_multiplier = 2 |
| - if self.proto.resource_type in (ResourceData.RESOURCE_TYPE_STYLESHEET, |
| - ResourceData.RESOURCE_TYPE_SCRIPT): |
| + if resource.resource_type in (ResourceData.RESOURCE_TYPE_STYLESHEET, |
| + ResourceData.RESOURCE_TYPE_SCRIPT): |
| type_multiplier = 3 |
| - elif self.proto.resource_type == ResourceData.RESOURCE_TYPE_FONT_RESOURCE: |
| + elif resource.resource_type == ResourceData.RESOURCE_TYPE_FONT_RESOURCE: |
| type_multiplier = 2 |
| - return (100 * (priority_multiplier * 100 + type_multiplier * 10) |
| - - self.proto.average_position) |
| + return (100 * (priority_multiplier * 100 + type_multiplier * 10) |
| + - resource.average_position) |
| @classmethod |
| def FromRow(cls, row): |
| """Builds an entry from a database row.""" |
| return Entry(*row) |
| - def __str__(self): |
| - return 'score: %s\nmain_page_url: %s\nconfidence: %f"\n%s' % ( |
| - self.score, self.main_page_url, self.confidence, self.proto) |
| - |
| - |
| -def FilterAndSort(entries, domain): |
| - """Filters and sorts the entries to be prefetched for a given domain. |
| + @classmethod |
| + def _PrettyPrintResource(cls, resource): |
| + print 'score: %d' % cls._ComputeResourceScore(resource) |
| + print resource |
|
pasko
2016/10/07 13:55:25
is the default python pretty-print looking good he
Benoit L
2016/10/07 14:03:10
Actually, protocol buffer are already pretty when
|
| - Uses the default thresholds defined in resource_prefetch_common.cc. |
| - """ |
| - result = filter( |
| - lambda x: ((domain is None or x.main_page_url == domain) |
| - and x.confidence > .7 |
| - and x.proto.number_of_hits >= 2), entries) |
| - return sorted(result, key=operator.attrgetter('score'), reverse=True) |
| + def PrettyPrintCandidates(self): |
| + """Prints the candidates for prefetch.""" |
| + print 'primary_key: %s' % self.prefetch_data.primary_key |
| + for resource in self.prefetch_data.resources: |
| + confidence = resource.number_of_hits / ( |
| + resource.number_of_hits + resource.number_of_misses) |
|
pasko
2016/10/07 13:55:25
perhaps need to explicitly ask for a floating poin
Benoit L
2016/10/07 14:03:10
/facepalm
Thanks!
Done.
|
| + if resource.number_of_hits < 2 or confidence < .7: |
| + continue |
| + self._PrettyPrintResource(resource) |
| def DatabaseStats(filename, domain): |
| connection = sqlite3.connect(filename) |
| c = connection.cursor() |
| - query = ('SELECT main_page_url, resource_url, proto ' |
| - 'FROM resource_prefetch_predictor_host') |
| + query = ('SELECT key, proto FROM resource_prefetch_predictor_host') |
| entries = [Entry.FromRow(row) for row in c.execute(query)] |
| - prefetched = FilterAndSort(entries, domain) |
| - for x in prefetched: |
| - print x |
| + for x in entries: |
| + if domain is None or x.primary_key == domain: |
| + x.PrettyPrintCandidates() |
| def main(): |