devxoul
11/21/2016 - 8:36 AM

List slow compile functions in Swift

List slow compile functions in Swift

# encoding: utf-8

import os
import re
import sys


def help_exit():
    print 'Usage: python slow.py [threshold-ms] [inputfile]'
    exit(1)

if len(sys.argv) > 1 and sys.argv[1] in ['-h', '--help']:
    help_exit()

if len(sys.argv) != 3:
    help_exit()

try:
    threshold = int(sys.argv[1])
except ValueError:
    print "Error: Threshold must be an integer: '{}'".format(sys.argv[1])
    exit(1)

input = sys.argv[2]
try:
    with open(input, 'r') as f:
        lines = f.readlines()
except IOError:
    print "Error: File doesn\'t exist: '{}'".format(input)
    exit(1)

slow_lines = []  # (duration, line)
pattern = re.compile('(\s*\d+\.\d+)ms.*')

total_duration = 0
pwd = os.path.abspath(os.path.curdir)

for line in lines:
    match = pattern.match(line)
    if match is None:
        continue
    duration = float(match.group(1))
    total_duration += duration
    if duration >= threshold:
        simple_line = line.replace(pwd, '.')
        slow_lines.append((duration, simple_line))

slow_lines.sort(reverse=True)
for duration, line in slow_lines:
    print line.strip()

total_slow_duration = sum([line[0] for line in slow_lines])
print ''
print 'Total Build Time: {}ms'.format(total_duration)
print 'Slow Functions  : {}ms'.format(total_slow_duration)

Slow.py

Usage

$ xcodebuild clean build -workspace 'MyApp.xcworkspace' -scheme 'MyApp' OTHERFLAGS="-Xfrontend -debug-time-function-bodies" | tee xcode_raw.log
$ python slow.py xcode_raw.log
23559.6ms   ./Sources/A.swift:219:16    @objc dynamic func doneButtonDidTap()
23439.6ms   ./Sources/B.swift:763:8  @objc final func checkCondition()
9346.7ms    ./Sources/C.swift:741:8    @objc final func updatePreviewComments()
2652.9ms    ./Sources/D.swift:865:8    @objc final func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
1049.5ms    ./Sources/E.swift:41:8 final func transformToJSON(_ value: UIColor?) -> String?
653.0ms ./Sources/F.swift:49:8  func flush()
Total Build Time: 227042.500002ms
Slow Functions  : 66860.6ms