PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/toolkit/content/tests/fennec-tile-testapp/logread.py

http://github.com/zpao/v8monkey
Python | 104 lines | 87 code | 15 blank | 2 comment | 16 complexity | 7ed39c39d12799e8eb14dd1784962ee6 MD5 | raw file
  1#!/usr/bin/python
  2import re, sys
  3
  4interesting_re = re.compile("(js_Execute|CallHook) ([^ ]+) ([^ ]+ )?([^ ]+ms)")
  5class Entry:
  6    def __init__(self, kind, depth, file, linenum, func, timetaken):
  7        self.kind = kind
  8        self.depth = depth
  9        self.file = file
 10        self.linenum = linenum
 11        self.func = func
 12        self.timetaken = timetaken
 13        self.calls = 0
 14        self.duration = 0
 15
 16    def __str__(self):
 17        return " ".join(map(str,[self.kind, self.depth, self.file, self.linenum, self.func, self.timetaken]))
 18    
 19    def id(self):
 20        if self.kind == "js_Execute":
 21            return self.file
 22        else:
 23            if self.file and self.linenum:
 24                strout = "%s:%d" % (self.file, self.linenum)
 25                if self.func:
 26                    strout = "%s %s" % (self.func, strout)
 27                return strout
 28            elif self.func:
 29                return self.func
 30            else:
 31                print("No clue what my id is:"+self)
 32           
 33    def call(self, timetaken):
 34        self.calls += 1
 35        self.duration += timetaken
 36
 37def parse_line(line):
 38    m = interesting_re.search(line)
 39    if not m:
 40        return None
 41
 42    ms_index = line.find("ms")
 43    depth = m.start() - ms_index - 3
 44    kind = m.group(1)
 45    func = None
 46    file = None
 47    linenum = None
 48    if kind == "CallHook":
 49        func = m.group(2)
 50        file = m.group(3)
 51        colpos = file.rfind(":")
 52        (file,linenum) = file[:colpos], file[colpos+1:-1]
 53        if linenum == "0":
 54            linenum = None
 55        else:
 56            linenum = int(linenum)
 57        offset = 1
 58    else:
 59        file = m.group(3)
 60    
 61    timetaken = None
 62    try:
 63        timetaken = float(m.group(4)[:-2])
 64    except:
 65        return None
 66    return Entry(kind, depth, file, linenum, func, timetaken)
 67
 68def compare(x,y):
 69    diff = x[1].calls - y[1].calls
 70    if diff == 0:
 71        return int(x[1].duration - y[1].duration)
 72    elif diff > 0:
 73        return 1
 74    elif diff < 0:
 75        return -1
 76
 77def frequency(ls):
 78    dict = {}
 79    for item in ls:
 80        id = item.id()
 81        stat = None
 82        if not id in dict:
 83            stat = dict[id] = item
 84        else:
 85            stat = dict[id]
 86        stat.call(item.timetaken)
 87
 88    ls = dict.items()
 89    ls.sort(compare)
 90    ls = filter(lambda (_,item): item.duration > 20, ls)
 91#    ls = filter(lambda (_,item): item.file and item.file.find("browser.js") != -1 and item.linenum <= 1223 and item.linenum >1067, ls)
 92    for key, item in ls:
 93        print(item.calls,key, str(item.duration)+"ms")
 94
 95def go():
 96    file = sys.argv[1]
 97    
 98    ls = filter(lambda x: x != None, map(parse_line, open(file).readlines()))
 99    
100    frequency(ls)
101    print ls[0]
102
103go()
104