OLD | NEW |
(Empty) | |
| 1 #! /usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import argparse |
| 7 import json |
| 8 import re |
| 9 import sys |
| 10 |
| 11 from loading_trace import LoadingTrace |
| 12 import request_track |
| 13 |
| 14 |
| 15 def _ArgumentParser(): |
| 16 """Builds a command line argument's parser. |
| 17 """ |
| 18 parser = argparse.ArgumentParser() |
| 19 subparsers = parser.add_subparsers(dest='subcommand', help='subcommand line') |
| 20 |
| 21 # requests listing subcommand. |
| 22 requests_parser = subparsers.add_parser('requests', |
| 23 help='Lists all request from the loading trace.') |
| 24 requests_parser.add_argument('loading_trace', type=file, |
| 25 help='Input loading trace to see the cache usage from.') |
| 26 requests_parser.add_argument('--output', |
| 27 type=argparse.FileType(), |
| 28 default=sys.stdout, |
| 29 help='Output destination path if different from stdout.') |
| 30 requests_parser.add_argument('--output-format', type=str, default='{url}', |
| 31 help='Output line format (Default to "{url}")') |
| 32 requests_parser.add_argument('--where', |
| 33 dest='where_statement', type=str, |
| 34 nargs=2, metavar=('FORMAT', 'REGEX'), default=[], |
| 35 help='Where statement to filter such as: --where "{protocol}" "https?"') |
| 36 return parser |
| 37 |
| 38 |
| 39 def _RequestsSubcommand(args): |
| 40 """`loading_trace_analyzer.py requests` Command line tool entry point. |
| 41 |
| 42 Example: |
| 43 Lists all request with timing: |
| 44 ... requests --output-format "{timing} {url}" |
| 45 |
| 46 Lists HTTP/HTTPS requests that have used the cache: |
| 47 ... requests --where "{protocol} {from_disk_cache}" "https?\S* True" |
| 48 """ |
| 49 where_format = None |
| 50 where_statement = None |
| 51 if args.where_statement: |
| 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(): |
| 62 request_event_json = request_event.ToJsonDict() |
| 63 |
| 64 if where_statement != None: |
| 65 where_in = where_format.format(**request_event_json) |
| 66 if not where_statement.match(where_in): |
| 67 continue |
| 68 |
| 69 args.output.write(args.output_format.format(**request_event_json) + '\n') |
| 70 return 0 |
| 71 |
| 72 |
| 73 def main(command_line_args): |
| 74 """Command line tool entry point. |
| 75 """ |
| 76 args = _ArgumentParser().parse_args(command_line_args) |
| 77 if args.subcommand == 'requests': |
| 78 return _RequestsSubcommand(args) |
| 79 assert False |
| 80 |
| 81 |
| 82 if __name__ == '__main__': |
| 83 sys.exit(main(sys.argv[1:])) |
OLD | NEW |