• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python2.7
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
18import bm_constants
19
20import argparse
21import subprocess
22import multiprocessing
23import os
24import shutil
25
26
27def _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('-j',
36                      '--jobs',
37                      type=int,
38                      default=multiprocessing.cpu_count(),
39                      help='How many CPUs to dedicate to this task')
40    argp.add_argument(
41        '-n',
42        '--name',
43        type=str,
44        help=
45        'Unique name of this build. To be used as a handle to pass to the other bm* scripts'
46    )
47    argp.add_argument('--counters', dest='counters', action='store_true')
48    argp.add_argument('--no-counters', dest='counters', action='store_false')
49    argp.set_defaults(counters=True)
50    args = argp.parse_args()
51    assert args.name
52    return args
53
54
55def _make_cmd(cfg, benchmarks, jobs):
56    return ['make'] + benchmarks + ['CONFIG=%s' % cfg, '-j', '%d' % jobs]
57
58
59def build(name, benchmarks, jobs, counters):
60    shutil.rmtree('bm_diff_%s' % name, ignore_errors=True)
61    subprocess.check_call(['git', 'submodule', 'update'])
62    try:
63        subprocess.check_call(_make_cmd('opt', benchmarks, jobs))
64        if counters:
65            subprocess.check_call(_make_cmd('counters', benchmarks, jobs))
66    except subprocess.CalledProcessError, e:
67        subprocess.check_call(['make', 'clean'])
68        subprocess.check_call(_make_cmd('opt', benchmarks, jobs))
69        if counters:
70            subprocess.check_call(_make_cmd('counters', benchmarks, jobs))
71    os.rename(
72        'bins',
73        'bm_diff_%s' % name,
74    )
75
76
77if __name__ == '__main__':
78    args = _args()
79    build(args.name, args.benchmarks, args.jobs, args.counters)
80