Chromium Code Reviews| Index: tools/android/loading/sandwich.py |
| diff --git a/tools/android/loading/sandwich.py b/tools/android/loading/sandwich.py |
| index c6d101f5fcb9d343951b7c4034517078b2d20f04..a9b3d1ad11f03f829ff50ab06f9c63b33983f368 100755 |
| --- a/tools/android/loading/sandwich.py |
| +++ b/tools/android/loading/sandwich.py |
| @@ -12,6 +12,7 @@ TODO(pasko): implement cache preparation and WPR. |
| """ |
| import argparse |
| +import csv |
| import json |
| import logging |
| import os |
| @@ -283,13 +284,17 @@ class SandwichRunner(object): |
| def _ArgumentParser(): |
| """Build a command line argument's parser. |
| """ |
| + # Command parser when dealing with jobs. |
| + common_job_parser = argparse.ArgumentParser(add_help=False) |
| + common_job_parser.add_argument('--job', required=True, |
| + help='JSON file with job description.') |
| + |
| + # Main parser |
| parser = argparse.ArgumentParser() |
| - parser.add_argument('--job', required=True, |
| - help='JSON file with job description.') |
| subparsers = parser.add_subparsers(dest='subcommand', help='subcommand line') |
| # Record WPR subcommand. |
| - record_wpr = subparsers.add_parser('record-wpr', |
| + record_wpr = subparsers.add_parser('record-wpr', parents=[common_job_parser], |
| help='Record WPR from sandwich job.') |
| record_wpr.add_argument('--wpr-archive', required=True, type=str, |
| dest='wpr_archive_path', |
| @@ -300,10 +305,11 @@ def _ArgumentParser(): |
| help='Patch WPR response headers.') |
| patch_wpr.add_argument('--wpr-archive', required=True, type=str, |
| dest='wpr_archive_path', |
| - help='Web page replay archive to generate.') |
| + help='Web page replay archive to patch.') |
| # Create cache subcommand. |
| create_cache_parser = subparsers.add_parser('create-cache', |
| + parents=[common_job_parser], |
| help='Create cache from sandwich job.') |
| create_cache_parser.add_argument('--cache-archive', required=True, type=str, |
| dest='cache_archive_path', |
| @@ -314,7 +320,8 @@ def _ArgumentParser(): |
| 'the cache from.') |
| # Run subcommand. |
| - run_parser = subparsers.add_parser('run', help='Run sandwich benchmark.') |
| + run_parser = subparsers.add_parser('run', parents=[common_job_parser], |
| + help='Run sandwich benchmark.') |
| run_parser.add_argument('--output', required=True, type=str, |
| dest='trace_output_directory', |
| help='Path of output directory to create.') |
| @@ -348,6 +355,18 @@ def _ArgumentParser(): |
| dest='wpr_archive_path', |
| help='Web page replay archive to load job\'s urls ' + |
| 'from.') |
| + |
| + # Pull metrics subcommand. |
| + create_cache_parser = subparsers.add_parser('pull-metrics', |
| + help='Pulls metrics in CSV from a run\'s loading trace.') |
| + create_cache_parser.add_argument('--traces-directory', required=True, |
| + dest='trace_output_directory', type=str, |
| + help='Path of loading traces directory.') |
| + create_cache_parser.add_argument('--out-metrics', default=None, type=str, |
| + dest='metrics_csv_path', |
| + help='Path where to save the metrics\'s '+ |
| + 'CSV.') |
| + |
| return parser |
| @@ -356,6 +375,8 @@ def _RecordWprMain(args): |
| sandwich_runner.PullConfigFromArgs(args) |
| sandwich_runner.wpr_record = True |
| sandwich_runner.PrintConfig() |
| + if not os.path.isdir(os.path.dirname(args.wpr_archive_path)): |
| + os.makedirs(os.path.dirname(args.wpr_archive_path)) |
| sandwich_runner.Run() |
| return 0 |
| @@ -394,6 +415,8 @@ def _CreateCacheMain(args): |
| sandwich_runner.PullConfigFromArgs(args) |
| sandwich_runner.cache_operation = 'save' |
| sandwich_runner.PrintConfig() |
| + if not os.path.isdir(os.path.dirname(args.cache_archive_path)): |
| + os.makedirs(os.path.dirname(args.cache_archive_path)) |
| sandwich_runner.Run() |
| return 0 |
| @@ -406,6 +429,19 @@ def _RunJobMain(args): |
| return 0 |
| +def _PullMetricsMain(args): |
| + trace_metrics_list = pull_sandwich_metrics.PullMetricsFromOutputDirectory( |
| + args.trace_output_directory) |
| + trace_metrics_list.sort(key=lambda e: e['id']) |
| + with open(args.metrics_csv_path, 'w') as csv_file: |
|
Benoit L
2016/02/25 16:02:13
Any reason to choose CSV rather than JSON?
gabadie
2016/02/25 16:55:06
The main motivation of using CSV here is to be abl
|
| + writer = csv.DictWriter(csv_file, |
| + fieldnames=pull_sandwich_metrics.CSV_FIELD_NAMES) |
| + writer.writeheader() |
| + for trace_metrics in trace_metrics_list: |
| + writer.writerow(trace_metrics) |
| + return 0 |
| + |
| + |
| def main(command_line_args): |
| logging.basicConfig(level=logging.INFO) |
| devil_chromium.Initialize() |
| @@ -424,6 +460,8 @@ def main(command_line_args): |
| return _CreateCacheMain(args) |
| if args.subcommand == 'run': |
| return _RunJobMain(args) |
| + if args.subcommand == 'pull-metrics': |
| + return _PullMetricsMain(args) |
| assert False |