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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/tests/fennec-tile-testapp/logread.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +#!/usr/bin/python
     1.5 +import re, sys
     1.6 +
     1.7 +interesting_re = re.compile("(js_Execute|CallHook) ([^ ]+) ([^ ]+ )?([^ ]+ms)")
     1.8 +class Entry:
     1.9 +    def __init__(self, kind, depth, file, linenum, func, timetaken):
    1.10 +        self.kind = kind
    1.11 +        self.depth = depth
    1.12 +        self.file = file
    1.13 +        self.linenum = linenum
    1.14 +        self.func = func
    1.15 +        self.timetaken = timetaken
    1.16 +        self.calls = 0
    1.17 +        self.duration = 0
    1.18 +
    1.19 +    def __str__(self):
    1.20 +        return " ".join(map(str,[self.kind, self.depth, self.file, self.linenum, self.func, self.timetaken]))
    1.21 +    
    1.22 +    def id(self):
    1.23 +        if self.kind == "js_Execute":
    1.24 +            return self.file
    1.25 +        else:
    1.26 +            if self.file and self.linenum:
    1.27 +                strout = "%s:%d" % (self.file, self.linenum)
    1.28 +                if self.func:
    1.29 +                    strout = "%s %s" % (self.func, strout)
    1.30 +                return strout
    1.31 +            elif self.func:
    1.32 +                return self.func
    1.33 +            else:
    1.34 +                print("No clue what my id is:"+self)
    1.35 +           
    1.36 +    def call(self, timetaken):
    1.37 +        self.calls += 1
    1.38 +        self.duration += timetaken
    1.39 +
    1.40 +def parse_line(line):
    1.41 +    m = interesting_re.search(line)
    1.42 +    if not m:
    1.43 +        return None
    1.44 +
    1.45 +    ms_index = line.find("ms")
    1.46 +    depth = m.start() - ms_index - 3
    1.47 +    kind = m.group(1)
    1.48 +    func = None
    1.49 +    file = None
    1.50 +    linenum = None
    1.51 +    if kind == "CallHook":
    1.52 +        func = m.group(2)
    1.53 +        file = m.group(3)
    1.54 +        colpos = file.rfind(":")
    1.55 +        (file,linenum) = file[:colpos], file[colpos+1:-1]
    1.56 +        if linenum == "0":
    1.57 +            linenum = None
    1.58 +        else:
    1.59 +            linenum = int(linenum)
    1.60 +        offset = 1
    1.61 +    else:
    1.62 +        file = m.group(3)
    1.63 +    
    1.64 +    timetaken = None
    1.65 +    try:
    1.66 +        timetaken = float(m.group(4)[:-2])
    1.67 +    except:
    1.68 +        return None
    1.69 +    return Entry(kind, depth, file, linenum, func, timetaken)
    1.70 +
    1.71 +def compare(x,y):
    1.72 +    diff = x[1].calls - y[1].calls
    1.73 +    if diff == 0:
    1.74 +        return int(x[1].duration - y[1].duration)
    1.75 +    elif diff > 0:
    1.76 +        return 1
    1.77 +    elif diff < 0:
    1.78 +        return -1
    1.79 +
    1.80 +def frequency(ls):
    1.81 +    dict = {}
    1.82 +    for item in ls:
    1.83 +        id = item.id()
    1.84 +        stat = None
    1.85 +        if not id in dict:
    1.86 +            stat = dict[id] = item
    1.87 +        else:
    1.88 +            stat = dict[id]
    1.89 +        stat.call(item.timetaken)
    1.90 +
    1.91 +    ls = dict.items()
    1.92 +    ls.sort(compare)
    1.93 +    ls = filter(lambda (_,item): item.duration > 20, ls)
    1.94 +#    ls = filter(lambda (_,item): item.file and item.file.find("browser.js") != -1 and item.linenum <= 1223 and item.linenum >1067, ls)
    1.95 +    for key, item in ls:
    1.96 +        print(item.calls,key, str(item.duration)+"ms")
    1.97 +
    1.98 +def go():
    1.99 +    file = sys.argv[1]
   1.100 +    
   1.101 +    ls = filter(lambda x: x != None, map(parse_line, open(file).readlines()))
   1.102 +    
   1.103 +    frequency(ls)
   1.104 +    print ls[0]
   1.105 +
   1.106 +go()
   1.107 +

mercurial