Skip to content

Commit d184322

Browse files
committed
Add time summary at the end of the build
1 parent 54c24dc commit d184322

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

utils/build-script

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ from swift_build_support.swift_build_support.toolchain import host_toolchain
4444
from swift_build_support.swift_build_support.utils \
4545
import exit_rejecting_arguments
4646
from swift_build_support.swift_build_support.utils import fatal_error
47+
from swift_build_support.swift_build_support.utils import log_analyzer
4748

4849

4950
# -----------------------------------------------------------------------------
@@ -720,6 +721,9 @@ def main():
720721

721722
if __name__ == "__main__":
722723
try:
723-
sys.exit(main())
724+
exit_code = main()
724725
except KeyboardInterrupt:
725726
sys.exit(1)
727+
finally:
728+
log_analyzer()
729+
sys.exit(exit_code)

utils/swift_build_support/swift_build_support/utils.py

+34
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,37 @@ def log_time(event, command, duration=0):
5757

5858
f.write("{}\n".format(json.dumps(log_event)))
5959
f.close()
60+
61+
62+
def log_analyzer():
63+
"""
64+
Analyze .build_script_log and provide a summary of the time execution.
65+
"""
66+
build_script_log_path = log_time_path()
67+
print("--- Build Script Analyzer ---")
68+
build_events = []
69+
total_duration = 0
70+
if os.path.exists(build_script_log_path):
71+
print("Build Script Log: {}".format(build_script_log_path))
72+
with open(build_script_log_path) as f:
73+
for event in f:
74+
build_event = json.loads(event)
75+
build_event["duration"] = float(build_event["duration"])
76+
total_duration += build_event["duration"]
77+
build_events.append(build_event)
78+
finish_events = [x for x in build_events if x["event"] == "end"]
79+
finish_events.sort(key=lambda x: x["duration"], reverse=True)
80+
81+
print("Build Percentage \t Build Duration (sec) \t Build Phase")
82+
print("================ \t ==================== \t ===========")
83+
event_row = '{:<17.1%} \t {:<21} \t {}'
84+
for build_event in finish_events:
85+
duration_percentage = \
86+
(float(build_event["duration"]) / float(total_duration))
87+
print(event_row.format(duration_percentage,
88+
build_event["duration"],
89+
build_event["command"]))
90+
print("Total Duration: {}".format(total_duration))
91+
else:
92+
print("Skip build script analyzer")
93+
print(".build_script_log file not found at {}".format(build_script_log_path))

0 commit comments

Comments
 (0)