• 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(
30        '-b',
31        '--benchmarks',
32        nargs='+',
33        choices=bm_constants._AVAILABLE_BENCHMARK_TESTS,
34        default=bm_constants._AVAILABLE_BENCHMARK_TESTS,
35        help='Which benchmarks to build')
36    argp.add_argument(
37        '-j',
38        '--jobs',
39        type=int,
40        default=multiprocessing.cpu_count(),
41        help='How many CPUs to dedicate to this task')
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
57def _make_cmd(cfg, benchmarks, jobs):
58    return ['make'] + benchmarks + ['CONFIG=%s' % cfg, '-j', '%d' % jobs]
59
60
61def build(name, benchmarks, jobs, counters):
62    shutil.rmtree('bm_diff_%s' % name, ignore_errors=True)
63    subprocess.check_call(['git', 'submodule', 'update'])
64    try:
65        subprocess.check_call(_make_cmd('opt', benchmarks, jobs))
66        if counters:
67            subprocess.check_call(_make_cmd('counters', benchmarks, jobs))
68    except subprocess.CalledProcessError, e:
69        subprocess.check_call(['make', 'clean'])
70        subprocess.check_call(_make_cmd('opt', benchmarks, jobs))
71        if counters:
72            subprocess.check_call(_make_cmd('counters', benchmarks, jobs))
73    os.rename(
74        'bins',
75        'bm_diff_%s' % name,
76    )
77
78
79if __name__ == '__main__':
80    args = _args()
81    build(args.name, args.benchmarks, args.jobs, args.counters)
82