binary_size.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2018 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. import argparse
  17. import glob
  18. import multiprocessing
  19. import os
  20. import shutil
  21. import subprocess
  22. import sys
  23. from parse_link_map import parse_link_map
  24. sys.path.append(
  25. os.path.join(os.path.dirname(sys.argv[0]), '..', '..', 'run_tests',
  26. 'python_utils'))
  27. import check_on_pr
  28. # Only show diff 1KB or greater
  29. diff_threshold = 1000
  30. size_labels = ('Core', 'ObjC', 'BoringSSL', 'Protobuf', 'Total')
  31. argp = argparse.ArgumentParser(
  32. description='Binary size diff of gRPC Objective-C sample')
  33. argp.add_argument('-d',
  34. '--diff_base',
  35. type=str,
  36. help='Commit or branch to compare the current one to')
  37. args = argp.parse_args()
  38. def dir_size(dir):
  39. total = 0
  40. for dirpath, dirnames, filenames in os.walk(dir):
  41. for f in filenames:
  42. fp = os.path.join(dirpath, f)
  43. total += os.stat(fp).st_size
  44. return total
  45. def get_size(where, frameworks):
  46. build_dir = 'src/objective-c/examples/Sample/Build/Build-%s/' % where
  47. if not frameworks:
  48. link_map_filename = 'Build/Intermediates.noindex/Sample.build/Release-iphoneos/Sample.build/Sample-LinkMap-normal-arm64.txt'
  49. return parse_link_map(build_dir + link_map_filename)
  50. else:
  51. framework_dir = 'Build/Products/Release-iphoneos/Sample.app/Frameworks/'
  52. boringssl_size = dir_size(build_dir + framework_dir +
  53. 'openssl.framework')
  54. core_size = dir_size(build_dir + framework_dir + 'grpc.framework')
  55. objc_size = dir_size(build_dir + framework_dir + 'GRPCClient.framework') + \
  56. dir_size(build_dir + framework_dir + 'RxLibrary.framework') + \
  57. dir_size(build_dir + framework_dir + 'ProtoRPC.framework')
  58. protobuf_size = dir_size(build_dir + framework_dir +
  59. 'Protobuf.framework')
  60. app_size = dir_size(build_dir +
  61. 'Build/Products/Release-iphoneos/Sample.app')
  62. return core_size, objc_size, boringssl_size, protobuf_size, app_size
  63. def build(where, frameworks):
  64. subprocess.check_call(['make', 'clean'])
  65. shutil.rmtree('src/objective-c/examples/Sample/Build/Build-%s' % where,
  66. ignore_errors=True)
  67. subprocess.check_call(
  68. 'CONFIG=opt EXAMPLE_PATH=src/objective-c/examples/Sample SCHEME=Sample FRAMEWORKS=%s ./build_one_example.sh'
  69. % ('YES' if frameworks else 'NO'),
  70. shell=True,
  71. cwd='src/objective-c/tests')
  72. os.rename('src/objective-c/examples/Sample/Build/Build',
  73. 'src/objective-c/examples/Sample/Build/Build-%s' % where)
  74. text = 'Objective-C binary sizes\n'
  75. for frameworks in [False, True]:
  76. build('new', frameworks)
  77. new_size = get_size('new', frameworks)
  78. old_size = None
  79. if args.diff_base:
  80. old = 'old'
  81. where_am_i = subprocess.check_output(
  82. ['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip()
  83. subprocess.check_call(['git', 'checkout', '--', '.'])
  84. subprocess.check_call(['git', 'checkout', args.diff_base])
  85. subprocess.check_call(['git', 'submodule', 'update', '--force'])
  86. try:
  87. build('old', frameworks)
  88. old_size = get_size('old', frameworks)
  89. finally:
  90. subprocess.check_call(['git', 'checkout', '--', '.'])
  91. subprocess.check_call(['git', 'checkout', where_am_i])
  92. subprocess.check_call(['git', 'submodule', 'update', '--force'])
  93. text += ('***************FRAMEWORKS****************\n'
  94. if frameworks else '*****************STATIC******************\n')
  95. row_format = "{:>10}{:>15}{:>15}" + '\n'
  96. text += row_format.format('New size', '', 'Old size')
  97. if old_size == None:
  98. for i in range(0, len(size_labels)):
  99. text += ('\n' if i == len(size_labels) -
  100. 1 else '') + row_format.format('{:,}'.format(new_size[i]),
  101. size_labels[i], '')
  102. else:
  103. has_diff = False
  104. for i in range(0, len(size_labels) - 1):
  105. if abs(new_size[i] - old_size[i]) < diff_threshold:
  106. continue
  107. if new_size[i] > old_size[i]:
  108. diff_sign = ' (>)'
  109. else:
  110. diff_sign = ' (<)'
  111. has_diff = True
  112. text += row_format.format('{:,}'.format(new_size[i]),
  113. size_labels[i] + diff_sign,
  114. '{:,}'.format(old_size[i]))
  115. i = len(size_labels) - 1
  116. if new_size[i] > old_size[i]:
  117. diff_sign = ' (>)'
  118. elif new_size[i] < old_size[i]:
  119. diff_sign = ' (<)'
  120. else:
  121. diff_sign = ' (=)'
  122. text += ('\n' if has_diff else '') + row_format.format(
  123. '{:,}'.format(new_size[i]), size_labels[i] + diff_sign,
  124. '{:,}'.format(old_size[i]))
  125. if not has_diff:
  126. text += '\n No significant differences in binary sizes\n'
  127. text += '\n'
  128. print(text)
  129. check_on_pr.check_on_pr('Binary Size', '```\n%s\n```' % text)