Debian Codegraph @

tools/profiling/microbenchmarks/bm_diff/bm_build.py

raw
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 args = argp.parse_args()
50 assert args.name
51 return args
52
53
54 def _build_cmd(cfg, benchmarks):
55 bazel_targets = [
56 '//test/cpp/microbenchmarks:%s' % benchmark for benchmark in benchmarks
57 ]
58 # --dynamic_mode=off makes sure that we get a monolithic binary that can be safely
59 # moved outside of the bazel-bin directory
60 return ['tools/bazel', 'build',
61 '--config=%s' % cfg, '--dynamic_mode=off'] + bazel_targets
62
63
64 def _build_config_and_copy(cfg, benchmarks, dest_dir):
65 """Build given config and copy resulting binaries to dest_dir/CONFIG"""
66 subprocess.check_call(_build_cmd(cfg, benchmarks))
67 cfg_dir = dest_dir + '/%s' % cfg
68 os.makedirs(cfg_dir)
69 subprocess.check_call(['cp'] + [
70 'bazel-bin/test/cpp/microbenchmarks/%s' % benchmark
71 for benchmark in benchmarks
72 ] + [cfg_dir])
73
74
75 def build(name, benchmarks, jobs):
76 dest_dir = 'bm_diff_%s' % name
77 shutil.rmtree(dest_dir, ignore_errors=True)
78 _build_config_and_copy('opt', benchmarks, dest_dir)
79
80
81 if __name__ == '__main__':
82 args = _args()
83 build(args.name, args.benchmarks, args.jobs)