2
0

simplebench.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python
  2. #
  3. # Simple benchmarking framework
  4. #
  5. # Copyright (c) 2019 Virtuozzo International GmbH.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. def bench_one(test_func, test_env, test_case, count=5, initial_run=True):
  21. """Benchmark one test-case
  22. test_func -- benchmarking function with prototype
  23. test_func(env, case), which takes test_env and test_case
  24. arguments and returns {'seconds': int} (which is benchmark
  25. result) on success and {'error': str} on error. Returned
  26. dict may contain any other additional fields.
  27. test_env -- test environment - opaque first argument for test_func
  28. test_case -- test case - opaque second argument for test_func
  29. count -- how many times to call test_func, to calculate average
  30. initial_run -- do initial run of test_func, which don't get into result
  31. Returns dict with the following fields:
  32. 'runs': list of test_func results
  33. 'average': average seconds per run (exists only if at least one run
  34. succeeded)
  35. 'delta': maximum delta between test_func result and the average
  36. (exists only if at least one run succeeded)
  37. 'n-failed': number of failed runs (exists only if at least one run
  38. failed)
  39. """
  40. if initial_run:
  41. print(' #initial run:')
  42. print(' ', test_func(test_env, test_case))
  43. runs = []
  44. for i in range(count):
  45. print(' #run {}'.format(i+1))
  46. res = test_func(test_env, test_case)
  47. print(' ', res)
  48. runs.append(res)
  49. result = {'runs': runs}
  50. successed = [r for r in runs if ('seconds' in r)]
  51. if successed:
  52. avg = sum(r['seconds'] for r in successed) / len(successed)
  53. result['average'] = avg
  54. result['delta'] = max(abs(r['seconds'] - avg) for r in successed)
  55. if len(successed) < count:
  56. result['n-failed'] = count - len(successed)
  57. return result
  58. def ascii_one(result):
  59. """Return ASCII representation of bench_one() returned dict."""
  60. if 'average' in result:
  61. s = '{:.2f} +- {:.2f}'.format(result['average'], result['delta'])
  62. if 'n-failed' in result:
  63. s += '\n({} failed)'.format(result['n-failed'])
  64. return s
  65. else:
  66. return 'FAILED'
  67. def bench(test_func, test_envs, test_cases, *args, **vargs):
  68. """Fill benchmark table
  69. test_func -- benchmarking function, see bench_one for description
  70. test_envs -- list of test environments, see bench_one
  71. test_cases -- list of test cases, see bench_one
  72. args, vargs -- additional arguments for bench_one
  73. Returns dict with the following fields:
  74. 'envs': test_envs
  75. 'cases': test_cases
  76. 'tab': filled 2D array, where cell [i][j] is bench_one result for
  77. test_cases[i] for test_envs[j] (i.e., rows are test cases and
  78. columns are test environments)
  79. """
  80. tab = {}
  81. results = {
  82. 'envs': test_envs,
  83. 'cases': test_cases,
  84. 'tab': tab
  85. }
  86. n = 1
  87. n_tests = len(test_envs) * len(test_cases)
  88. for env in test_envs:
  89. for case in test_cases:
  90. print('Testing {}/{}: {} :: {}'.format(n, n_tests,
  91. env['id'], case['id']))
  92. if case['id'] not in tab:
  93. tab[case['id']] = {}
  94. tab[case['id']][env['id']] = bench_one(test_func, env, case,
  95. *args, **vargs)
  96. n += 1
  97. print('Done')
  98. return results
  99. def ascii(results):
  100. """Return ASCII representation of bench() returned dict."""
  101. from tabulate import tabulate
  102. tab = [[""] + [c['id'] for c in results['envs']]]
  103. for case in results['cases']:
  104. row = [case['id']]
  105. for env in results['envs']:
  106. row.append(ascii_one(results['tab'][case['id']][env['id']]))
  107. tab.append(row)
  108. return tabulate(tab)