OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 # Copyright 2016 Google Inc. | 3 # Copyright 2016 Google Inc. |
4 # | 4 # |
5 # Use of this source code is governed by a BSD-style license that can be | 5 # Use of this source code is governed by a BSD-style license that can be |
6 # found in the LICENSE file. | 6 # found in the LICENSE file. |
7 | 7 |
8 from __future__ import print_function | 8 from __future__ import print_function |
9 from _benchresult import BenchResult | 9 from _benchresult import BenchResult |
10 from argparse import ArgumentParser | 10 from argparse import ArgumentParser |
11 from datetime import datetime | 11 from datetime import datetime |
12 import collections | 12 import collections |
13 import operator | 13 import operator |
14 import os | 14 import os |
15 import sys | 15 import sys |
16 import tempfile | 16 import tempfile |
17 import urllib | 17 import urllib |
18 import urlparse | 18 import urlparse |
19 import webbrowser | 19 import webbrowser |
20 | 20 |
21 __argparse = ArgumentParser(description=""" | 21 __argparse = ArgumentParser(description=''' |
22 | 22 |
23 Parses output files from skpbench.py into csv. | 23 Parses output files from skpbench.py into csv. |
24 | 24 |
25 This script can also be used to generate a Google sheet: | 25 This script can also be used to generate a Google sheet: |
26 | 26 |
27 (1) Install the "Office Editing for Docs, Sheets & Slides" Chrome extension: | 27 (1) Install the "Office Editing for Docs, Sheets & Slides" Chrome extension: |
28 https://chrome.google.com/webstore/detail/office-editing-for-docs-s/gbkeegba
iigmenfmjfclcdgdpimamgkj | 28 https://chrome.google.com/webstore/detail/office-editing-for-docs-s/gbkeegba
iigmenfmjfclcdgdpimamgkj |
29 | 29 |
30 (2) Designate Chrome os-wide as the default application for opening .csv files. | 30 (2) Designate Chrome os-wide as the default application for opening .csv files. |
31 | 31 |
32 (3) Run parseskpbench.py with the --open flag. | 32 (3) Run parseskpbench.py with the --open flag. |
33 | 33 |
34 """) | 34 ''') |
35 | 35 |
36 __argparse.add_argument('-r', '--result', | 36 __argparse.add_argument('-r', '--result', |
37 choices=['median', 'accum', 'max', 'min'], default='median', | 37 choices=['median', 'accum', 'max', 'min'], default='median', |
38 help='result to use for cell values') | 38 help="result to use for cell values") |
39 __argparse.add_argument('-f', '--force', | 39 __argparse.add_argument('-f', '--force', |
40 action='store_true', help='silently ignore warnings') | 40 action='store_true', help='silently ignore warnings') |
41 __argparse.add_argument('-o', '--open', | 41 __argparse.add_argument('-o', '--open', |
42 action='store_true', | 42 action='store_true', |
43 help='generate a temp file and open it (theoretically in a web browser)') | 43 help="generate a temp file and open it (theoretically in a web browser)") |
44 __argparse.add_argument('-n', '--name', | 44 __argparse.add_argument('-n', '--name', |
45 default='skpbench_%s' % datetime.now().strftime('%Y-%m-%d_%H.%M.%S.csv'), | 45 default='skpbench_%s' % datetime.now().strftime('%Y-%m-%d_%H.%M.%S.csv'), |
46 help='if using --open, a name for the temp file') | 46 help="if using --open, a name for the temp file") |
47 __argparse.add_argument('sources', | 47 __argparse.add_argument('sources', |
48 nargs='+', help='source files with skpbench results ("-" for stdin)') | 48 nargs='+', help="source files with skpbench results ('-' for stdin)") |
49 | 49 |
50 FLAGS = __argparse.parse_args() | 50 FLAGS = __argparse.parse_args() |
51 | 51 |
52 | 52 |
53 class Parser: | 53 class Parser: |
54 def __init__(self): | 54 def __init__(self): |
55 self.configs = list() # use list to preserve the order configs appear in. | 55 self.configs = list() # use list to preserve the order configs appear in. |
56 self.rows = collections.defaultdict(dict) | 56 self.rows = collections.defaultdict(dict) |
57 self.cols = collections.defaultdict(dict) | 57 self.cols = collections.defaultdict(dict) |
58 self.metric = None | 58 self.metric = None |
59 self.samples = None | 59 self.samples = None |
60 self.sample_ms = None | 60 self.sample_ms = None |
61 | 61 |
62 def parse_file(self, infile): | 62 def parse_file(self, infile): |
63 for line in infile: | 63 for line in infile: |
64 match = BenchResult.match(line) | 64 match = BenchResult.match(line) |
65 if not match: | 65 if not match: |
66 continue | 66 continue |
67 if self.metric is None: | 67 if self.metric is None: |
68 self.metric = match.metric | 68 self.metric = match.metric |
69 elif match.metric != self.metric: | 69 elif match.metric != self.metric: |
70 raise ValueError('results have mismatched metrics (%s and %s)' % | 70 raise ValueError("results have mismatched metrics (%s and %s)" % |
71 (self.metric, match.metric)) | 71 (self.metric, match.metric)) |
72 if self.samples is None: | 72 if self.samples is None: |
73 self.samples = match.samples | 73 self.samples = match.samples |
74 elif not FLAGS.force and match.samples != self.samples: | 74 elif not FLAGS.force and match.samples != self.samples: |
75 raise ValueError('results have mismatched number of samples. ' | 75 raise ValueError("results have mismatched number of samples. " |
76 '(use --force to ignore)') | 76 "(use --force to ignore)") |
77 if self.sample_ms is None: | 77 if self.sample_ms is None: |
78 self.sample_ms = match.sample_ms | 78 self.sample_ms = match.sample_ms |
79 elif not FLAGS.force and match.sample_ms != self.sample_ms: | 79 elif not FLAGS.force and match.sample_ms != self.sample_ms: |
80 raise ValueError('results have mismatched sampling times. ' | 80 raise ValueError("results have mismatched sampling times. " |
81 '(use --force to ignore)') | 81 "(use --force to ignore)") |
82 if not match.config in self.configs: | 82 if not match.config in self.configs: |
83 self.configs.append(match.config) | 83 self.configs.append(match.config) |
84 self.rows[match.bench][match.config] = match.get_string(FLAGS.result) | 84 self.rows[match.bench][match.config] = match.get_string(FLAGS.result) |
85 self.cols[match.config][match.bench] = getattr(match, FLAGS.result) | 85 self.cols[match.config][match.bench] = getattr(match, FLAGS.result) |
86 | 86 |
87 def print_csv(self, outfile=sys.stdout): | 87 def print_csv(self, outfile=sys.stdout): |
88 print('%s_%s' % (FLAGS.result, self.metric), file=outfile) | 88 print('%s_%s' % (FLAGS.result, self.metric), file=outfile) |
89 | 89 |
90 # Write the header. | 90 # Write the header. |
91 outfile.write('bench,') | 91 outfile.write('bench,') |
92 for config in self.configs: | 92 for config in self.configs: |
93 outfile.write('%s,' % config) | 93 outfile.write('%s,' % config) |
94 outfile.write('\n') | 94 outfile.write('\n') |
95 | 95 |
96 # Write the rows. | 96 # Write the rows. |
97 for bench, row in self.rows.items(): | 97 for bench, row in self.rows.items(): |
98 outfile.write('%s,' % bench) | 98 outfile.write('%s,' % bench) |
99 for config in self.configs: | 99 for config in self.configs: |
100 if config in row: | 100 if config in row: |
101 outfile.write('%s,' % row[config]) | 101 outfile.write('%s,' % row[config]) |
102 elif FLAGS.force: | 102 elif FLAGS.force: |
103 outfile.write(',') | 103 outfile.write(',') |
104 else: | 104 else: |
105 raise ValueError('%s: missing value for %s. (use --force to ignore)' % | 105 raise ValueError("%s: missing value for %s. (use --force to ignore)" % |
106 (bench, config)) | 106 (bench, config)) |
107 outfile.write('\n') | 107 outfile.write('\n') |
108 | 108 |
109 # Add simple, literal averages. | 109 # Add simple, literal averages. |
110 if len(self.rows) > 1: | 110 if len(self.rows) > 1: |
111 outfile.write('\n') | 111 outfile.write('\n') |
112 self.__print_computed_row('MEAN', | 112 self.__print_computed_row('MEAN', |
113 lambda col: reduce(operator.add, col.values()) / len(col), | 113 lambda col: reduce(operator.add, col.values()) / len(col), |
114 outfile=outfile) | 114 outfile=outfile) |
115 self.__print_computed_row('GEOMEAN', | 115 self.__print_computed_row('GEOMEAN', |
(...skipping 30 matching lines...) Expand all Loading... |
146 pathname = os.path.join(dirname, basename) | 146 pathname = os.path.join(dirname, basename) |
147 with open(pathname, mode='w') as tmpfile: | 147 with open(pathname, mode='w') as tmpfile: |
148 parser.print_csv(outfile=tmpfile) | 148 parser.print_csv(outfile=tmpfile) |
149 fileuri = urlparse.urljoin('file:', urllib.pathname2url(pathname)) | 149 fileuri = urlparse.urljoin('file:', urllib.pathname2url(pathname)) |
150 print('opening %s' % fileuri) | 150 print('opening %s' % fileuri) |
151 webbrowser.open(fileuri) | 151 webbrowser.open(fileuri) |
152 | 152 |
153 | 153 |
154 if __name__ == '__main__': | 154 if __name__ == '__main__': |
155 main() | 155 main() |
OLD | NEW |