bbangert /
dozer
WSGI middleware fork of Robert Brewer's Dowser
Clone URL : http://bitbucket.org/bbangert/dozer/ (size: 146.1 KB)
| commit 26: | f571108578b5 |
| parent 24: | 9c7f31a852ba |
| branch: | trunk |
Adding graphviz file format
Changed (Δ1.4 KB):
raw changeset »
dozer/profile.py (42 lines added, 2 lines removed)
Up to file-list dozer/profile.py:
| … | … | @@ -106,10 +106,12 @@ class Profiler(object): |
106 |
106 |
safe_environ[k] = v |
107 |
107 |
profile_run = dict(time=datetime.now(), profile=tree, |
108 |
108 |
environ=safe_environ) |
109 |
fname |
|
109 |
fname_base = str(time.time()).replace('.', '_') |
|
110 |
prof_file = fname_base + '.pkl' |
|
110 |
111 |
|
111 |
112 |
dir_name = self.profile_path or '' |
112 |
cPickle.dump(profile_run, open(os.path.join(dir_name, |
|
113 |
cPickle.dump(profile_run, open(os.path.join(dir_name, prof_file), 'wb')) |
|
114 |
write_dot_graph(results, tree, os.path.join(dir_name, fname_base+'.gv')) |
|
113 |
115 |
del results, tree, profile_run |
114 |
116 |
return [body] |
115 |
117 |
|
| … | … | @@ -122,6 +124,11 @@ def label(code): |
122 |
124 |
return '%s %s:%d' % (code.co_name, |
123 |
125 |
code.co_filename, |
124 |
126 |
code.co_firstlineno) |
127 |
||
128 |
||
129 |
def graphlabel(code): |
|
130 |
lb = label(code) |
|
131 |
return lb.replace('"', "'").strip() |
|
125 |
132 |
|
126 |
133 |
|
127 |
134 |
def setup_time(t): |
| … | … | @@ -138,6 +145,39 @@ def setup_time(t): |
138 |
145 |
t = t*1000 |
139 |
146 |
t = '%0.2f' % t |
140 |
147 |
return t |
148 |
||
149 |
def write_dot_graph(data, tree, filename): |
|
150 |
f = open(filename, 'w') |
|
151 |
f.write('digraph prof {\n') |
|
152 |
f.write('\tsize="11,9"; ratio = fill;\n') |
|
153 |
f.write('\tnode [style=filled];\n') |
|
154 |
||
155 |
# Find the largest time |
|
156 |
highest = 0.00 |
|
157 |
for entry in tree.values(): |
|
158 |
if float(entry['cost']) > highest: |
|
159 |
highest = float(entry['cost']) |
|
160 |
||
161 |
for entry in data: |
|
162 |
code = entry.code |
|
163 |
entry_name = graphlabel(code) |
|
164 |
if isinstance(code, str) or setup_time(entry.totaltime) == '0.00': |
|
165 |
continue |
|
166 |
code = code.replace("\n",'').strip() |
|
167 |
f.write('\t"%s" [label="%s\\n%sms"]\n' % (entry_name, code)) |
|
168 |
else: |
|
169 |
t = tree[label(code)]['cost'] |
|
170 |
f.write('\t"%s" [label="%s\\n%sms"]\n' % (entry_name, code.co_name, t)) |
|
171 |
if entry.calls: |
|
172 |
for subentry in entry.calls: |
|
173 |
subcode = subentry.code |
|
174 |
if isinstance(subcode, str) or setup_time(subentry.totaltime) == '0.00': |
|
175 |
continue |
|
176 |
sub_name = graphlabel(subcode) |
|
177 |
f.write('\t"%s" -> "%s"\n' % (entry_name, sub_name)) |
|
178 |
f.write('}\n') |
|
179 |
f.close() |
|
180 |
||
141 |
181 |
|
142 |
182 |
def buildtree(data): |
143 |
183 |
"""Takes a pmstats object as returned by cProfile and constructs |
