| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 import csv | |
| 5 import logging | |
| 6 import os | |
| 7 import sys | |
| 8 | |
| 9 from telemetry.core import browser_finder | |
| 10 from telemetry.core import browser_options | |
| 11 from telemetry.page import all_page_actions # pylint: disable=W0611 | |
| 12 from telemetry.page import block_page_benchmark_results | |
| 13 from telemetry.page import csv_page_benchmark_results | |
| 14 from telemetry.page import multi_page_benchmark | |
| 15 from telemetry.page import page_runner | |
| 16 from telemetry.page import page_set | |
| 17 from telemetry.test import discover | |
| 18 | |
| 19 def Main(benchmark_dir): | |
| 20 """Turns a MultiPageBenchmark into a command-line program. | |
| 21 | |
| 22 Args: | |
| 23 benchmark_dir: Path to directory containing MultiPageBenchmarks. | |
| 24 """ | |
| 25 benchmarks = discover.Discover(benchmark_dir, | |
| 26 os.path.join(benchmark_dir, '..'), | |
| 27 '', | |
| 28 multi_page_benchmark.MultiPageBenchmark) | |
| 29 | |
| 30 # Naively find the benchmark. If we use the browser options parser, we run | |
| 31 # the risk of failing to parse if we use a benchmark-specific parameter. | |
| 32 benchmark_name = None | |
| 33 for arg in sys.argv: | |
| 34 if arg in benchmarks: | |
| 35 benchmark_name = arg | |
| 36 | |
| 37 options = browser_options.BrowserOptions() | |
| 38 parser = options.CreateParser('%prog [options] <benchmark> <page_set>') | |
| 39 | |
| 40 page_runner.PageRunner.AddCommandLineOptions(parser) | |
| 41 parser.add_option('--output-format', | |
| 42 dest='output_format', | |
| 43 default='csv', | |
| 44 help='Output format. Can be "csv" or "block". ' | |
| 45 'Defaults to "%default".') | |
| 46 parser.add_option('-o', '--output', | |
| 47 dest='output_file', | |
| 48 help='Redirects output to a file. Defaults to stdout.') | |
| 49 | |
| 50 benchmark = None | |
| 51 if benchmark_name is not None: | |
| 52 benchmark = benchmarks[benchmark_name]() | |
| 53 benchmark.AddCommandLineOptions(parser) | |
| 54 | |
| 55 _, args = parser.parse_args() | |
| 56 | |
| 57 if benchmark is None or len(args) != 2: | |
| 58 parser.print_usage() | |
| 59 import page_sets # pylint: disable=F0401 | |
| 60 print >> sys.stderr, 'Available benchmarks:\n%s\n' % ',\n'.join( | |
| 61 sorted(benchmarks.keys())) | |
| 62 print >> sys.stderr, 'Available page_sets:\n%s\n' % ',\n'.join( | |
| 63 sorted([os.path.relpath(f) | |
| 64 for f in page_sets.GetAllPageSetFilenames()])) | |
| 65 sys.exit(1) | |
| 66 | |
| 67 ps = page_set.PageSet.FromFile(args[1]) | |
| 68 | |
| 69 benchmark.CustomizeBrowserOptions(options) | |
| 70 possible_browser = browser_finder.FindBrowser(options) | |
| 71 if not possible_browser: | |
| 72 print >> sys.stderr, """No browser found.\n | |
| 73 Use --browser=list to figure out which are available.\n""" | |
| 74 sys.exit(1) | |
| 75 | |
| 76 if not options.output_file: | |
| 77 output_file = sys.stdout | |
| 78 elif options.output_file == '-': | |
| 79 output_file = sys.stdout | |
| 80 else: | |
| 81 output_file = open(os.path.expanduser(options.output_file), 'w') | |
| 82 | |
| 83 if options.output_format == 'csv': | |
| 84 results = csv_page_benchmark_results.CsvPageBenchmarkResults( | |
| 85 csv.writer(output_file), | |
| 86 benchmark.results_are_the_same_on_every_page) | |
| 87 elif options.output_format in ('block', 'terminal-block'): | |
| 88 results = block_page_benchmark_results.BlockPageBenchmarkResults( | |
| 89 output_file) | |
| 90 else: | |
| 91 raise Exception('Invalid --output-format value: "%s". Valid values are ' | |
| 92 '"csv" and "block".' | |
| 93 % options.output_format) | |
| 94 | |
| 95 with page_runner.PageRunner(ps) as runner: | |
| 96 runner.Run(options, possible_browser, benchmark, results) | |
| 97 # When using an exact executable, assume it is a reference build for the | |
| 98 # purpose of outputting the perf results. | |
| 99 results.PrintSummary(options.browser_executable and '_ref' or '') | |
| 100 | |
| 101 if len(results.page_failures): | |
| 102 logging.warning('Failed pages: %s', '\n'.join( | |
| 103 [failure['page'].url for failure in results.page_failures])) | |
| 104 | |
| 105 if len(results.skipped_pages): | |
| 106 logging.warning('Skipped pages: %s', '\n'.join( | |
| 107 [skipped['page'].url for skipped in results.skipped_pages])) | |
| 108 return min(255, len(results.page_failures)) | |
| OLD | NEW |