Merge pull request #15664 from muxi/binary-size

Scripts for iOS size audit
reviewable/pr13368/r10^2
Muxi Yan 7 years ago committed by GitHub
commit 6fcbee0d03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/objective-c/examples/Sample/Sample.xcodeproj/project.pbxproj
  2. 2
      src/objective-c/examples/Sample/Sample.xcodeproj/xcshareddata/xcschemes/Sample.xcscheme
  3. 78
      src/objective-c/tests/analyze_link_map.py
  4. 5
      src/objective-c/tests/build_one_example.sh
  5. 127
      tools/profiling/ios_bin/binary_diff.py
  6. 104
      tools/profiling/ios_bin/parse_link_map.py

@ -325,6 +325,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Sample/Info.plist;
LD_GENERATE_MAP_FILE = YES;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.grpc.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
@ -337,6 +338,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
INFOPLIST_FILE = Sample/Info.plist;
LD_GENERATE_MAP_FILE = YES;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "org.grpc.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";

@ -42,7 +42,7 @@
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"

@ -1,78 +0,0 @@
#!/usr/bin/python
# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script analyzes link map file generated by Xcode. It calculates and
# prints out the sizes of each dependent library and the total sizes of the
# symbols.
# The script takes one parameter, which is the path to the link map file.
import sys
import re
table_tag = {}
state = "start"
table_stats_symbol = {}
table_stats_dead = {}
section_total_size = 0
symbol_total_size = 0
file_import = sys.argv[1]
lines = list(open(file_import))
for line in lines:
line_stripped = line[:-1]
if "# Object files:" == line_stripped:
state = "object"
continue
elif "# Sections:" == line_stripped:
state = "section"
continue
elif "# Symbols:" == line_stripped:
state = "symbol"
continue
elif "# Dead Stripped Symbols:" == line_stripped:
state = "dead"
continue
if state == "object":
segs = re.search('(\[ *[0-9]*\]) (.*)', line_stripped)
table_tag[segs.group(1)] = segs.group(2)
if state == "section":
if len(line_stripped) == 0 or line_stripped[0] == '#':
continue
segs = re.search('^(.+?)\s+(.+?)\s+.*', line_stripped)
section_total_size += int(segs.group(2), 16)
if state == "symbol":
if len(line_stripped) == 0 or line_stripped[0] == '#':
continue
segs = re.search('^.+?\s+(.+?)\s+(\[.+?\]).*', line_stripped)
target = table_tag[segs.group(2)]
target_stripped = re.search('^(.*?)(\(.+?\))?$', target).group(1)
size = int(segs.group(1), 16)
if not target_stripped in table_stats_symbol:
table_stats_symbol[target_stripped] = 0
table_stats_symbol[target_stripped] += size
print("Sections total size: %d" % section_total_size)
for target in table_stats_symbol:
print(target)
print(table_stats_symbol[target])
symbol_total_size += table_stats_symbol[target]
print("Symbols total size: %d" % symbol_total_size)

@ -42,6 +42,9 @@ xcodebuild \
build \
-workspace *.xcworkspace \
-scheme $SCHEME \
-destination name="iPhone 6" \
-destination generic/platform=iOS \
-derivedDataPath Build \
CODE_SIGN_IDENTITY="" \
CODE_SIGNING_REQUIRED=NO \
| egrep -v "$XCODEBUILD_FILTER" \
| egrep -v "^$" -

