Chromium Code Reviews| 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 """Instructs Chrome to load series of web pages and reports results. | 6 """Instructs Chrome to load series of web pages and reports results. |
| 7 | 7 |
| 8 When running Chrome is sandwiched between preprocessed disk caches and | 8 When running Chrome is sandwiched between preprocessed disk caches and |
| 9 WepPageReplay serving all connections. | 9 WepPageReplay serving all connections. |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 from devil.android import device_utils | 28 from devil.android import device_utils |
| 29 | 29 |
| 30 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) | 30 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) |
| 31 from pylib import constants | 31 from pylib import constants |
| 32 import devil_chromium | 32 import devil_chromium |
| 33 | 33 |
| 34 import chrome_cache | 34 import chrome_cache |
| 35 import chrome_setup | 35 import chrome_setup |
| 36 import device_setup | 36 import device_setup |
| 37 import devtools_monitor | 37 import devtools_monitor |
| 38 import frame_load_lens | |
| 39 import loading_trace | |
| 38 import options | 40 import options |
| 39 import page_track | 41 import page_track |
| 40 import pull_sandwich_metrics | 42 import pull_sandwich_metrics |
| 43 import request_dependencies_lens | |
| 41 import trace_recorder | 44 import trace_recorder |
| 42 import tracing | 45 import tracing |
| 43 import wpr_backend | 46 import wpr_backend |
| 44 | 47 |
| 45 | 48 |
| 46 # Use options layer to access constants. | 49 # Use options layer to access constants. |
| 47 OPTIONS = options.OPTIONS | 50 OPTIONS = options.OPTIONS |
| 48 | 51 |
| 49 _JOB_SEARCH_PATH = 'sandwich_jobs' | 52 _JOB_SEARCH_PATH = 'sandwich_jobs' |
| 50 | 53 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 def _RunNavigation(self, url, clear_cache, trace_id=None): | 199 def _RunNavigation(self, url, clear_cache, trace_id=None): |
| 197 with device_setup.DeviceConnection( | 200 with device_setup.DeviceConnection( |
| 198 device=self._device, | 201 device=self._device, |
| 199 additional_flags=self._chrome_additional_flags) as connection: | 202 additional_flags=self._chrome_additional_flags) as connection: |
| 200 additional_metadata = {} | 203 additional_metadata = {} |
| 201 if self._GetEmulatorNetworkCondition('browser'): | 204 if self._GetEmulatorNetworkCondition('browser'): |
| 202 additional_metadata = chrome_setup.SetUpEmulationAndReturnMetadata( | 205 additional_metadata = chrome_setup.SetUpEmulationAndReturnMetadata( |
| 203 connection=connection, | 206 connection=connection, |
| 204 emulated_device_name=None, | 207 emulated_device_name=None, |
| 205 emulated_network_name=self._GetEmulatorNetworkCondition('browser')) | 208 emulated_network_name=self._GetEmulatorNetworkCondition('browser')) |
| 206 loading_trace = trace_recorder.MonitorUrl( | 209 trace = trace_recorder.MonitorUrl( |
| 207 connection, url, | 210 connection, url, |
| 208 clear_cache=clear_cache, | 211 clear_cache=clear_cache, |
| 209 categories=pull_sandwich_metrics.CATEGORIES, | 212 categories=pull_sandwich_metrics.CATEGORIES, |
| 210 timeout=_DEVTOOLS_TIMEOUT) | 213 timeout=_DEVTOOLS_TIMEOUT) |
| 211 loading_trace.metadata.update(additional_metadata) | 214 trace.metadata.update(additional_metadata) |
| 212 if trace_id != None and self.trace_output_directory: | 215 if trace_id != None and self.trace_output_directory: |
| 213 loading_trace_path = os.path.join( | 216 trace_path = os.path.join( |
| 214 self.trace_output_directory, str(trace_id), 'trace.json') | 217 self.trace_output_directory, str(trace_id), 'trace.json') |
| 215 os.makedirs(os.path.dirname(loading_trace_path)) | 218 os.makedirs(os.path.dirname(trace_path)) |
| 216 loading_trace.ToJsonFile(loading_trace_path) | 219 trace.ToJsonFile(trace_path) |
| 217 | 220 |
| 218 def _RunUrl(self, url, trace_id=0): | 221 def _RunUrl(self, url, trace_id=0): |
| 219 clear_cache = False | 222 clear_cache = False |
| 220 if self.cache_operation == 'clear': | 223 if self.cache_operation == 'clear': |
| 221 clear_cache = True | 224 clear_cache = True |
| 222 elif self.cache_operation == 'push': | 225 elif self.cache_operation == 'push': |
| 223 self._device.KillAll(OPTIONS.chrome_package_name, quiet=True) | 226 self._device.KillAll(OPTIONS.chrome_package_name, quiet=True) |
| 224 chrome_cache.PushBrowserCache(self._device, | 227 chrome_cache.PushBrowserCache(self._device, |
| 225 self._local_cache_directory_path) | 228 self._local_cache_directory_path) |
| 226 elif self.cache_operation == 'reload': | 229 elif self.cache_operation == 'reload': |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 create_cache_parser = subparsers.add_parser('pull-metrics', | 363 create_cache_parser = subparsers.add_parser('pull-metrics', |
| 361 help='Pulls metrics in CSV from a run\'s loading trace.') | 364 help='Pulls metrics in CSV from a run\'s loading trace.') |
| 362 create_cache_parser.add_argument('--traces-directory', required=True, | 365 create_cache_parser.add_argument('--traces-directory', required=True, |
| 363 dest='trace_output_directory', type=str, | 366 dest='trace_output_directory', type=str, |
| 364 help='Path of loading traces directory.') | 367 help='Path of loading traces directory.') |
| 365 create_cache_parser.add_argument('--out-metrics', default=None, type=str, | 368 create_cache_parser.add_argument('--out-metrics', default=None, type=str, |
| 366 dest='metrics_csv_path', | 369 dest='metrics_csv_path', |
| 367 help='Path where to save the metrics\'s '+ | 370 help='Path where to save the metrics\'s '+ |
| 368 'CSV.') | 371 'CSV.') |
| 369 | 372 |
| 373 # Filter cache subcommand. | |
| 374 filter_cache_parser = subparsers.add_parser('filter-cache', | |
| 375 help='Cache filtering that keeps only resources discoverable by the HTML'+ | |
| 376 ' document parser.') | |
| 377 filter_cache_parser.add_argument('--cache-archive', type=str, required=True, | |
| 378 dest='cache_archive_path', | |
| 379 help='Path of the cache archive to filter.') | |
| 380 filter_cache_parser.add_argument('--output', type=str, required=True, | |
| 381 dest='output_cache_archive_path', | |
| 382 help='Path of filtered cache archive.') | |
| 383 filter_cache_parser.add_argument('loading_trace_paths', type=str, nargs='+', | |
|
pasko
2016/02/26 17:10:06
underscores -> dashes, please
gabadie
2016/03/01 10:40:48
Nope. Because non optional argument. I would have
pasko
2016/03/01 17:55:53
ah, I see, thanks, maybe move the definition of th
| |
| 384 metavar='LOADING_TRACE', | |
| 385 help='A loading trace path to generate the' + | |
|
pasko
2016/02/26 17:10:06
please document more details on how they are gener
gabadie
2016/03/01 10:40:48
Done.
pasko
2016/03/01 17:55:53
No, I was not mixing them this time. We can name t
| |
| 386 ' urls white-list from.') | |
| 387 | |
| 370 return parser | 388 return parser |
| 371 | 389 |
| 372 | 390 |
| 373 def _RecordWprMain(args): | 391 def _RecordWprMain(args): |
| 374 sandwich_runner = SandwichRunner(args.job) | 392 sandwich_runner = SandwichRunner(args.job) |
| 375 sandwich_runner.PullConfigFromArgs(args) | 393 sandwich_runner.PullConfigFromArgs(args) |
| 376 sandwich_runner.wpr_record = True | 394 sandwich_runner.wpr_record = True |
| 377 sandwich_runner.PrintConfig() | 395 sandwich_runner.PrintConfig() |
| 378 if not os.path.isdir(os.path.dirname(args.wpr_archive_path)): | 396 if not os.path.isdir(os.path.dirname(args.wpr_archive_path)): |
| 379 os.makedirs(os.path.dirname(args.wpr_archive_path)) | 397 os.makedirs(os.path.dirname(args.wpr_archive_path)) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 trace_metrics_list.sort(key=lambda e: e['id']) | 453 trace_metrics_list.sort(key=lambda e: e['id']) |
| 436 with open(args.metrics_csv_path, 'w') as csv_file: | 454 with open(args.metrics_csv_path, 'w') as csv_file: |
| 437 writer = csv.DictWriter(csv_file, | 455 writer = csv.DictWriter(csv_file, |
| 438 fieldnames=pull_sandwich_metrics.CSV_FIELD_NAMES) | 456 fieldnames=pull_sandwich_metrics.CSV_FIELD_NAMES) |
| 439 writer.writeheader() | 457 writer.writeheader() |
| 440 for trace_metrics in trace_metrics_list: | 458 for trace_metrics in trace_metrics_list: |
| 441 writer.writerow(trace_metrics) | 459 writer.writerow(trace_metrics) |
| 442 return 0 | 460 return 0 |
| 443 | 461 |
| 444 | 462 |
| 463 def _FilterCacheMain(args): | |
| 464 whitelisted_urls = set() | |
| 465 for loading_trace_path in args.loading_trace_paths: | |
| 466 logging.info('loading %s' % loading_trace_path) | |
| 467 trace = loading_trace.LoadingTrace.FromJsonFile(loading_trace_path) | |
| 468 requests_lens = request_dependencies_lens.RequestDependencyLens(trace) | |
| 469 deps = requests_lens.GetRequestDependencies() | |
| 470 | |
| 471 main_resource_request = deps[0][0] | |
| 472 logging.info('white-listing %s' % main_resource_request.url) | |
| 473 whitelisted_urls.add(main_resource_request.url) | |
| 474 for (first, second, reason) in deps: | |
| 475 # Ignore data protocols. | |
| 476 if not second.protocol.startswith('http'): | |
| 477 continue | |
| 478 if (first.request_id == main_resource_request.request_id and | |
| 479 reason == 'parser' and second.url not in whitelisted_urls): | |
| 480 logging.info('white-listing %s' % second.url) | |
| 481 whitelisted_urls.add(second.url) | |
| 482 | |
| 483 if not os.path.isdir(os.path.dirname(args.output_cache_archive_path)): | |
| 484 os.makedirs(os.path.dirname(args.output_cache_archive_path)) | |
| 485 chrome_cache.ApplyUrlWhitelistToCacheArchive(args.cache_archive_path, | |
| 486 whitelisted_urls, | |
| 487 args.output_cache_archive_path) | |
| 488 return 0 | |
| 489 | |
| 490 | |
| 445 def main(command_line_args): | 491 def main(command_line_args): |
| 446 logging.basicConfig(level=logging.INFO) | 492 logging.basicConfig(level=logging.INFO) |
| 447 devil_chromium.Initialize() | 493 devil_chromium.Initialize() |
| 448 | 494 |
| 449 # Don't give the argument yet. All we are interested in for now is accessing | 495 # Don't give the argument yet. All we are interested in for now is accessing |
| 450 # the default values of OPTIONS. | 496 # the default values of OPTIONS. |
| 451 OPTIONS.ParseArgs([]) | 497 OPTIONS.ParseArgs([]) |
| 452 | 498 |
| 453 args = _ArgumentParser().parse_args(command_line_args) | 499 args = _ArgumentParser().parse_args(command_line_args) |
| 454 | 500 |
| 455 if args.subcommand == 'record-wpr': | 501 if args.subcommand == 'record-wpr': |
| 456 return _RecordWprMain(args) | 502 return _RecordWprMain(args) |
| 457 if args.subcommand == 'patch-wpr': | 503 if args.subcommand == 'patch-wpr': |
| 458 return _PatchWprMain(args) | 504 return _PatchWprMain(args) |
| 459 if args.subcommand == 'create-cache': | 505 if args.subcommand == 'create-cache': |
| 460 return _CreateCacheMain(args) | 506 return _CreateCacheMain(args) |
| 461 if args.subcommand == 'run': | 507 if args.subcommand == 'run': |
| 462 return _RunJobMain(args) | 508 return _RunJobMain(args) |
| 463 if args.subcommand == 'pull-metrics': | 509 if args.subcommand == 'pull-metrics': |
| 464 return _PullMetricsMain(args) | 510 return _PullMetricsMain(args) |
| 511 if args.subcommand == 'filter-cache': | |
| 512 return _FilterCacheMain(args) | |
| 465 assert False | 513 assert False |
| 466 | 514 |
| 467 | 515 |
| 468 if __name__ == '__main__': | 516 if __name__ == '__main__': |
| 469 sys.exit(main(sys.argv[1:])) | 517 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |