| OLD | NEW |
| 1 #! /usr/bin/env python | 1 #! /usr/bin/env 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 import argparse | 6 import argparse |
| 7 import json | 7 import json |
| 8 import re | 8 import re |
| 9 import sys | 9 import sys |
| 10 | 10 |
| 11 from loading_trace import LoadingTrace | 11 from loading_trace import LoadingTrace |
| 12 import request_track | 12 import request_track |
| 13 | 13 |
| 14 | 14 |
| 15 def _ArgumentParser(): | 15 def _ArgumentParser(): |
| 16 """Builds a command line argument's parser. | 16 """Builds a command line argument's parser. |
| 17 """ | 17 """ |
| 18 parser = argparse.ArgumentParser() | 18 parser = argparse.ArgumentParser() |
| 19 subparsers = parser.add_subparsers(dest='subcommand', help='subcommand line') | 19 subparsers = parser.add_subparsers(dest='subcommand', help='subcommand line') |
| 20 | 20 |
| 21 # requests listing subcommand. | 21 # requests listing subcommand. |
| 22 requests_parser = subparsers.add_parser('requests', | 22 requests_parser = subparsers.add_parser('requests', |
| 23 help='Lists all request from the loading trace.') | 23 help='Lists all request from the loading trace.') |
| 24 requests_parser.add_argument('loading_trace', type=file, | 24 requests_parser.add_argument('loading_trace', type=str, |
| 25 help='Input loading trace to see the cache usage from.') | 25 help='Input loading trace to see the cache usage from.') |
| 26 requests_parser.add_argument('--output', | 26 requests_parser.add_argument('--output', |
| 27 type=argparse.FileType(), | 27 type=argparse.FileType('w'), |
| 28 default=sys.stdout, | 28 default=sys.stdout, |
| 29 help='Output destination path if different from stdout.') | 29 help='Output destination path if different from stdout.') |
| 30 requests_parser.add_argument('--output-format', type=str, default='{url}', | 30 requests_parser.add_argument('--output-format', type=str, default='{url}', |
| 31 help='Output line format (Default to "{url}")') | 31 help='Output line format (Default to "{url}")') |
| 32 requests_parser.add_argument('--where', | 32 requests_parser.add_argument('--where', |
| 33 dest='where_statement', type=str, | 33 dest='where_statement', type=str, |
| 34 nargs=2, metavar=('FORMAT', 'REGEX'), default=[], | 34 nargs=2, metavar=('FORMAT', 'REGEX'), default=[], |
| 35 help='Where statement to filter such as: --where "{protocol}" "https?"') | 35 help='Where statement to filter such as: --where "{protocol}" "https?"') |
| 36 return parser | 36 return parser |
| 37 | 37 |
| 38 | 38 |
| 39 def _RequestsSubcommand(args): | 39 def ListRequests(loading_trace_path, |
| 40 output_format='{url}', |
| 41 where_format='{url}', |
| 42 where_statement=None): |
| 40 """`loading_trace_analyzer.py requests` Command line tool entry point. | 43 """`loading_trace_analyzer.py requests` Command line tool entry point. |
| 41 | 44 |
| 45 Args: |
| 46 loading_trace_path: Path of the loading trace. |
| 47 output_format: Output format of the generated strings. |
| 48 where_format: String formated to be regex tested with <where_statement> |
| 49 where_statement: Regex for selecting request event. |
| 50 |
| 51 Yields: |
| 52 Formated string of the selected request event. |
| 53 |
| 42 Example: | 54 Example: |
| 43 Lists all request with timing: | 55 Lists all request with timing: |
| 44 ... requests --output-format "{timing} {url}" | 56 ... requests --output-format "{timing} {url}" |
| 45 | 57 |
| 46 Lists HTTP/HTTPS requests that have used the cache: | 58 Lists HTTP/HTTPS requests that have used the cache: |
| 47 ... requests --where "{protocol} {from_disk_cache}" "https?\S* True" | 59 ... requests --where "{protocol} {from_disk_cache}" "https?\S* True" |
| 48 """ | 60 """ |
| 49 where_format = None | 61 if where_statement: |
| 50 where_statement = None | 62 where_statement = re.compile(where_statement) |
| 51 if args.where_statement: | 63 loading_trace = LoadingTrace.FromJsonFile(loading_trace_path) |
| 52 where_format = args.where_statement[0] | |
| 53 try: | |
| 54 where_statement = re.compile(args.where_statement[1]) | |
| 55 except re.error as e: | |
| 56 sys.stderr.write("Invalid where statement REGEX: {}\n{}\n".format( | |
| 57 args.where_statement[1], str(e))) | |
| 58 return 1 | |
| 59 | |
| 60 loading_trace = LoadingTrace.FromJsonDict(json.load(args.loading_trace)) | |
| 61 for request_event in loading_trace.request_track.GetEvents(): | 64 for request_event in loading_trace.request_track.GetEvents(): |
| 62 request_event_json = request_event.ToJsonDict() | 65 request_event_json = request_event.ToJsonDict() |
| 63 | |
| 64 if where_statement != None: | 66 if where_statement != None: |
| 65 where_in = where_format.format(**request_event_json) | 67 where_in = where_format.format(**request_event_json) |
| 66 if not where_statement.match(where_in): | 68 if not where_statement.match(where_in): |
| 67 continue | 69 continue |
| 68 | 70 yield output_format.format(**request_event_json) |
| 69 args.output.write(args.output_format.format(**request_event_json) + '\n') | |
| 70 return 0 | |
| 71 | 71 |
| 72 | 72 |
| 73 def main(command_line_args): | 73 def main(command_line_args): |
| 74 """Command line tool entry point. | 74 """Command line tool entry point. |
| 75 """ | 75 """ |
| 76 args = _ArgumentParser().parse_args(command_line_args) | 76 args = _ArgumentParser().parse_args(command_line_args) |
| 77 if args.subcommand == 'requests': | 77 if args.subcommand == 'requests': |
| 78 return _RequestsSubcommand(args) | 78 try: |
| 79 where_format = None |
| 80 where_statement = None |
| 81 if args.where_statement: |
| 82 where_format = args.where_statement[0] |
| 83 where_statement = args.where_statement[1] |
| 84 for output_line in ListRequests(loading_trace_path=args.loading_trace, |
| 85 output_format=args.output_format, |
| 86 where_format=where_format, |
| 87 where_statement=where_statement): |
| 88 args.output.write(output_line + '\n') |
| 89 return 0 |
| 90 except re.error as e: |
| 91 sys.stderr.write("Invalid where statement REGEX: {}\n{}\n".format( |
| 92 where_statement[1], str(e))) |
| 93 return 1 |
| 79 assert False | 94 assert False |
| 80 | 95 |
| 81 | 96 |
| 82 if __name__ == '__main__': | 97 if __name__ == '__main__': |
| 83 sys.exit(main(sys.argv[1:])) | 98 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |