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 |
| 11 TODO(pasko): implement cache preparation and WPR. | 11 TODO(pasko): implement cache preparation and WPR. |
| 12 """ | 12 """ |
| 13 | 13 |
| 14 import argparse | 14 import argparse |
| 15 import csv | |
| 15 import json | 16 import json |
| 16 import logging | 17 import logging |
| 17 import os | 18 import os |
| 18 import shutil | 19 import shutil |
| 19 import sys | 20 import sys |
| 20 import tempfile | 21 import tempfile |
| 21 import time | 22 import time |
| 22 | 23 |
| 23 _SRC_DIR = os.path.abspath(os.path.join( | 24 _SRC_DIR = os.path.abspath(os.path.join( |
| 24 os.path.dirname(__file__), '..', '..', '..')) | 25 os.path.dirname(__file__), '..', '..', '..')) |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 self._local_cache_directory_path = None | 277 self._local_cache_directory_path = None |
| 277 if self.cache_operation == 'save': | 278 if self.cache_operation == 'save': |
| 278 self._PullCacheFromDevice() | 279 self._PullCacheFromDevice() |
| 279 if self.trace_output_directory: | 280 if self.trace_output_directory: |
| 280 self._SaveRunInfos(ran_urls) | 281 self._SaveRunInfos(ran_urls) |
| 281 | 282 |
| 282 | 283 |
| 283 def _ArgumentParser(): | 284 def _ArgumentParser(): |
| 284 """Build a command line argument's parser. | 285 """Build a command line argument's parser. |
| 285 """ | 286 """ |
| 287 # Command parser when dealing with jobs. | |
| 288 common_job_parser = argparse.ArgumentParser(add_help=False) | |
| 289 common_job_parser.add_argument('--job', required=True, | |
| 290 help='JSON file with job description.') | |
| 291 | |
| 292 # Main parser | |
| 286 parser = argparse.ArgumentParser() | 293 parser = argparse.ArgumentParser() |
| 287 parser.add_argument('--job', required=True, | |
| 288 help='JSON file with job description.') | |
| 289 subparsers = parser.add_subparsers(dest='subcommand', help='subcommand line') | 294 subparsers = parser.add_subparsers(dest='subcommand', help='subcommand line') |
| 290 | 295 |
| 291 # Record WPR subcommand. | 296 # Record WPR subcommand. |
| 292 record_wpr = subparsers.add_parser('record-wpr', | 297 record_wpr = subparsers.add_parser('record-wpr', parents=[common_job_parser], |
| 293 help='Record WPR from sandwich job.') | 298 help='Record WPR from sandwich job.') |
| 294 record_wpr.add_argument('--wpr-archive', required=True, type=str, | 299 record_wpr.add_argument('--wpr-archive', required=True, type=str, |
| 295 dest='wpr_archive_path', | 300 dest='wpr_archive_path', |
| 296 help='Web page replay archive to generate.') | 301 help='Web page replay archive to generate.') |
| 297 | 302 |
| 298 # Patch WPR subcommand. | 303 # Patch WPR subcommand. |
| 299 patch_wpr = subparsers.add_parser('patch-wpr', | 304 patch_wpr = subparsers.add_parser('patch-wpr', |
| 300 help='Patch WPR response headers.') | 305 help='Patch WPR response headers.') |
| 301 patch_wpr.add_argument('--wpr-archive', required=True, type=str, | 306 patch_wpr.add_argument('--wpr-archive', required=True, type=str, |
| 302 dest='wpr_archive_path', | 307 dest='wpr_archive_path', |
| 303 help='Web page replay archive to generate.') | 308 help='Web page replay archive to patch.') |
| 304 | 309 |
| 305 # Create cache subcommand. | 310 # Create cache subcommand. |
| 306 create_cache_parser = subparsers.add_parser('create-cache', | 311 create_cache_parser = subparsers.add_parser('create-cache', |
| 312 parents=[common_job_parser], | |
| 307 help='Create cache from sandwich job.') | 313 help='Create cache from sandwich job.') |
| 308 create_cache_parser.add_argument('--cache-archive', required=True, type=str, | 314 create_cache_parser.add_argument('--cache-archive', required=True, type=str, |
| 309 dest='cache_archive_path', | 315 dest='cache_archive_path', |
| 310 help='Cache archive destination path.') | 316 help='Cache archive destination path.') |
| 311 create_cache_parser.add_argument('--wpr-archive', default=None, type=str, | 317 create_cache_parser.add_argument('--wpr-archive', default=None, type=str, |
| 312 dest='wpr_archive_path', | 318 dest='wpr_archive_path', |
| 313 help='Web page replay archive to create ' + | 319 help='Web page replay archive to create ' + |
| 314 'the cache from.') | 320 'the cache from.') |
| 315 | 321 |
| 316 # Run subcommand. | 322 # Run subcommand. |
| 317 run_parser = subparsers.add_parser('run', help='Run sandwich benchmark.') | 323 run_parser = subparsers.add_parser('run', parents=[common_job_parser], |
| 324 help='Run sandwich benchmark.') | |
| 318 run_parser.add_argument('--output', required=True, type=str, | 325 run_parser.add_argument('--output', required=True, type=str, |
| 319 dest='trace_output_directory', | 326 dest='trace_output_directory', |
| 320 help='Path of output directory to create.') | 327 help='Path of output directory to create.') |
| 321 run_parser.add_argument('--cache-archive', type=str, | 328 run_parser.add_argument('--cache-archive', type=str, |
| 322 dest='cache_archive_path', | 329 dest='cache_archive_path', |
| 323 help='Cache archive destination path.') | 330 help='Cache archive destination path.') |
| 324 run_parser.add_argument('--cache-op', | 331 run_parser.add_argument('--cache-op', |
| 325 choices=['clear', 'push', 'reload'], | 332 choices=['clear', 'push', 'reload'], |
| 326 dest='cache_operation', | 333 dest='cache_operation', |
| 327 default='clear', | 334 default='clear', |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 341 choices=['browser', 'wpr'], | 348 choices=['browser', 'wpr'], |
| 342 help='Set which component is emulating the network condition.' + | 349 help='Set which component is emulating the network condition.' + |
| 343 ' (Default to browser). Wpr network emulator requires --wpr-archive' + | 350 ' (Default to browser). Wpr network emulator requires --wpr-archive' + |
| 344 ' to be set.') | 351 ' to be set.') |
| 345 run_parser.add_argument('--job-repeat', default=1, type=int, | 352 run_parser.add_argument('--job-repeat', default=1, type=int, |
| 346 help='How many times to run the job.') | 353 help='How many times to run the job.') |
| 347 run_parser.add_argument('--wpr-archive', default=None, type=str, | 354 run_parser.add_argument('--wpr-archive', default=None, type=str, |
| 348 dest='wpr_archive_path', | 355 dest='wpr_archive_path', |
| 349 help='Web page replay archive to load job\'s urls ' + | 356 help='Web page replay archive to load job\'s urls ' + |
| 350 'from.') | 357 'from.') |
| 358 | |
| 359 # Pull metrics subcommand. | |
| 360 create_cache_parser = subparsers.add_parser('pull-metrics', | |
| 361 help='Pulls metrics in CSV from a run\'s loading trace.') | |
| 362 create_cache_parser.add_argument('--traces-directory', required=True, | |
| 363 dest='trace_output_directory', type=str, | |
| 364 help='Path of loading traces directory.') | |
| 365 create_cache_parser.add_argument('--out-metrics', default=None, type=str, | |
| 366 dest='metrics_csv_path', | |
| 367 help='Path where to save the metrics\'s '+ | |
| 368 'CSV.') | |
| 369 | |
| 351 return parser | 370 return parser |
| 352 | 371 |
| 353 | 372 |
| 354 def _RecordWprMain(args): | 373 def _RecordWprMain(args): |
| 355 sandwich_runner = SandwichRunner(args.job) | 374 sandwich_runner = SandwichRunner(args.job) |
| 356 sandwich_runner.PullConfigFromArgs(args) | 375 sandwich_runner.PullConfigFromArgs(args) |
| 357 sandwich_runner.wpr_record = True | 376 sandwich_runner.wpr_record = True |
| 358 sandwich_runner.PrintConfig() | 377 sandwich_runner.PrintConfig() |
| 378 if not os.path.isdir(os.path.dirname(args.wpr_archive_path)): | |
| 379 os.makedirs(os.path.dirname(args.wpr_archive_path)) | |
| 359 sandwich_runner.Run() | 380 sandwich_runner.Run() |
| 360 return 0 | 381 return 0 |
| 361 | 382 |
| 362 | 383 |
| 363 def _PatchWprMain(args): | 384 def _PatchWprMain(args): |
| 364 # Sets the resources cache max-age to 10 years. | 385 # Sets the resources cache max-age to 10 years. |
| 365 MAX_AGE = 10 * 365 * 24 * 60 * 60 | 386 MAX_AGE = 10 * 365 * 24 * 60 * 60 |
| 366 CACHE_CONTROL = 'public, max-age={}'.format(MAX_AGE) | 387 CACHE_CONTROL = 'public, max-age={}'.format(MAX_AGE) |
| 367 | 388 |
| 368 wpr_archive = wpr_backend.WprArchiveBackend(args.wpr_archive_path) | 389 wpr_archive = wpr_backend.WprArchiveBackend(args.wpr_archive_path) |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 387 url_entry.SetResponseHeader('cache-control', CACHE_CONTROL) | 408 url_entry.SetResponseHeader('cache-control', CACHE_CONTROL) |
| 388 wpr_archive.Persist() | 409 wpr_archive.Persist() |
| 389 return 0 | 410 return 0 |
| 390 | 411 |
| 391 | 412 |
| 392 def _CreateCacheMain(args): | 413 def _CreateCacheMain(args): |
| 393 sandwich_runner = SandwichRunner(args.job) | 414 sandwich_runner = SandwichRunner(args.job) |
| 394 sandwich_runner.PullConfigFromArgs(args) | 415 sandwich_runner.PullConfigFromArgs(args) |
| 395 sandwich_runner.cache_operation = 'save' | 416 sandwich_runner.cache_operation = 'save' |
| 396 sandwich_runner.PrintConfig() | 417 sandwich_runner.PrintConfig() |
| 418 if not os.path.isdir(os.path.dirname(args.cache_archive_path)): | |
| 419 os.makedirs(os.path.dirname(args.cache_archive_path)) | |
| 397 sandwich_runner.Run() | 420 sandwich_runner.Run() |
| 398 return 0 | 421 return 0 |
| 399 | 422 |
| 400 | 423 |
| 401 def _RunJobMain(args): | 424 def _RunJobMain(args): |
| 402 sandwich_runner = SandwichRunner(args.job) | 425 sandwich_runner = SandwichRunner(args.job) |
| 403 sandwich_runner.PullConfigFromArgs(args) | 426 sandwich_runner.PullConfigFromArgs(args) |
| 404 sandwich_runner.PrintConfig() | 427 sandwich_runner.PrintConfig() |
| 405 sandwich_runner.Run() | 428 sandwich_runner.Run() |
| 406 return 0 | 429 return 0 |
| 407 | 430 |
| 408 | 431 |
| 432 def _PullMetricsMain(args): | |
| 433 trace_metrics_list = pull_sandwich_metrics.PullMetricsFromOutputDirectory( | |
| 434 args.trace_output_directory) | |
| 435 trace_metrics_list.sort(key=lambda e: e['id']) | |
| 436 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
| |
| 437 writer = csv.DictWriter(csv_file, | |
| 438 fieldnames=pull_sandwich_metrics.CSV_FIELD_NAMES) | |
| 439 writer.writeheader() | |
| 440 for trace_metrics in trace_metrics_list: | |
| 441 writer.writerow(trace_metrics) | |
| 442 return 0 | |
| 443 | |
| 444 | |
| 409 def main(command_line_args): | 445 def main(command_line_args): |
| 410 logging.basicConfig(level=logging.INFO) | 446 logging.basicConfig(level=logging.INFO) |
| 411 devil_chromium.Initialize() | 447 devil_chromium.Initialize() |
| 412 | 448 |
| 413 # Don't give the argument yet. All we are interested in for now is accessing | 449 # Don't give the argument yet. All we are interested in for now is accessing |
| 414 # the default values of OPTIONS. | 450 # the default values of OPTIONS. |
| 415 OPTIONS.ParseArgs([]) | 451 OPTIONS.ParseArgs([]) |
| 416 | 452 |
| 417 args = _ArgumentParser().parse_args(command_line_args) | 453 args = _ArgumentParser().parse_args(command_line_args) |
| 418 | 454 |
| 419 if args.subcommand == 'record-wpr': | 455 if args.subcommand == 'record-wpr': |
| 420 return _RecordWprMain(args) | 456 return _RecordWprMain(args) |
| 421 if args.subcommand == 'patch-wpr': | 457 if args.subcommand == 'patch-wpr': |
| 422 return _PatchWprMain(args) | 458 return _PatchWprMain(args) |
| 423 if args.subcommand == 'create-cache': | 459 if args.subcommand == 'create-cache': |
| 424 return _CreateCacheMain(args) | 460 return _CreateCacheMain(args) |
| 425 if args.subcommand == 'run': | 461 if args.subcommand == 'run': |
| 426 return _RunJobMain(args) | 462 return _RunJobMain(args) |
| 463 if args.subcommand == 'pull-metrics': | |
| 464 return _PullMetricsMain(args) | |
| 427 assert False | 465 assert False |
| 428 | 466 |
| 429 | 467 |
| 430 if __name__ == '__main__': | 468 if __name__ == '__main__': |
| 431 sys.exit(main(sys.argv[1:])) | 469 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |