Skip to content

Commit e44288a

Browse files
shahmishalahoppen
authored andcommitted
Add time summary at the end of the build
1 parent 99fefc3 commit e44288a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

utils/build-script

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ from swift_build_support.swift_build_support.toolchain import host_toolchain
4040
from swift_build_support.swift_build_support.utils \
4141
import exit_rejecting_arguments
4242
from swift_build_support.swift_build_support.utils import fatal_error
43+
from swift_build_support.swift_build_support.utils import log_analyzer
4344

4445

4546
# -----------------------------------------------------------------------------
@@ -719,6 +720,9 @@ def main():
719720

720721
if __name__ == "__main__":
721722
try:
722-
sys.exit(main())
723+
exit_code = main()
723724
except KeyboardInterrupt:
724725
sys.exit(1)
726+
finally:
727+
log_analyzer()
728+
sys.exit(exit_code)

utils/swift_build_support/swift_build_support/utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,37 @@ def log_time(event, command, duration=0):
5555

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

0 commit comments

Comments
 (0)