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