@ -0,0 +1,127 @@
#!/usr/bin/env python2.7
#
# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import glob
import multiprocessing
import os
import shutil
import subprocess
import sys
from parse_link_map import parse_link_map
sys.path.append(
os.path.join(
os.path.dirname(sys.argv[0]), '..', '..', 'run_tests', 'python_utils'))
import comment_on_pr
size_labels = ('Core', 'ObjC', 'BoringSSL', 'Protobuf', 'Total')
argp = argparse.ArgumentParser(
description='Binary size diff of gRPC Objective-C sample')
argp.add_argument(
'-d',
'--diff_base',
type=str,
help='Commit or branch to compare the current one to')
args = argp.parse_args()
def dir_size(dir):
total = 0
for dirpath, dirnames, filenames in os.walk(dir):
for f in filenames:
fp = os.path.join(dirpath, f)
total += os.stat(fp).st_size
return total
def get_size(where, frameworks):
build_dir = 'src/objective-c/examples/Sample/Build-%s/' % where
if not frameworks:
link_map_filename = 'Build/Intermediates.noindex/Sample.build/Release-iphoneos/Sample.build/Sample-LinkMap-normal-arm64.txt'
return parse_link_map(build_dir + link_map_filename)
else:
framework_dir = 'Build/Products/Release-iphoneos/Sample.app/Frameworks/'
boringssl_size = dir_size(
build_dir + framework_dir + 'openssl.framework')
core_size = dir_size(build_dir + framework_dir + 'grpc.framework')
objc_size = dir_size(build_dir + framework_dir + 'GRPCClient.framework') + \
dir_size(build_dir + framework_dir + 'RxLibrary.framework') + \
dir_size(build_dir + framework_dir + 'ProtoRPC.framework')
protobuf_size = dir_size(
build_dir + framework_dir + 'Protobuf.framework')
app_size = dir_size(
build_dir + 'Build/Products/Release-iphoneos/Sample.app')
return core_size, objc_size, boringssl_size, protobuf_size, app_size
def build(where, frameworks):
shutil.rmtree(
'src/objective-c/examples/Sample/Build-%s' % where, ignore_errors=True)
subprocess.check_call(
'CONFIG=opt EXAMPLE_PATH=src/objective-c/examples/Sample SCHEME=Sample FRAMEWORKS=%s ./build_one_example.sh'
% ('YES' if frameworks else 'NO'),
shell=True,
cwd='src/objective-c/tests')
os.rename('src/objective-c/examples/Sample/Build',
'src/objective-c/examples/Sample/Build-%s' % where)
text = ''
for frameworks in [False, True]:
build('new', frameworks)
new_size = get_size('new', frameworks)
old_size = None
if args.diff_base:
old = 'old'
where_am_i = subprocess.check_output(
['git', 'rev-parse', '--abbrev-ref', 'HEAD']).strip()
subprocess.check_call(['git', 'checkout', '--', '.'])
subprocess.check_call(['git', 'checkout', args.diff_base])
subprocess.check_call(['git', 'submodule', 'update'])
try:
build('old', frameworks)
old_size = get_size('old', frameworks)
finally:
subprocess.check_call(['git', 'checkout', '--', '.'])
subprocess.check_call(['git', 'checkout', where_am_i])
subprocess.check_call(['git', 'submodule', 'update'])
text += ('****************FRAMEWORKS*****************\n'
if frameworks else '******************STATIC*******************\n')
row_format = "{:>10}{:>15}{:>15}" + '\n'
text += row_format.format('New size', '', 'Old size')
for i in range(0, len(size_labels)):
if old_size == None:
diff_sign = ' '
elif new_size[i] == old_size[i]:
diff_sign = ' (=)'
elif new_size[i] > old_size[i]:
diff_sign = ' (>)'
else:
diff_sign = ' (<)'
text += ('\n' if i == len(size_labels) - 1 else '') + row_format.format(
'{:,}'.format(new_size[i]), size_labels[i] + diff_sign,
'{:,}'.format(old_size[i]) if old_size != None else '')
text += '\n'
print text
comment_on_pr.comment_on_pr('```\n%s\n```' % text)

@ -0,0 +1,104 @@
#!/usr/bin/python
# Copyright 2018 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This script analyzes link map file generated by Xcode. It calculates and
# prints out the sizes of each dependent library and the total sizes of the
# symbols.
# The script takes one parameter, which is the path to the link map file.
import sys
import re
def parse_link_map(filename):
table_tag = {}
state = "start"
table_stats_symbol = {}
table_stats_dead = {}
section_total_size = 0
symbol_total_size = 0
boringssl_size = 0
core_size = 0
objc_size = 0
protobuf_size = 0
lines = list(open(filename))
for line in lines:
line_stripped = line[:-1]
if "# Object files:" == line_stripped:
state = "object"
continue
elif "# Sections:" == line_stripped:
state = "section"
continue
elif "# Symbols:" == line_stripped:
state = "symbol"
continue
elif "# Dead Stripped Symbols:" == line_stripped:
state = "dead"
continue
if state == "object":
segs = re.search('(\[ *[0-9]*\]) (.*)', line_stripped)
table_tag[segs.group(1)] = segs.group(2)
if state == "section":
if len(line_stripped) == 0 or line_stripped[0] == '#':
continue
segs = re.search('^(.+?)\s+(.+?)\s+.*', line_stripped)
section_total_size += int(segs.group(2), 16)
if state == "symbol":
if len(line_stripped) == 0 or line_stripped[0] == '#':
continue
segs = re.search('^.+?\s+(.+?)\s+(\[.+?\]).*', line_stripped)
target = table_tag[segs.group(2)]
target_stripped = re.search('^(.*?)(\(.+?\))?$', target).group(1)
size = int(segs.group(1), 16)
if not target_stripped in table_stats_symbol:
table_stats_symbol[target_stripped] = 0
table_stats_symbol[target_stripped] += size
if 'BoringSSL' in target_stripped:
boringssl_size += size
elif 'libgRPC-Core' in target_stripped:
core_size += size
elif 'libgRPC-RxLibrary' in target_stripped or \
'libgRPC' in target_stripped or \
'libgRPC-ProtoLibrary' in target_stripped:
objc_size += size
elif 'libProtobuf' in target_stripped:
protobuf_size += size
for target in table_stats_symbol:
symbol_total_size += table_stats_symbol[target]
return core_size, objc_size, boringssl_size, protobuf_size, symbol_total_size
def main():
filename = sys.argv[1]
core_size, objc_size, boringssl_size, protobuf_size, total_size = parse_link_map(
filename)
print('Core size:{:,}'.format(core_size))
print('ObjC size:{:,}'.format(objc_size))
print('BoringSSL size:{:,}'.format(boringssl_size))
print('Protobuf size:{:,}\n'.format(protobuf_size))
print('Total size:{:,}'.format(total_size))
if __name__ == "__main__":
main()
Loading…
Cancel
Save