Home | History | Annotate | Download | only in bin
      1 #!/usr/bin/env python
      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
      4 # found in the LICENSE file.
      5 
      6 import argparse
      7 import codecs
      8 import json
      9 import os
     10 import sys
     11 
     12 sys.path.insert(1, os.path.join(os.path.dirname(__file__), '..'))
     13 from tracing import results_renderer
     14 from tracing.metrics import metric_runner
     15 from tracing.metrics import discover
     16 
     17 def Main(argv):
     18   all_metrics = discover.DiscoverMetrics(
     19       ['/tracing/metrics/all_metrics.html'])
     20   parser = argparse.ArgumentParser(
     21       description='Runs metrics on local traces')
     22   parser.add_argument('trace_file_or_dir',
     23                       help='A trace file, or a dir containing trace files')
     24   parser.add_argument('metrics', nargs='+',
     25                       help=('Function names of registered metrics '
     26                             '(not filenames.) '
     27                             'Available metrics are: %s' %
     28                             ', '.join(all_metrics)),
     29                       choices=all_metrics, metavar='metricName')
     30   parser.add_argument('--filename', default='results', type=str,
     31                       help='Output file name (no extension)')
     32   parser.add_argument('--reset', action='store_true',
     33                       help=('Whether to ignore existing results in HTML file '
     34                             '(if it exists'))
     35   parser.add_argument('--also-output-json', action='store_true',
     36                       help=('Also output json file containing values. Note that'
     37                             'this only contains the results of current run'))
     38 
     39   args = parser.parse_args(argv[1:])
     40   trace_file_or_dir = os.path.abspath(args.trace_file_or_dir)
     41 
     42   if os.path.isdir(trace_file_or_dir):
     43     trace_dir = trace_file_or_dir
     44     traces = [os.path.join(trace_dir, trace) for trace in os.listdir(trace_dir)]
     45   else:
     46     traces = [trace_file_or_dir]
     47 
     48   results = {k: v.AsDict() for k, v in
     49              metric_runner.RunMetricOnTraces(traces, args.metrics).iteritems()}
     50 
     51   failures = []
     52   values = []
     53   for trace in traces:
     54     failures.extend(results[trace]['pairs'].get('failures', []))
     55     values.extend(results[trace]['pairs'].get('values', []))
     56   if failures:
     57     print 'Running metric failed:'
     58     for failure in failures:
     59       print failure['stack']
     60 
     61 
     62   output_file = args.filename + '.html'
     63   open(output_file, 'a').close() # Create file if it doesn't exist.
     64   with codecs.open(output_file, mode='r+', encoding='utf-8') as output_stream:
     65     results_renderer.RenderHTMLView(values, output_stream, args.reset)
     66     print 'HTML result created in file://' + os.path.abspath(output_file)
     67 
     68   if args.also_output_json:
     69     output_file = args.filename + '.json'
     70     with open(output_file, 'w') as f:
     71       json.dump(values, f, indent=2, sort_keys=True, separators=(',', ': '))
     72     print 'JSON result created in file://' + os.path.abspath(output_file)
     73 
     74 
     75 if __name__ == '__main__':
     76   sys.exit(Main(sys.argv))
     77