michael@0: #!/usr/bin/python michael@0: import re, sys michael@0: michael@0: interesting_re = re.compile("(js_Execute|CallHook) ([^ ]+) ([^ ]+ )?([^ ]+ms)") michael@0: class Entry: michael@0: def __init__(self, kind, depth, file, linenum, func, timetaken): michael@0: self.kind = kind michael@0: self.depth = depth michael@0: self.file = file michael@0: self.linenum = linenum michael@0: self.func = func michael@0: self.timetaken = timetaken michael@0: self.calls = 0 michael@0: self.duration = 0 michael@0: michael@0: def __str__(self): michael@0: return " ".join(map(str,[self.kind, self.depth, self.file, self.linenum, self.func, self.timetaken])) michael@0: michael@0: def id(self): michael@0: if self.kind == "js_Execute": michael@0: return self.file michael@0: else: michael@0: if self.file and self.linenum: michael@0: strout = "%s:%d" % (self.file, self.linenum) michael@0: if self.func: michael@0: strout = "%s %s" % (self.func, strout) michael@0: return strout michael@0: elif self.func: michael@0: return self.func michael@0: else: michael@0: print("No clue what my id is:"+self) michael@0: michael@0: def call(self, timetaken): michael@0: self.calls += 1 michael@0: self.duration += timetaken michael@0: michael@0: def parse_line(line): michael@0: m = interesting_re.search(line) michael@0: if not m: michael@0: return None michael@0: michael@0: ms_index = line.find("ms") michael@0: depth = m.start() - ms_index - 3 michael@0: kind = m.group(1) michael@0: func = None michael@0: file = None michael@0: linenum = None michael@0: if kind == "CallHook": michael@0: func = m.group(2) michael@0: file = m.group(3) michael@0: colpos = file.rfind(":") michael@0: (file,linenum) = file[:colpos], file[colpos+1:-1] michael@0: if linenum == "0": michael@0: linenum = None michael@0: else: michael@0: linenum = int(linenum) michael@0: offset = 1 michael@0: else: michael@0: file = m.group(3) michael@0: michael@0: timetaken = None michael@0: try: michael@0: timetaken = float(m.group(4)[:-2]) michael@0: except: michael@0: return None michael@0: return Entry(kind, depth, file, linenum, func, timetaken) michael@0: michael@0: def compare(x,y): michael@0: diff = x[1].calls - y[1].calls michael@0: if diff == 0: michael@0: return int(x[1].duration - y[1].duration) michael@0: elif diff > 0: michael@0: return 1 michael@0: elif diff < 0: michael@0: return -1 michael@0: michael@0: def frequency(ls): michael@0: dict = {} michael@0: for item in ls: michael@0: id = item.id() michael@0: stat = None michael@0: if not id in dict: michael@0: stat = dict[id] = item michael@0: else: michael@0: stat = dict[id] michael@0: stat.call(item.timetaken) michael@0: michael@0: ls = dict.items() michael@0: ls.sort(compare) michael@0: ls = filter(lambda (_,item): item.duration > 20, ls) michael@0: # ls = filter(lambda (_,item): item.file and item.file.find("browser.js") != -1 and item.linenum <= 1223 and item.linenum >1067, ls) michael@0: for key, item in ls: michael@0: print(item.calls,key, str(item.duration)+"ms") michael@0: michael@0: def go(): michael@0: file = sys.argv[1] michael@0: michael@0: ls = filter(lambda x: x != None, map(parse_line, open(file).readlines())) michael@0: michael@0: frequency(ls) michael@0: print ls[0] michael@0: michael@0: go() michael@0: