_benchresult.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. """Parses an skpbench result from a line of output text."""
  6. from __future__ import print_function
  7. import re
  8. import sys
  9. class BenchResult:
  10. FLOAT_REGEX = '[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?'
  11. PATTERN = re.compile('^(?P<accum_pad> *)'
  12. '(?P<accum>' + FLOAT_REGEX + ')'
  13. '(?P<median_pad> +)'
  14. '(?P<median>' + FLOAT_REGEX + ')'
  15. '(?P<max_pad> +)'
  16. '(?P<max>' + FLOAT_REGEX + ')'
  17. '(?P<min_pad> +)'
  18. '(?P<min>' + FLOAT_REGEX + ')'
  19. '(?P<stddev_pad> +)'
  20. '(?P<stddev>' + FLOAT_REGEX + '%)'
  21. '(?P<samples_pad> +)'
  22. '(?P<samples>\d+)'
  23. '(?P<sample_ms_pad> +)'
  24. '(?P<sample_ms>\d+)'
  25. '(?P<clock_pad> +)'
  26. '(?P<clock>[cg]pu)'
  27. '(?P<metric_pad> +)'
  28. '(?P<metric>ms|fps)'
  29. '(?P<config_pad> +)'
  30. '(?P<config>[^\s]+)'
  31. '(?P<bench_pad> +)'
  32. '(?P<bench>[^\s]+)$')
  33. @classmethod
  34. def match(cls, text):
  35. match = cls.PATTERN.search(text)
  36. return cls(match) if match else None
  37. def __init__(self, match):
  38. self.accum = float(match.group('accum'))
  39. self.median = float(match.group('median'))
  40. self.max = float(match.group('max'))
  41. self.min = float(match.group('min'))
  42. self.stddev = float(match.group('stddev')[:-1]) # Drop '%' sign.
  43. self.samples = int(match.group('samples'))
  44. self.sample_ms = int(match.group('sample_ms'))
  45. self.clock = match.group('clock')
  46. self.metric = match.group('metric')
  47. self.config = match.group('config')
  48. self.bench = match.group('bench')
  49. self._match = match
  50. def get_string(self, name):
  51. return self._match.group(name)
  52. def format(self, config_suffix=None):
  53. if not config_suffix or config_suffix == '':
  54. return self._match.group(0)
  55. else:
  56. values = list()
  57. for name in ['accum', 'median', 'max', 'min', 'stddev',
  58. 'samples', 'sample_ms', 'clock', 'metric', 'config']:
  59. values.append(self.get_string(name + '_pad'))
  60. values.append(self.get_string(name))
  61. values.append(config_suffix)
  62. bench_pad = self.get_string('bench_pad')
  63. values.append(bench_pad[min(len(config_suffix), len(bench_pad) - 1):])
  64. values.append(self.get_string('bench'))
  65. return ''.join(values)