bm_build.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 build opt and counters benchmarks """
  17. import argparse
  18. import multiprocessing
  19. import os
  20. import shutil
  21. import subprocess
  22. import bm_constants
  23. def _args():
  24. argp = argparse.ArgumentParser(description='Builds microbenchmarks')
  25. argp.add_argument('-b',
  26. '--benchmarks',
  27. nargs='+',
  28. choices=bm_constants._AVAILABLE_BENCHMARK_TESTS,
  29. default=bm_constants._AVAILABLE_BENCHMARK_TESTS,
  30. help='Which benchmarks to build')
  31. argp.add_argument(
  32. '-j',
  33. '--jobs',
  34. type=int,
  35. default=multiprocessing.cpu_count(),
  36. help=
  37. 'Deprecated. Bazel chooses number of CPUs to build with automatically.')
  38. argp.add_argument(
  39. '-n',
  40. '--name',
  41. type=str,
  42. help=
  43. 'Unique name of this build. To be used as a handle to pass to the other bm* scripts'
  44. )
  45. argp.add_argument('--counters', dest='counters', action='store_true')
  46. argp.add_argument('--no-counters', dest='counters', action='store_false')
  47. argp.set_defaults(counters=True)
  48. args = argp.parse_args()
  49. assert args.name
  50. return args
  51. def _build_cmd(cfg, benchmarks):
  52. bazel_targets = [
  53. '//test/cpp/microbenchmarks:%s' % benchmark for benchmark in benchmarks
  54. ]
  55. # --dynamic_mode=off makes sure that we get a monolithic binary that can be safely
  56. # moved outside of the bazel-bin directory
  57. return ['tools/bazel', 'build',
  58. '--config=%s' % cfg, '--dynamic_mode=off'] + bazel_targets
  59. def _build_config_and_copy(cfg, benchmarks, dest_dir):
  60. """Build given config and copy resulting binaries to dest_dir/CONFIG"""
  61. subprocess.check_call(_build_cmd(cfg, benchmarks))
  62. cfg_dir = dest_dir + '/%s' % cfg
  63. os.makedirs(cfg_dir)
  64. subprocess.check_call(['cp'] + [
  65. 'bazel-bin/test/cpp/microbenchmarks/%s' % benchmark
  66. for benchmark in benchmarks
  67. ] + [cfg_dir])
  68. def build(name, benchmarks, jobs, counters):
  69. dest_dir = 'bm_diff_%s' % name
  70. shutil.rmtree(dest_dir, ignore_errors=True)
  71. _build_config_and_copy('opt', benchmarks, dest_dir)
  72. if counters:
  73. _build_config_and_copy('counters', benchmarks, dest_dir)
  74. if __name__ == '__main__':
  75. args = _args()
  76. build(args.name, args.benchmarks, args.jobs, args.counters)