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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:39