21 |
# $Id$ |
# $Id$ |
22 |
|
|
23 |
import os |
import os |
24 |
|
import StringIO |
25 |
|
import sys |
26 |
import tempfile |
import tempfile |
27 |
import profile |
import profile |
28 |
import time |
import time |
29 |
|
import pstats |
30 |
|
|
31 |
|
from wxPython.lib.dialogs import wxScrolledMessageDialog |
32 |
|
|
33 |
|
from Thuban import _ |
34 |
from Thuban.UI.command import registry, Command |
from Thuban.UI.command import registry, Command |
35 |
import Thuban.UI.mainwindow |
from Thuban.UI.mainwindow import main_menu |
|
menu = Thuban.UI.mainwindow.main_menu.InsertMenu("profiler", "&Profiler") |
|
36 |
|
|
37 |
# |
# |
38 |
# Customization |
# Customization |
48 |
tempfile.mktemp() |
tempfile.mktemp() |
49 |
profile_dir = tempfile.tempdir |
profile_dir = tempfile.tempdir |
50 |
|
|
51 |
|
# Wether to pop up a dialog box with the result. |
52 |
|
popup_dialog_box = True |
53 |
|
|
54 |
|
|
55 |
# |
# |
56 |
# Timing and profiling a complete redraw |
# Timing and profiling a complete redraw |
84 |
how to access the data in the generated .profile file. |
how to access the data in the generated .profile file. |
85 |
""" |
""" |
86 |
print "profiling screen renderer...", |
print "profiling screen renderer...", |
87 |
|
sys.stdout.flush() |
88 |
prof = profile.Profile(bias = profiler_bias) |
prof = profile.Profile(bias = profiler_bias) |
89 |
prof.runctx("do_redraw(context)", globals(), locals()) |
prof.runctx("do_redraw(context)", globals(), locals()) |
90 |
filename = os.path.join(profile_dir, "thuban-render.profile") |
filename = os.path.join(profile_dir, "thuban-render.profile") |
91 |
prof.dump_stats(filename) |
prof.dump_stats(filename) |
92 |
print "done and saved to", filename |
print "done and saved to", filename |
93 |
|
|
94 |
|
if popup_dialog_box: |
95 |
|
# catch the printout to stdout so that we can present the |
96 |
|
# text in a dialog |
97 |
|
f = StringIO.StringIO() |
98 |
|
orig_stdout = sys.stdout |
99 |
|
sys.stdout = f |
100 |
|
try: |
101 |
|
p = pstats.Stats(filename) |
102 |
|
msg = _('These are the statistics sorted by cumulative time:') |
103 |
|
p.strip_dirs().sort_stats('cumulative').print_stats() |
104 |
|
m = f.getvalue() |
105 |
|
msg = '%s\n\n%s' % (msg, m) |
106 |
|
finally: |
107 |
|
sys.stdout = orig_stdout |
108 |
|
|
109 |
|
dlg = wxScrolledMessageDialog(context.mainwindow, msg, |
110 |
|
_('Profile Screen Render')) |
111 |
|
dlg.ShowModal() |
112 |
|
|
113 |
registry.Add(Command("profile_screen_renderer", "Profile Screen Render", |
|
114 |
|
registry.Add(Command("profile_screen_renderer", _('Profile Screen Render'), |
115 |
profile_screen_renderer, |
profile_screen_renderer, |
116 |
helptext = "Profile the screen render")) |
helptext = _('Profile the screen render'))) |
|
menu.InsertItem("profile_screen_renderer") |
|
117 |
|
|
118 |
|
|
119 |
# |
# |
127 |
""" |
""" |
128 |
start = time.clock() |
start = time.clock() |
129 |
do_redraw(context) |
do_redraw(context) |
130 |
print "redraw finished in", time.clock() - start, "s" |
duration = time.clock() - start |
131 |
|
msg = _('Redraw finished in %g seconds.') % duration |
132 |
|
if popup_dialog_box: |
133 |
|
context.mainwindow.RunMessageBox(_('Time Screen Render'), msg) |
134 |
|
else: |
135 |
|
print msg |
136 |
|
|
137 |
|
|
138 |
registry.Add(Command("time_screen_renderer", "Time Screen Render", |
registry.Add(Command("time_screen_renderer", _('Time Screen Render'), |
139 |
time_screen_renderer, |
time_screen_renderer, |
140 |
helptext = "Time the screen render")) |
helptext = _('Time the screen render'))) |
141 |
menu.InsertItem("time_screen_renderer") |
|
142 |
|
# find the extensions menu (create it anew if not found) |
143 |
|
extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions')) |
144 |
|
|
145 |
|
profiler_menu = extensions_menu.InsertMenu("profiler", _('&Profiler')) |
146 |
|
profiler_menu.InsertItem("time_screen_renderer") |
147 |
|
profiler_menu.InsertItem("profile_screen_renderer") |