# Copyright 2016 Google Inc. # # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Parses an skpbench result from a line of output text.""" from __future__ import print_function import re import sys class BenchResult: FLOAT_REGEX = '[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?' PATTERN = re.compile('^(?P *)' '(?P' + FLOAT_REGEX + ')' '(?P +)' '(?P' + FLOAT_REGEX + ')' '(?P +)' '(?P' + FLOAT_REGEX + ')' '(?P +)' '(?P' + FLOAT_REGEX + ')' '(?P +)' '(?P' + FLOAT_REGEX + '%)' '(?P +)' '(?P\d+)' '(?P +)' '(?P\d+)' '(?P +)' '(?Pms|fps)' '(?P +)' '(?P[^\s]+)' '(?P +)' '(?P[^\s]+)$') @classmethod def match(cls, text): match = cls.PATTERN.search(text) return cls(match) if match else None def __init__(self, match): self.accum = float(match.group('accum')) self.median = float(match.group('median')) self.max = float(match.group('max')) self.min = float(match.group('min')) self.stddev = float(match.group('stddev')[:-1]) # Drop '%' sign. self.samples = int(match.group('samples')) self.sample_ms = int(match.group('sample_ms')) self.metric = match.group('metric') self.config = match.group('config') self.bench = match.group('bench') self._match = match def get_string(self, name): return self._match.group(name) def print_values(self, config_suffix=None, outfile=sys.stdout): if not config_suffix or config_suffix == '': print(self._match.group(0), file=outfile) else: values = list() for name in ['accum', 'median', 'max', 'min', 'stddev', 'samples', 'sample_ms', 'metric', 'config']: values.append(self.get_string(name + '_pad')) values.append(self.get_string(name)) values.append(config_suffix) bench_pad = self.get_string('bench_pad') values.append(bench_pad[min(len(config_suffix), len(bench_pad) - 1):]) values.append(self.get_string('bench')) print(''.join(values), file=outfile)