csmartdalton | 4b5179b | 2016-09-19 11:03:58 -0700 | [diff] [blame] | 1 | # Copyright 2016 Google Inc. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | '''Parses an skpbench result from a line of output text.''' |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | import re |
| 10 | import sys |
| 11 | |
| 12 | class BenchResult: |
| 13 | FLOAT_REGEX = '[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?' |
| 14 | PATTERN = re.compile('^(?P<median_pad> *)' |
| 15 | '(?P<median>' + FLOAT_REGEX + ')' |
| 16 | '(?P<accum_pad> +)' |
| 17 | '(?P<accum>' + FLOAT_REGEX + ')' |
| 18 | '(?P<max_pad> +)' |
| 19 | '(?P<max>' + FLOAT_REGEX + ')' |
| 20 | '(?P<min_pad> +)' |
| 21 | '(?P<min>' + FLOAT_REGEX + ')' |
| 22 | '(?P<stddev_pad> +)' |
| 23 | '(?P<stddev>' + FLOAT_REGEX + '%)' |
| 24 | '(?P<metric_pad> +)' |
| 25 | '(?P<metric>ms|fps)' |
| 26 | '(?P<samples_pad> +)' |
| 27 | '(?P<samples>\d+)' |
| 28 | '(?P<sample_ms_pad> +)' |
| 29 | '(?P<sample_ms>\d+)' |
| 30 | '(?P<config_pad> +)' |
| 31 | '(?P<config>[^\s]+)' |
| 32 | '(?P<bench_pad> +)' |
| 33 | '(?P<bench>[^\s]+)$') |
| 34 | |
| 35 | @classmethod |
| 36 | def match(cls, text): |
| 37 | match = cls.PATTERN.search(text) |
| 38 | return cls(match) if match else None |
| 39 | |
| 40 | def __init__(self, match): |
| 41 | self.median = float(match.group('median')) |
| 42 | self.accum = float(match.group('accum')) |
| 43 | self.max = float(match.group('max')) |
| 44 | self.min = float(match.group('min')) |
| 45 | self.stddev = float(match.group('stddev')[:-1]) # Drop '%' sign. |
| 46 | self.metric = match.group('metric') |
| 47 | self.samples = int(match.group('samples')) |
| 48 | self.sample_ms = int(match.group('sample_ms')) |
| 49 | self.config = match.group('config') |
| 50 | self.bench = match.group('bench') |
| 51 | self._match = match |
| 52 | |
| 53 | def get_string(self, name): |
| 54 | return self._match.group(name) |
| 55 | |
| 56 | def print_values(self, config_suffix=None, outfile=sys.stdout): |
| 57 | if not config_suffix or config_suffix == '': |
| 58 | print(self._match.group(0), file=outfile) |
| 59 | else: |
| 60 | values = list() |
| 61 | for name in ['median', 'accum', 'max', 'min', 'stddev', |
| 62 | 'metric', 'samples', 'sample_ms', 'config']: |
| 63 | values.append(self.get_string(name + '_pad')) |
| 64 | values.append(self.get_string(name)) |
| 65 | values.append(config_suffix) |
| 66 | bench_pad = self.get_string('bench_pad') |
| 67 | values.append(bench_pad[min(len(config_suffix), len(bench_pad) - 1):]) |
| 68 | values.append(self.get_string('bench')) |
| 69 | print(''.join(values), file=outfile) |