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
17import argparse
18import glob
19import multiprocessing
20import os
21import shutil
22import subprocess
23import sys
24
25sys.path.append(
26    os.path.join(
27        os.path.dirname(sys.argv[0]), '..', '..', 'run_tests', 'python_utils'))
28import comment_on_pr
29
30argp = argparse.ArgumentParser(description='Perform diff on microbenchmarks')
31
32argp.add_argument(
33    '-d',
34    '--diff_base',
35    type=str,
36    help='Commit or branch to compare the current one to')
37
38argp.add_argument('-j', '--jobs', type=int, default=multiprocessing.cpu_count())
39
40args = argp.parse_args()
41
42LIBS = [
43    'libgrpc.so',
44    'libgrpc++.so',
45]
46
47
48def build(where):
49    subprocess.check_call('make -j%d' % args.jobs, shell=True, cwd='.')
50    shutil.rmtree('bloat_diff_%s' % where, ignore_errors=True)
51    os.rename('libs', 'bloat_diff_%s' % where)
52
53
54build('new')
55
56if args.diff_base:
57    old = 'old'
58    where_am_i = subprocess.check_output(
59        ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip()
60    subprocess.check_call(['git', 'checkout', args.diff_base])
61    subprocess.check_call(['git', 'submodule', 'update'])
62    try:
63        try:
64            build('old')
65        except subprocess.CalledProcessError, e:
66            subprocess.check_call(['make', 'clean'])
67            build('old')
68    finally:
69        subprocess.check_call(['git', 'checkout', where_am_i])
70        subprocess.check_call(['git', 'submodule', 'update'])
71
72subprocess.check_call(
73    'make -j%d' % args.jobs, shell=True, cwd='third_party/bloaty')
74
75text = ''
76for lib in LIBS:
77    text += '****************************************************************\n\n'
78    text += lib + '\n\n'
79    old_version = glob.glob('bloat_diff_old/opt/%s' % lib)
80    new_version = glob.glob('bloat_diff_new/opt/%s' % lib)
81    assert len(new_version) == 1
82    cmd = 'third_party/bloaty/bloaty -d compileunits,symbols'
83    if old_version:
84        assert len(old_version) == 1
85        text += subprocess.check_output(
86            '%s %s -- %s' % (cmd, new_version[0], old_version[0]), shell=True)
87    else:
88        text += subprocess.check_output(
89            '%s %s' % (cmd, new_version[0]), shell=True)
90    text += '\n\n'
91
92print text
93comment_on_pr.comment_on_pr('```\n%s\n```' % text)
94