2016-09-19 18:03:58 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright 2016 Google Inc.
|
|
|
|
#
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
from _benchresult import BenchResult
|
|
|
|
from argparse import ArgumentParser
|
2016-10-05 15:42:03 +00:00
|
|
|
from collections import defaultdict, namedtuple
|
2016-09-19 18:03:58 +00:00
|
|
|
from datetime import datetime
|
|
|
|
import operator
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import urllib
|
|
|
|
import urlparse
|
|
|
|
import webbrowser
|
|
|
|
|
2016-09-23 18:36:11 +00:00
|
|
|
__argparse = ArgumentParser(description="""
|
2016-09-19 18:03:58 +00:00
|
|
|
|
2016-11-10 19:19:00 +00:00
|
|
|
Formats skpbench.py outputs as csv.
|
2016-09-19 18:03:58 +00:00
|
|
|
|
|
|
|
This script can also be used to generate a Google sheet:
|
|
|
|
|
|
|
|
(1) Install the "Office Editing for Docs, Sheets & Slides" Chrome extension:
|
|
|
|
https://chrome.google.com/webstore/detail/office-editing-for-docs-s/gbkeegbaiigmenfmjfclcdgdpimamgkj
|
|
|
|
|
2016-10-05 15:42:03 +00:00
|
|
|
(2) Update your global OS file associations to use Chrome for .csv files.
|
2016-09-19 18:03:58 +00:00
|
|
|
|
|
|
|
(3) Run parseskpbench.py with the --open flag.
|
|
|
|
|
2016-09-23 18:36:11 +00:00
|
|
|
""")
|
2016-09-19 18:03:58 +00:00
|
|
|
|
|
|
|
__argparse.add_argument('-r', '--result',
|
2016-11-10 19:19:00 +00:00
|
|
|
choices=['accum', 'median', 'max', 'min'], default='accum',
|
|
|
|
help="result to use for cell values")
|
2016-09-19 18:03:58 +00:00
|
|
|
__argparse.add_argument('-f', '--force',
|
2016-11-10 19:19:00 +00:00
|
|
|
action='store_true', help='silently ignore warnings')
|
2016-09-19 18:03:58 +00:00
|
|
|
__argparse.add_argument('-o', '--open',
|
2016-11-10 19:19:00 +00:00
|
|
|
action='store_true',
|
|
|
|
help="generate a temp file and open it (theoretically in a web browser)")
|
2016-09-19 18:03:58 +00:00
|
|
|
__argparse.add_argument('-n', '--name',
|
2016-11-10 19:19:00 +00:00
|
|
|
default='skpbench_%s' % datetime.now().strftime('%Y-%m-%d_%H.%M.%S.csv'),
|
|
|
|
help="if using --open, a name for the temp file")
|
2016-09-19 18:03:58 +00:00
|
|
|
__argparse.add_argument('sources',
|
2016-11-10 19:19:00 +00:00
|
|
|
nargs='+', help="source files that contain skpbench results ('-' for stdin)")
|
2016-09-19 18:03:58 +00:00
|
|
|
|
|
|
|
FLAGS = __argparse.parse_args()
|
|
|
|
|
2016-10-05 15:42:03 +00:00
|
|
|
RESULT_QUALIFIERS = ('sample_ms', 'clock', 'metric')
|
|
|
|
|
|
|
|
class FullConfig(namedtuple('fullconfig', ('config',) + RESULT_QUALIFIERS)):
|
|
|
|
def qualified_name(self, qualifiers=RESULT_QUALIFIERS):
|
|
|
|
return get_qualified_name(self.config.replace(',', ' '),
|
|
|
|
{x:getattr(self, x) for x in qualifiers})
|
|
|
|
|
|
|
|
def get_qualified_name(name, qualifiers):
|
|
|
|
if not qualifiers:
|
|
|
|
return name
|
|
|
|
else:
|
|
|
|
args = ('%s=%s' % (k,v) for k,v in qualifiers.iteritems())
|
|
|
|
return '%s (%s)' % (name, ' '.join(args))
|
2016-09-19 18:03:58 +00:00
|
|
|
|
|
|
|
class Parser:
|
|
|
|
def __init__(self):
|
2016-10-05 15:42:03 +00:00
|
|
|
self.sheet_qualifiers = {x:None for x in RESULT_QUALIFIERS}
|
|
|
|
self.config_qualifiers = set()
|
|
|
|
self.fullconfigs = list() # use list to preserve the order.
|
|
|
|
self.rows = defaultdict(dict)
|
|
|
|
self.cols = defaultdict(dict)
|
2016-09-19 18:03:58 +00:00
|
|
|
|
|
|
|
def parse_file(self, infile):
|
|
|
|
for line in infile:
|
|
|
|
match = BenchResult.match(line)
|
|
|
|
if not match:
|
|
|
|
continue
|
2016-10-05 15:42:03 +00:00
|
|
|
|
|
|
|
fullconfig = FullConfig(*(match.get_string(x)
|
|
|
|
for x in FullConfig._fields))
|
|
|
|
if not fullconfig in self.fullconfigs:
|
|
|
|
self.fullconfigs.append(fullconfig)
|
|
|
|
|
|
|
|
for qualifier, value in self.sheet_qualifiers.items():
|
|
|
|
if value is None:
|
|
|
|
self.sheet_qualifiers[qualifier] = match.get_string(qualifier)
|
|
|
|
elif value != match.get_string(qualifier):
|
|
|
|
del self.sheet_qualifiers[qualifier]
|
|
|
|
self.config_qualifiers.add(qualifier)
|
|
|
|
|
|
|
|
self.rows[match.bench][fullconfig] = match.get_string(FLAGS.result)
|
|
|
|
self.cols[fullconfig][match.bench] = getattr(match, FLAGS.result)
|
2016-09-19 18:03:58 +00:00
|
|
|
|
|
|
|
def print_csv(self, outfile=sys.stdout):
|
2016-10-05 15:42:03 +00:00
|
|
|
# Write the title.
|
|
|
|
print(get_qualified_name(FLAGS.result, self.sheet_qualifiers), file=outfile)
|
2016-09-19 18:03:58 +00:00
|
|
|
|
|
|
|
# Write the header.
|
|
|
|
outfile.write('bench,')
|
2016-10-05 15:42:03 +00:00
|
|
|
for fullconfig in self.fullconfigs:
|
|
|
|
outfile.write('%s,' % fullconfig.qualified_name(self.config_qualifiers))
|
2016-09-19 18:03:58 +00:00
|
|
|
outfile.write('\n')
|
|
|
|
|
|
|
|
# Write the rows.
|
2016-10-05 15:42:03 +00:00
|
|
|
for bench, row in self.rows.iteritems():
|
2016-09-19 18:03:58 +00:00
|
|
|
outfile.write('%s,' % bench)
|
2016-10-05 15:42:03 +00:00
|
|
|
for fullconfig in self.fullconfigs:
|
|
|
|
if fullconfig in row:
|
|
|
|
outfile.write('%s,' % row[fullconfig])
|
2016-09-19 18:03:58 +00:00
|
|
|
elif FLAGS.force:
|
2016-10-05 15:42:03 +00:00
|
|
|
outfile.write('NULL,')
|
2016-09-19 18:03:58 +00:00
|
|
|
else:
|
2016-09-22 12:10:02 +00:00
|
|
|
raise ValueError("%s: missing value for %s. (use --force to ignore)" %
|
2016-10-05 15:42:03 +00:00
|
|
|
(bench,
|
|
|
|
fullconfig.qualified_name(self.config_qualifiers)))
|
2016-09-19 18:03:58 +00:00
|
|
|
outfile.write('\n')
|
|
|
|
|
|
|
|
# Add simple, literal averages.
|
|
|
|
if len(self.rows) > 1:
|
|
|
|
outfile.write('\n')
|
2016-10-05 15:42:03 +00:00
|
|
|
self._print_computed_row('MEAN',
|
2016-09-19 18:03:58 +00:00
|
|
|
lambda col: reduce(operator.add, col.values()) / len(col),
|
|
|
|
outfile=outfile)
|
2016-10-05 15:42:03 +00:00
|
|
|
self._print_computed_row('GEOMEAN',
|
2016-09-19 18:03:58 +00:00
|
|
|
lambda col: reduce(operator.mul, col.values()) ** (1.0 / len(col)),
|
|
|
|
outfile=outfile)
|
|
|
|
|
2016-10-05 15:42:03 +00:00
|
|
|
def _print_computed_row(self, name, func, outfile=sys.stdout):
|
2016-09-19 18:03:58 +00:00
|
|
|
outfile.write('%s,' % name)
|
2016-10-05 15:42:03 +00:00
|
|
|
for fullconfig in self.fullconfigs:
|
|
|
|
if len(self.cols[fullconfig]) != len(self.rows):
|
|
|
|
outfile.write('NULL,')
|
|
|
|
continue
|
|
|
|
outfile.write('%.4g,' % func(self.cols[fullconfig]))
|
2016-09-19 18:03:58 +00:00
|
|
|
outfile.write('\n')
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = Parser()
|
|
|
|
|
|
|
|
# Parse the input files.
|
|
|
|
for src in FLAGS.sources:
|
|
|
|
if src == '-':
|
|
|
|
parser.parse_file(sys.stdin)
|
|
|
|
else:
|
|
|
|
with open(src, mode='r') as infile:
|
|
|
|
parser.parse_file(infile)
|
|
|
|
|
|
|
|
# Print the csv.
|
|
|
|
if not FLAGS.open:
|
|
|
|
parser.print_csv()
|
|
|
|
else:
|
|
|
|
dirname = tempfile.mkdtemp()
|
|
|
|
basename = FLAGS.name
|
|
|
|
if os.path.splitext(basename)[1] != '.csv':
|
|
|
|
basename += '.csv';
|
|
|
|
pathname = os.path.join(dirname, basename)
|
|
|
|
with open(pathname, mode='w') as tmpfile:
|
|
|
|
parser.print_csv(outfile=tmpfile)
|
|
|
|
fileuri = urlparse.urljoin('file:', urllib.pathname2url(pathname))
|
|
|
|
print('opening %s' % fileuri)
|
|
|
|
webbrowser.open(fileuri)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|