Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: tools/android/loading/sandwich.py

Issue 1726403005: sandwich: Makes pull_sandwich_metrics.py a sub-command. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@i09
Patch Set: Removes useless import Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/android/loading/pull_sandwich_metrics.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 shutil.rmtree(self._local_cache_directory_path) 279 shutil.rmtree(self._local_cache_directory_path)
279 self._local_cache_directory_path = None 280 self._local_cache_directory_path = None
280 if self.cache_operation == 'save': 281 if self.cache_operation == 'save':
281 self._PullCacheFromDevice() 282 self._PullCacheFromDevice()
282 if self.trace_output_directory: 283 if self.trace_output_directory:
283 self._SaveRunInfos(ran_urls) 284 self._SaveRunInfos(ran_urls)
284 285
285 286
286 def _ArgumentParser(): 287 def _ArgumentParser():
287 """Build a command line argument's parser.""" 288 """Build a command line argument's parser."""
289 # Command parser when dealing with jobs.
290 common_job_parser = argparse.ArgumentParser(add_help=False)
291 common_job_parser.add_argument('--job', required=True,
292 help='JSON file with job description.')
293
294 # Main parser
288 parser = argparse.ArgumentParser() 295 parser = argparse.ArgumentParser()
289 parser.add_argument('--job', required=True,
290 help='JSON file with job description.')
291 subparsers = parser.add_subparsers(dest='subcommand', help='subcommand line') 296 subparsers = parser.add_subparsers(dest='subcommand', help='subcommand line')
292 297
293 # Record WPR subcommand. 298 # Record WPR subcommand.
294 record_wpr = subparsers.add_parser('record-wpr', 299 record_wpr = subparsers.add_parser('record-wpr', parents=[common_job_parser],
295 help='Record WPR from sandwich job.') 300 help='Record WPR from sandwich job.')
296 record_wpr.add_argument('--wpr-archive', required=True, type=str, 301 record_wpr.add_argument('--wpr-archive', required=True, type=str,
297 dest='wpr_archive_path', 302 dest='wpr_archive_path',
298 help='Web page replay archive to generate.') 303 help='Web page replay archive to generate.')
299 304
300 # Patch WPR subcommand. 305 # Patch WPR subcommand.
301 patch_wpr = subparsers.add_parser('patch-wpr', 306 patch_wpr = subparsers.add_parser('patch-wpr',
302 help='Patch WPR response headers.') 307 help='Patch WPR response headers.')
303 patch_wpr.add_argument('--wpr-archive', required=True, type=str, 308 patch_wpr.add_argument('--wpr-archive', required=True, type=str,
304 dest='wpr_archive_path', 309 dest='wpr_archive_path',
305 help='Web page replay archive to generate.') 310 help='Web page replay archive to patch.')
pasko 2016/02/26 14:07:13 nit: maybe better to say: to 'generate or patch'?
gabadie 2016/03/01 10:20:27 No because for this sub command, we are only patch
306 311
307 # Create cache subcommand. 312 # Create cache subcommand.
308 create_cache_parser = subparsers.add_parser('create-cache', 313 create_cache_parser = subparsers.add_parser('create-cache',
314 parents=[common_job_parser],
309 help='Create cache from sandwich job.') 315 help='Create cache from sandwich job.')
310 create_cache_parser.add_argument('--cache-archive', required=True, type=str, 316 create_cache_parser.add_argument('--cache-archive', required=True, type=str,
311 dest='cache_archive_path', 317 dest='cache_archive_path',
312 help='Cache archive destination path.') 318 help='Cache archive destination path.')
313 create_cache_parser.add_argument('--wpr-archive', default=None, type=str, 319 create_cache_parser.add_argument('--wpr-archive', default=None, type=str,
314 dest='wpr_archive_path', 320 dest='wpr_archive_path',
315 help='Web page replay archive to create ' + 321 help='Web page replay archive to create ' +
316 'the cache from.') 322 'the cache from.')
317 323
318 # Run subcommand. 324 # Run subcommand.
319 run_parser = subparsers.add_parser('run', help='Run sandwich benchmark.') 325 run_parser = subparsers.add_parser('run', parents=[common_job_parser],
326 help='Run sandwich benchmark.')
320 run_parser.add_argument('--output', required=True, type=str, 327 run_parser.add_argument('--output', required=True, type=str,
321 dest='trace_output_directory', 328 dest='trace_output_directory',
322 help='Path of output directory to create.') 329 help='Path of output directory to create.')
323 run_parser.add_argument('--cache-archive', type=str, 330 run_parser.add_argument('--cache-archive', type=str,
324 dest='cache_archive_path', 331 dest='cache_archive_path',
325 help='Cache archive destination path.') 332 help='Cache archive destination path.')
326 run_parser.add_argument('--cache-op', 333 run_parser.add_argument('--cache-op',
327 choices=['clear', 'push', 'reload'], 334 choices=['clear', 'push', 'reload'],
328 dest='cache_operation', 335 dest='cache_operation',
329 default='clear', 336 default='clear',
(...skipping 13 matching lines...) Expand all
343 choices=['browser', 'wpr'], 350 choices=['browser', 'wpr'],
344 help='Set which component is emulating the network condition.' + 351 help='Set which component is emulating the network condition.' +
345 ' (Default to browser). Wpr network emulator requires --wpr-archive' + 352 ' (Default to browser). Wpr network emulator requires --wpr-archive' +
346 ' to be set.') 353 ' to be set.')
347 run_parser.add_argument('--job-repeat', default=1, type=int, 354 run_parser.add_argument('--job-repeat', default=1, type=int,
348 help='How many times to run the job.') 355 help='How many times to run the job.')
349 run_parser.add_argument('--wpr-archive', default=None, type=str, 356 run_parser.add_argument('--wpr-archive', default=None, type=str,
350 dest='wpr_archive_path', 357 dest='wpr_archive_path',
351 help='Web page replay archive to load job\'s urls ' + 358 help='Web page replay archive to load job\'s urls ' +
352 'from.') 359 'from.')
360
361 # Pull metrics subcommand.
362 create_cache_parser = subparsers.add_parser('pull-metrics',
363 help='Pulls metrics in CSV from a run\'s loading trace.')
pasko 2016/02/26 14:07:13 Extracts metrics from a loading trace and saves as
gabadie 2016/03/01 10:20:27 Done.
364 create_cache_parser.add_argument('--traces-directory', required=True,
pasko 2016/02/26 14:07:14 nit: trace-directory
gabadie 2016/03/01 10:20:27 Done.
365 dest='trace_output_directory', type=str,
366 help='Path of loading traces directory.')
367 create_cache_parser.add_argument('--out-metrics', default=None, type=str,
368 dest='metrics_csv_path',
369 help='Path where to save the metrics\'s '+
370 'CSV.')
371
353 return parser 372 return parser
354 373
355 374
356 def _RecordWprMain(args): 375 def _RecordWprMain(args):
357 sandwich_runner = SandwichRunner(args.job) 376 sandwich_runner = SandwichRunner(args.job)
358 sandwich_runner.PullConfigFromArgs(args) 377 sandwich_runner.PullConfigFromArgs(args)
359 sandwich_runner.wpr_record = True 378 sandwich_runner.wpr_record = True
360 sandwich_runner.PrintConfig() 379 sandwich_runner.PrintConfig()
380 if not os.path.isdir(os.path.dirname(args.wpr_archive_path)):
381 os.makedirs(os.path.dirname(args.wpr_archive_path))
361 sandwich_runner.Run() 382 sandwich_runner.Run()
362 return 0 383 return 0
363 384
364 385
365 def _PatchWprMain(args): 386 def _PatchWprMain(args):
366 # Sets the resources cache max-age to 10 years. 387 # Sets the resources cache max-age to 10 years.
367 MAX_AGE = 10 * 365 * 24 * 60 * 60 388 MAX_AGE = 10 * 365 * 24 * 60 * 60
368 CACHE_CONTROL = 'public, max-age={}'.format(MAX_AGE) 389 CACHE_CONTROL = 'public, max-age={}'.format(MAX_AGE)
369 390
370 wpr_archive = wpr_backend.WprArchiveBackend(args.wpr_archive_path) 391 wpr_archive = wpr_backend.WprArchiveBackend(args.wpr_archive_path)
(...skipping 18 matching lines...) Expand all
389 url_entry.SetResponseHeader('cache-control', CACHE_CONTROL) 410 url_entry.SetResponseHeader('cache-control', CACHE_CONTROL)
390 wpr_archive.Persist() 411 wpr_archive.Persist()
391 return 0 412 return 0
392 413
393 414
394 def _CreateCacheMain(args): 415 def _CreateCacheMain(args):
395 sandwich_runner = SandwichRunner(args.job) 416 sandwich_runner = SandwichRunner(args.job)
396 sandwich_runner.PullConfigFromArgs(args) 417 sandwich_runner.PullConfigFromArgs(args)
397 sandwich_runner.cache_operation = 'save' 418 sandwich_runner.cache_operation = 'save'
398 sandwich_runner.PrintConfig() 419 sandwich_runner.PrintConfig()
420 if not os.path.isdir(os.path.dirname(args.cache_archive_path)):
421 os.makedirs(os.path.dirname(args.cache_archive_path))
399 sandwich_runner.Run() 422 sandwich_runner.Run()
400 return 0 423 return 0
401 424
402 425
403 def _RunJobMain(args): 426 def _RunJobMain(args):
404 sandwich_runner = SandwichRunner(args.job) 427 sandwich_runner = SandwichRunner(args.job)
405 sandwich_runner.PullConfigFromArgs(args) 428 sandwich_runner.PullConfigFromArgs(args)
406 sandwich_runner.PrintConfig() 429 sandwich_runner.PrintConfig()
407 sandwich_runner.Run() 430 sandwich_runner.Run()
408 return 0 431 return 0
409 432
410 433
434 def _PullMetricsMain(args):
435 trace_metrics_list = pull_sandwich_metrics.PullMetricsFromOutputDirectory(
436 args.trace_output_directory)
437 trace_metrics_list.sort(key=lambda e: e['id'])
438 with open(args.metrics_csv_path, 'w') as csv_file:
439 writer = csv.DictWriter(csv_file,
440 fieldnames=pull_sandwich_metrics.CSV_FIELD_NAMES)
441 writer.writeheader()
442 for trace_metrics in trace_metrics_list:
443 writer.writerow(trace_metrics)
444 return 0
445
446
411 def main(command_line_args): 447 def main(command_line_args):
412 logging.basicConfig(level=logging.INFO) 448 logging.basicConfig(level=logging.INFO)
413 devil_chromium.Initialize() 449 devil_chromium.Initialize()
414 450
415 # Don't give the argument yet. All we are interested in for now is accessing 451 # Don't give the argument yet. All we are interested in for now is accessing
416 # the default values of OPTIONS. 452 # the default values of OPTIONS.
417 OPTIONS.ParseArgs([]) 453 OPTIONS.ParseArgs([])
418 454
419 args = _ArgumentParser().parse_args(command_line_args) 455 args = _ArgumentParser().parse_args(command_line_args)
420 456
421 if args.subcommand == 'record-wpr': 457 if args.subcommand == 'record-wpr':
422 return _RecordWprMain(args) 458 return _RecordWprMain(args)
423 if args.subcommand == 'patch-wpr': 459 if args.subcommand == 'patch-wpr':
424 return _PatchWprMain(args) 460 return _PatchWprMain(args)
425 if args.subcommand == 'create-cache': 461 if args.subcommand == 'create-cache':
426 return _CreateCacheMain(args) 462 return _CreateCacheMain(args)
427 if args.subcommand == 'run': 463 if args.subcommand == 'run':
428 return _RunJobMain(args) 464 return _RunJobMain(args)
465 if args.subcommand == 'pull-metrics':
466 return _PullMetricsMain(args)
429 assert False 467 assert False
430 468
431 469
432 if __name__ == '__main__': 470 if __name__ == '__main__':
433 sys.exit(main(sys.argv[1:])) 471 sys.exit(main(sys.argv[1:]))
OLDNEW
« no previous file with comments | « tools/android/loading/pull_sandwich_metrics.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698