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..228e3ef2d6a450af8be322869fc1356e2fb6cb66 100755 |
| --- a/tools/resource_prefetch_predictor/prefetch_predictor_tool.py |
| +++ b/tools/resource_prefetch_predictor/prefetch_predictor_tool.py |
| @@ -12,74 +12,80 @@ 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): |
| - """Mirrors ResourcePrefetchPredictorTables::ComputeResourceScore.""" |
| + 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): |
| + """Mirrors ResourcePrefetchPredictorTables::ComputeResourceScore. |
| + |
| + Args: |
| + param: ResourceData. |
|
pasko
2016/10/07 15:44:56
s/param/resource/
|
| + |
| + Return: |
| + The resource score (int). |
| + """ |
| 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. |
| - |
| - 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) |
| + @classmethod |
| + def _PrettyPrintResource(cls, resource): |
| + """Pretty-prints a resource to stdout. |
| + |
| + Args: |
| + param: ResourceData. |
|
pasko
2016/10/07 15:44:56
ditto
|
| + """ |
| + print 'score: %d' % cls._ComputeResourceScore(resource) |
| + print resource |
| + |
| + 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 = float(resource.number_of_hits) / ( |
| + resource.number_of_hits + resource.number_of_misses) |
| + 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(): |