Kevin Lubick | 9c2249f | 2016-11-10 14:19:00 -0500 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2016 Google Inc. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | from _benchresult import BenchResult |
| 10 | from argparse import ArgumentTypeError, ArgumentParser |
| 11 | from collections import defaultdict |
| 12 | import json |
| 13 | import sys |
| 14 | |
| 15 | __argparse = ArgumentParser(description=""" |
| 16 | |
| 17 | Formats skpbench.py outputs for Skia Perf. |
| 18 | |
| 19 | """) |
| 20 | |
| 21 | __argparse.add_argument('sources', |
| 22 | nargs='+', help="source files that contain skpbench results") |
| 23 | __argparse.add_argument('--properties', |
| 24 | nargs='*', default=list(), |
| 25 | help="space-separated key/value pairs identifying the run") |
| 26 | __argparse.add_argument('--key', |
| 27 | nargs='*', default=list(), |
| 28 | help="space-separated key/value pairs identifying the builder") |
| 29 | __argparse.add_argument('-o', '--outfile', |
| 30 | default='-', help="output file ('-' for stdout)") |
| 31 | |
| 32 | FLAGS = __argparse.parse_args() |
| 33 | |
| 34 | def parse_key_value_pairs(args): |
| 35 | if not args: |
| 36 | return dict() |
| 37 | if len(args) % 2: |
| 38 | raise ArgumentTypeError("uneven number of key/value arguments.") |
| 39 | return {k:v for k,v in zip(args[::2], args[1::2])} |
| 40 | |
| 41 | def skiaperf_result(benchresult): |
| 42 | result = {x:benchresult.get_string(x) for x in ('accum', 'median')} |
| 43 | result['options'] = {x:benchresult.get_string(x) |
| 44 | for x in ('clock', 'metric', 'sample_ms')} |
| 45 | return result |
| 46 | |
| 47 | def emit_as_json(data, outfile): |
| 48 | json.dump(data, outfile, indent=4, separators=(',', ' : '), sort_keys=True) |
| 49 | print('', file=outfile) |
| 50 | |
| 51 | def main(): |
| 52 | data = parse_key_value_pairs( |
| 53 | FLAGS.properties + [ |
| 54 | 'key', parse_key_value_pairs(FLAGS.key), |
| 55 | 'results', defaultdict(dict)]) |
| 56 | |
| 57 | for src in FLAGS.sources: |
| 58 | with open(src, mode='r') as infile: |
| 59 | for line in infile: |
| 60 | match = BenchResult.match(line) |
| 61 | if match: |
| 62 | data['results'][match.bench][match.config] = skiaperf_result(match) |
| 63 | |
| 64 | if FLAGS.outfile != '-': |
| 65 | with open(FLAGS.outfile, 'w+') as outfile: |
| 66 | emit_as_json(data, outfile) |
| 67 | else: |
| 68 | emit_as_json(data, sys.stdout) |
| 69 | |
| 70 | if __name__ == '__main__': |
| 71 | main() |