author | Sverre Rabbelier <srabbelier@gmail.com> |
Sat, 12 Sep 2009 14:38:42 +0200 | |
changeset 2905 | 378833eb5a95 |
parent 2857 | bc793800116e |
permissions | -rw-r--r-- |
2832 | 1 |
import pstats |
2 |
import cPickle |
|
3 |
import zlib |
|
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
4 |
import re |
2832 | 5 |
|
6 |
class PickleStats(object): |
|
7 |
def __init__(self, stats): |
|
8 |
self.stats = stats |
|
9 |
||
10 |
def create_stats(self): |
|
11 |
"pstats.Stats checks for the existence of this method to see if it can load from an object" |
|
12 |
pass |
|
13 |
||
14 |
def from_file(fileobj): |
|
15 |
"load ppstats from an open file object" |
|
16 |
stats = cPickle.load(fileobj) |
|
17 |
ps = PickleStats(stats) |
|
18 |
return Stats(ps) |
|
19 |
||
20 |
def from_filename(filename): |
|
21 |
"load ppstats from a filename" |
|
22 |
fileobj = open(filename, 'rb') |
|
23 |
return from_file(fileobj) |
|
24 |
||
25 |
def from_gz_file(fileobj): |
|
26 |
"load ppstats from an open file containing gzipped data" |
|
27 |
data = fileobj.read() |
|
28 |
stats = cPickle.loads(zlib.decompress(data)) |
|
29 |
ps = PickleStats(stats) |
|
30 |
return Stats(ps) |
|
31 |
||
32 |
def from_gz_filename(filename): |
|
33 |
"load ppstats from a file containing gzipped data, by filename" |
|
34 |
fileobj = open(filename, 'rb') |
|
35 |
return from_gz_file(fileobj) |
|
36 |
||
37 |
def from_gz(gz_string): |
|
38 |
"load ppstats from a string of gzipped data" |
|
39 |
return Stats(PickleStats(cPickle.loads(zlib.decompress(gz_string)))) |
|
40 |
||
41 |
def from_stats(stats): |
|
42 |
"load ppstats from a stats object" |
|
43 |
return Stats(PickleStats(stats)) |
|
44 |
||
45 |
def from_string(stats_string): |
|
46 |
return Stats(PickleStats(cPickle.loads(stats_string))) |
|
47 |
||
48 |
class Stats(pstats.Stats): |
|
49 |
def __init__(self, *args, **kwargs): |
|
50 |
pstats.Stats.__init__(self, *args) |
|
51 |
self.replace_dirs = {} |
|
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
52 |
self.replace_regexes = {} |
2832 | 53 |
|
54 |
def set_output(self, stream): |
|
55 |
"redirect output of print_stats to the file object <stream>" |
|
56 |
self.stream = stream |
|
57 |
||
58 |
def hide_directory(self, dirname, replacement=''): |
|
59 |
"replace occurences of <dirname> in filenames with <replacement>" |
|
60 |
self.replace_dirs[dirname] = replacement |
|
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
61 |
|
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
62 |
def hide_regex(self, pattern, replacement=''): |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
63 |
"call re.sub(pattern, replacement) on each filename" |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
64 |
self.replace_regexes[pattern] = replacement |
2832 | 65 |
|
66 |
def func_strip_path(self, func_name): |
|
67 |
"take a filename, line, name tuple and mangle appropiately" |
|
68 |
filename, line, name = func_name |
|
69 |
||
70 |
for dirname in self.replace_dirs: |
|
71 |
filename = filename.replace(dirname, self.replace_dirs[dirname]) |
|
72 |
||
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
73 |
for pattern in self.replace_regexes: |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
74 |
filename = re.sub(pattern, self.replace_regexes[pattern], filename) |
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
75 |
|
2832 | 76 |
return filename, line, name |
77 |
||
78 |
def strip_dirs(self): |
|
79 |
"strip irrelevant/redundant directories from filenames in profile data" |
|
80 |
func_std_string = pstats.func_std_string |
|
2857
bc793800116e
Updated to r11 of app_profiler
Sverre Rabbelier <srabbelier@gmail.com>
parents:
2832
diff
changeset
|
81 |
add_func_stats = pstats.add_func_stats |
2832 | 82 |
|
83 |
oldstats = self.stats |
|
84 |
self.stats = newstats = {} |
|
85 |
max_name_len = 0 |
|
86 |
for func, (cc, nc, tt, ct, callers) in oldstats.iteritems(): |
|
87 |
newfunc = self.func_strip_path(func) |
|
88 |
if len(func_std_string(newfunc)) > max_name_len: |
|
89 |
max_name_len = len(func_std_string(newfunc)) |
|
90 |
newcallers = {} |
|
91 |
for func2, caller in callers.iteritems(): |
|
92 |
newcallers[self.func_strip_path(func2)] = caller |
|
93 |
||
94 |
if newfunc in newstats: |
|
95 |
newstats[newfunc] = add_func_stats( |
|
96 |
newstats[newfunc], |
|
97 |
(cc, nc, tt, ct, newcallers)) |
|
98 |
else: |
|
99 |
newstats[newfunc] = (cc, nc, tt, ct, newcallers) |
|
100 |
old_top = self.top_level |
|
101 |
self.top_level = new_top = {} |
|
102 |
for func in old_top: |
|
103 |
new_top[self.func_strip_path(func)] = None |
|
104 |
||
105 |
self.max_name_len = max_name_len |
|
106 |
||
107 |
self.fcn_list = None |
|
108 |
self.all_callees = None |
|
109 |
return self |
|
110 |
||
111 |
def dump_stats_pickle(self): |
|
112 |
"return a string containing picked stats information (dump_stats uses marshall)" |
|
113 |
return cPickle.dumps(self.stats) |
|
114 |
||
115 |
def load_stats_pickle(self, pickle_string): |
|
116 |
"load from string returned by dump_stats_pickle" |
|
117 |
return self.load_stats(PickleStats(cPickle.load(pickle_string))) |