blob: 28d3aeaf4b591dfd3243d6b4b5eccaa0eeec042e [file] [log] [blame]
csmartdalton4b5179b2016-09-19 11:03:58 -07001# 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
csmartdaltonbf41fa82016-09-23 11:36:11 -07006"""Parses an skpbench result from a line of output text."""
csmartdalton4b5179b2016-09-19 11:03:58 -07007
8from __future__ import print_function
9import re
10import sys
11
12class BenchResult:
13 FLOAT_REGEX = '[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?'
csmartdalton6904b192016-09-29 06:23:23 -070014 PATTERN = re.compile('^(?P<accum_pad> *)'
csmartdalton4b5179b2016-09-19 11:03:58 -070015 '(?P<accum>' + FLOAT_REGEX + ')'
csmartdalton6904b192016-09-29 06:23:23 -070016 '(?P<median_pad> +)'
17 '(?P<median>' + FLOAT_REGEX + ')'
csmartdalton4b5179b2016-09-19 11:03:58 -070018 '(?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 + '%)'
csmartdalton4b5179b2016-09-19 11:03:58 -070024 '(?P<samples_pad> +)'
25 '(?P<samples>\d+)'
26 '(?P<sample_ms_pad> +)'
27 '(?P<sample_ms>\d+)'
csmartdaltonc6618dd2016-10-05 08:42:03 -070028 '(?P<clock_pad> +)'
29 '(?P<clock>[cg]pu)'
csmartdalton6904b192016-09-29 06:23:23 -070030 '(?P<metric_pad> +)'
31 '(?P<metric>ms|fps)'
csmartdalton4b5179b2016-09-19 11:03:58 -070032 '(?P<config_pad> +)'
33 '(?P<config>[^\s]+)'
34 '(?P<bench_pad> +)'
35 '(?P<bench>[^\s]+)$')
36
37 @classmethod
38 def match(cls, text):
39 match = cls.PATTERN.search(text)
40 return cls(match) if match else None
41
42 def __init__(self, match):
csmartdalton4b5179b2016-09-19 11:03:58 -070043 self.accum = float(match.group('accum'))
csmartdalton6904b192016-09-29 06:23:23 -070044 self.median = float(match.group('median'))
csmartdalton4b5179b2016-09-19 11:03:58 -070045 self.max = float(match.group('max'))
46 self.min = float(match.group('min'))
47 self.stddev = float(match.group('stddev')[:-1]) # Drop '%' sign.
csmartdalton4b5179b2016-09-19 11:03:58 -070048 self.samples = int(match.group('samples'))
49 self.sample_ms = int(match.group('sample_ms'))
csmartdaltonc6618dd2016-10-05 08:42:03 -070050 self.clock = match.group('clock')
csmartdalton6904b192016-09-29 06:23:23 -070051 self.metric = match.group('metric')
csmartdalton4b5179b2016-09-19 11:03:58 -070052 self.config = match.group('config')
53 self.bench = match.group('bench')
54 self._match = match
55
56 def get_string(self, name):
57 return self._match.group(name)
58
csmartdalton49df7702016-11-10 10:36:28 -050059 def format(self, config_suffix=None):
csmartdalton4b5179b2016-09-19 11:03:58 -070060 if not config_suffix or config_suffix == '':
csmartdalton49df7702016-11-10 10:36:28 -050061 return self._match.group(0)
csmartdalton4b5179b2016-09-19 11:03:58 -070062 else:
63 values = list()
csmartdalton6904b192016-09-29 06:23:23 -070064 for name in ['accum', 'median', 'max', 'min', 'stddev',
csmartdaltonc6618dd2016-10-05 08:42:03 -070065 'samples', 'sample_ms', 'clock', 'metric', 'config']:
csmartdalton4b5179b2016-09-19 11:03:58 -070066 values.append(self.get_string(name + '_pad'))
67 values.append(self.get_string(name))
68 values.append(config_suffix)
69 bench_pad = self.get_string('bench_pad')
70 values.append(bench_pad[min(len(config_suffix), len(bench_pad) - 1):])
71 values.append(self.get_string('bench'))
csmartdalton49df7702016-11-10 10:36:28 -050072 return ''.join(values)