bm_run.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2017 gRPC authors.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """ Python utility to run opt and counters benchmarks and save json output """
  17. import argparse
  18. import itertools
  19. import multiprocessing
  20. import os
  21. import random
  22. import subprocess
  23. import sys
  24. import bm_constants
  25. import jobset
  26. sys.path.append(
  27. os.path.join(os.path.dirname(sys.argv[0]), '..', '..', '..', 'run_tests',
  28. 'python_utils'))
  29. def _args():
  30. argp = argparse.ArgumentParser(description='Runs microbenchmarks')
  31. argp.add_argument('-b',
  32. '--benchmarks',
  33. nargs='+',
  34. choices=bm_constants._AVAILABLE_BENCHMARK_TESTS,
  35. default=bm_constants._AVAILABLE_BENCHMARK_TESTS,
  36. help='Benchmarks to run')
  37. argp.add_argument('-j',
  38. '--jobs',
  39. type=int,
  40. default=multiprocessing.cpu_count(),
  41. help='Number of CPUs to use')
  42. argp.add_argument(
  43. '-n',
  44. '--name',
  45. type=str,
  46. help=
  47. 'Unique name of the build to run. Needs to match the handle passed to bm_build.py'
  48. )
  49. argp.add_argument('-r',
  50. '--regex',
  51. type=str,
  52. default="",
  53. help='Regex to filter benchmarks run')
  54. argp.add_argument(
  55. '-l',
  56. '--loops',
  57. type=int,
  58. default=20,
  59. help=
  60. 'Number of times to loops the benchmarks. More loops cuts down on noise'
  61. )
  62. argp.add_argument('--counters', dest='counters', action='store_true')
  63. argp.add_argument('--no-counters', dest='counters', action='store_false')
  64. argp.set_defaults(counters=True)
  65. args = argp.parse_args()
  66. assert args.name
  67. if args.loops < 3:
  68. print("WARNING: This run will likely be noisy. Increase loops to at "
  69. "least 3.")
  70. return args
  71. def _collect_bm_data(bm, cfg, name, regex, idx, loops):
  72. jobs_list = []
  73. for line in subprocess.check_output([
  74. 'bm_diff_%s/%s/%s' % (name, cfg, bm), '--benchmark_list_tests',
  75. '--benchmark_filter=%s' % regex
  76. ]).splitlines():
  77. line = line.decode('UTF-8')
  78. stripped_line = line.strip().replace("/",
  79. "_").replace("<", "_").replace(
  80. ">", "_").replace(", ", "_")
  81. cmd = [
  82. 'bm_diff_%s/%s/%s' % (name, cfg, bm),
  83. '--benchmark_filter=^%s$' % line,
  84. '--benchmark_out=%s.%s.%s.%s.%d.json' %
  85. (bm, stripped_line, cfg, name, idx),
  86. '--benchmark_out_format=json',
  87. ]
  88. jobs_list.append(
  89. jobset.JobSpec(cmd,
  90. shortname='%s %s %s %s %d/%d' %
  91. (bm, line, cfg, name, idx + 1, loops),
  92. verbose_success=True,
  93. cpu_cost=2,
  94. timeout_seconds=60 * 60)) # one hour
  95. return jobs_list
  96. def create_jobs(name, benchmarks, loops, regex, counters):
  97. jobs_list = []
  98. for loop in range(0, loops):
  99. for bm in benchmarks:
  100. jobs_list += _collect_bm_data(bm, 'opt', name, regex, loop, loops)
  101. if counters:
  102. jobs_list += _collect_bm_data(bm, 'counters', name, regex, loop,
  103. loops)
  104. random.shuffle(jobs_list, random.SystemRandom().random)
  105. return jobs_list
  106. if __name__ == '__main__':
  107. args = _args()
  108. jobs_list = create_jobs(args.name, args.benchmarks, args.loops, args.regex,
  109. args.counters)
  110. jobset.run(jobs_list, maxjobs=args.jobs)