csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [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 ArgumentParser |
| 11 | from datetime import datetime |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 12 | import collections |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 13 | import operator |
| 14 | import os |
| 15 | import sys |
| 16 | import tempfile |
| 17 | import urllib |
| 18 | import urlparse |
| 19 | import webbrowser |
| 20 | |
csmartdalton | bf41fa8 | 2016-09-23 11:36:11 -0700 | [diff] [blame] | 21 | __argparse = ArgumentParser(description=""" |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 22 | |
| 23 | Parses output files from skpbench.py into csv. |
| 24 | |
| 25 | This script can also be used to generate a Google sheet: |
| 26 | |
| 27 | (1) Install the "Office Editing for Docs, Sheets & Slides" Chrome extension: |
| 28 | https://chrome.google.com/webstore/detail/office-editing-for-docs-s/gbkeegbaiigmenfmjfclcdgdpimamgkj |
| 29 | |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 30 | (2) Designate Chrome os-wide as the default application for opening .csv files. |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 31 | |
| 32 | (3) Run parseskpbench.py with the --open flag. |
| 33 | |
csmartdalton | bf41fa8 | 2016-09-23 11:36:11 -0700 | [diff] [blame] | 34 | """) |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 35 | |
| 36 | __argparse.add_argument('-r', '--result', |
csmartdalton | 6904b19 | 2016-09-29 06:23:23 -0700 | [diff] [blame] | 37 | choices=['accum', 'median', 'max', 'min'], default='accum', |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 38 | help="result to use for cell values") |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 39 | __argparse.add_argument('-f', '--force', |
| 40 | action='store_true', help='silently ignore warnings') |
| 41 | __argparse.add_argument('-o', '--open', |
| 42 | action='store_true', |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 43 | help="generate a temp file and open it (theoretically in a web browser)") |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 44 | __argparse.add_argument('-n', '--name', |
| 45 | default='skpbench_%s' % datetime.now().strftime('%Y-%m-%d_%H.%M.%S.csv'), |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 46 | help="if using --open, a name for the temp file") |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 47 | __argparse.add_argument('sources', |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 48 | nargs='+', help="source files with skpbench results ('-' for stdin)") |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 49 | |
| 50 | FLAGS = __argparse.parse_args() |
| 51 | |
| 52 | |
| 53 | class Parser: |
| 54 | def __init__(self): |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 55 | self.configs = list() # use list to preserve the order configs appear in. |
| 56 | self.rows = collections.defaultdict(dict) |
| 57 | self.cols = collections.defaultdict(dict) |
| 58 | self.metric = None |
| 59 | self.sample_ms = None |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 60 | |
| 61 | def parse_file(self, infile): |
| 62 | for line in infile: |
| 63 | match = BenchResult.match(line) |
| 64 | if not match: |
| 65 | continue |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 66 | if self.metric is None: |
| 67 | self.metric = match.metric |
| 68 | elif match.metric != self.metric: |
| 69 | raise ValueError("results have mismatched metrics (%s and %s)" % |
| 70 | (self.metric, match.metric)) |
| 71 | if self.sample_ms is None: |
| 72 | self.sample_ms = match.sample_ms |
| 73 | elif not FLAGS.force and match.sample_ms != self.sample_ms: |
| 74 | raise ValueError("results have mismatched sampling times. " |
| 75 | "(use --force to ignore)") |
| 76 | if not match.config in self.configs: |
| 77 | self.configs.append(match.config) |
| 78 | self.rows[match.bench][match.config] = match.get_string(FLAGS.result) |
| 79 | self.cols[match.config][match.bench] = getattr(match, FLAGS.result) |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 80 | |
| 81 | def print_csv(self, outfile=sys.stdout): |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 82 | print('%s_%s' % (FLAGS.result, self.metric), file=outfile) |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 83 | |
| 84 | # Write the header. |
| 85 | outfile.write('bench,') |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 86 | for config in self.configs: |
| 87 | outfile.write('%s,' % config) |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 88 | outfile.write('\n') |
| 89 | |
| 90 | # Write the rows. |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 91 | for bench, row in self.rows.items(): |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 92 | outfile.write('%s,' % bench) |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 93 | for config in self.configs: |
| 94 | if config in row: |
| 95 | outfile.write('%s,' % row[config]) |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 96 | elif FLAGS.force: |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 97 | outfile.write(',') |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 98 | else: |
csmartdalton | d7a9db6 | 2016-09-22 05:10:02 -0700 | [diff] [blame] | 99 | raise ValueError("%s: missing value for %s. (use --force to ignore)" % |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 100 | (bench, config)) |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 101 | outfile.write('\n') |
| 102 | |
| 103 | # Add simple, literal averages. |
| 104 | if len(self.rows) > 1: |
| 105 | outfile.write('\n') |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 106 | self.__print_computed_row('MEAN', |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 107 | lambda col: reduce(operator.add, col.values()) / len(col), |
| 108 | outfile=outfile) |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 109 | self.__print_computed_row('GEOMEAN', |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 110 | lambda col: reduce(operator.mul, col.values()) ** (1.0 / len(col)), |
| 111 | outfile=outfile) |
| 112 | |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 113 | def __print_computed_row(self, name, func, outfile=sys.stdout): |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 114 | outfile.write('%s,' % name) |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 115 | for config in self.configs: |
| 116 | assert(len(self.cols[config]) == len(self.rows)) |
| 117 | outfile.write('%.4g,' % func(self.cols[config])) |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 118 | outfile.write('\n') |
| 119 | |
mtklein | 56df2de | 2016-10-04 12:49:45 -0700 | [diff] [blame] | 120 | |
csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 121 | def main(): |
| 122 | parser = Parser() |
| 123 | |
| 124 | # Parse the input files. |
| 125 | for src in FLAGS.sources: |
| 126 | if src == '-': |
| 127 | parser.parse_file(sys.stdin) |
| 128 | else: |
| 129 | with open(src, mode='r') as infile: |
| 130 | parser.parse_file(infile) |
| 131 | |
| 132 | # Print the csv. |
| 133 | if not FLAGS.open: |
| 134 | parser.print_csv() |
| 135 | else: |
| 136 | dirname = tempfile.mkdtemp() |
| 137 | basename = FLAGS.name |
| 138 | if os.path.splitext(basename)[1] != '.csv': |
| 139 | basename += '.csv'; |
| 140 | pathname = os.path.join(dirname, basename) |
| 141 | with open(pathname, mode='w') as tmpfile: |
| 142 | parser.print_csv(outfile=tmpfile) |
| 143 | fileuri = urlparse.urljoin('file:', urllib.pathname2url(pathname)) |
| 144 | print('opening %s' % fileuri) |
| 145 | webbrowser.open(fileuri) |
| 146 | |
| 147 | |
| 148 | if __name__ == '__main__': |
| 149 | main() |