Last active
November 30, 2016 23:21
-
-
Save boompig/c085b52b34901d9a1793a6005df4128a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import sys | |
def skip(fp): | |
for i, line in enumerate(fp): | |
if line.endswith("clearing screen\n"): | |
return i | |
raise ValueError("Error: skipped whole file") | |
def print_profile(time_by_file, occurrences_by_file): | |
# sort by time, descending | |
for fname, time in sorted(time_by_file.iteritems(), key=lambda pair: -1 * pair[1]): | |
print("%s -> %.3f ms (%d occurrences)" % (fname, time, occurrences_by_file[fname])) | |
def parse_line(sline, line_num): | |
""" | |
:param sline: Stripped line | |
:param line_num: The line number (0-indexed)""" | |
components = sline.split(": ") | |
times = components[0].split(" ") | |
if len(times) == 2: | |
# this is an action | |
return (None, None) | |
elif len(times) == 3: | |
self_time = float(times[-1]) | |
source_file = components[-1].replace("sourcing ", "", 1) | |
return (source_file, self_time) | |
else: | |
raise ValueError("Failed to parse line %d: '%s'" % (line_num + 1, sline)) | |
def profile(fname): | |
time_by_file = {} | |
occurrences_by_file = {} | |
with open(fname) as fp: | |
n = skip(fp) | |
print("skipped a few lines!") | |
for i, line in enumerate(fp): | |
# strip trailing newline | |
source_file, source_time = parse_line(line[:-1], n + i) | |
if source_file: | |
if source_file not in time_by_file: | |
time_by_file[source_file] = 0.0 | |
occurrences_by_file[source_file] = 0 | |
time_by_file[source_file] += source_time | |
occurrences_by_file[source_file] += 1 | |
print_profile(time_by_file, occurrences_by_file) | |
if __name__ == "__main__": | |
fname = sys.argv[1] | |
print("parsing %s" % fname) | |
profile(fname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment