1 |
bh |
1896 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with the software for details. |
7 |
|
|
|
8 |
|
|
"""Performance Measurement |
9 |
|
|
|
10 |
|
|
This module implements two Thuban commands in a new Profiling menu: |
11 |
|
|
|
12 |
|
|
Profile Screen Render -- Run the screen rendering code in a profile |
13 |
|
|
|
14 |
|
|
Time Screen Render -- Measure the time taken for a complete redraw |
15 |
|
|
|
16 |
|
|
See the individual functions for more details. |
17 |
|
|
""" |
18 |
|
|
|
19 |
|
|
__version__ = "$Revision$" |
20 |
|
|
# $Source$ |
21 |
|
|
# $Id$ |
22 |
|
|
|
23 |
|
|
import os |
24 |
jan |
1901 |
import StringIO |
25 |
|
|
import sys |
26 |
bh |
1896 |
import tempfile |
27 |
|
|
import profile |
28 |
|
|
import time |
29 |
jan |
1901 |
import pstats |
30 |
bh |
1896 |
|
31 |
jan |
1901 |
from wxPython.lib.dialogs import wxScrolledMessageDialog |
32 |
|
|
|
33 |
|
|
from Thuban import _ |
34 |
bh |
1896 |
from Thuban.UI.command import registry, Command |
35 |
jan |
2203 |
from Thuban.UI.mainwindow import main_menu |
36 |
jan |
2357 |
from Thuban.UI.extensionregistry import ExtensionDesc, ext_registry |
37 |
bh |
1896 |
|
38 |
jan |
2357 |
ext_registry.add(ExtensionDesc( |
39 |
|
|
name = 'profiling', |
40 |
|
|
version = '1.0.0', |
41 |
|
|
authors= [ 'Bernhard Herzog' ], |
42 |
|
|
copyright = '2003 Intevation GmbH', |
43 |
|
|
desc = _("Provide a profiler and a timer\n" |
44 |
|
|
"for screen rendering."))) |
45 |
|
|
|
46 |
bh |
1896 |
# |
47 |
|
|
# Customization |
48 |
|
|
# |
49 |
|
|
# Assign to these in your ~/.thuban/thubanstart |
50 |
|
|
|
51 |
|
|
# The machine specific profiler bias. See the standard python profile |
52 |
|
|
# module for details on how to find out which value to use. |
53 |
|
|
profiler_bias = 0 |
54 |
|
|
|
55 |
|
|
# The directory the profile output is to be written to |
56 |
|
|
# (Call mktemp once to initialize tempfile.tempdir) |
57 |
|
|
tempfile.mktemp() |
58 |
|
|
profile_dir = tempfile.tempdir |
59 |
|
|
|
60 |
bh |
1915 |
# Wether to pop up a dialog box with the result. |
61 |
|
|
popup_dialog_box = True |
62 |
bh |
1896 |
|
63 |
bh |
1915 |
|
64 |
bh |
1896 |
# |
65 |
|
|
# Timing and profiling a complete redraw |
66 |
|
|
# |
67 |
|
|
|
68 |
|
|
def do_redraw(context): |
69 |
|
|
"""Perform a complete redraw in the canvas in context""" |
70 |
|
|
canvas = context.mainwindow.canvas |
71 |
|
|
|
72 |
|
|
# Make sure there are no no finished bitmaps and active renderer |
73 |
|
|
canvas.full_redraw() |
74 |
|
|
|
75 |
|
|
# Iterate until all is drawn |
76 |
|
|
for c in canvas._render_iterator(): |
77 |
|
|
pass |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
# |
81 |
|
|
# Profiling the redraw |
82 |
|
|
# |
83 |
|
|
|
84 |
|
|
|
85 |
|
|
def profile_screen_renderer(context): |
86 |
|
|
"""Script to run the redraw in the profiler |
87 |
|
|
|
88 |
|
|
The data gathered by the profiler will be written to |
89 |
|
|
<TMPDIR>/thuban-render.profile (<TMPDIR> is your system specific |
90 |
|
|
temporary directory. |
91 |
|
|
|
92 |
|
|
See the python documentation of the profile and pstats modules for |
93 |
|
|
how to access the data in the generated .profile file. |
94 |
|
|
""" |
95 |
|
|
print "profiling screen renderer...", |
96 |
bh |
1915 |
sys.stdout.flush() |
97 |
bh |
1896 |
prof = profile.Profile(bias = profiler_bias) |
98 |
|
|
prof.runctx("do_redraw(context)", globals(), locals()) |
99 |
|
|
filename = os.path.join(profile_dir, "thuban-render.profile") |
100 |
|
|
prof.dump_stats(filename) |
101 |
|
|
print "done and saved to", filename |
102 |
|
|
|
103 |
bh |
1915 |
if popup_dialog_box: |
104 |
|
|
# catch the printout to stdout so that we can present the |
105 |
|
|
# text in a dialog |
106 |
|
|
f = StringIO.StringIO() |
107 |
|
|
orig_stdout = sys.stdout |
108 |
|
|
sys.stdout = f |
109 |
|
|
try: |
110 |
|
|
p = pstats.Stats(filename) |
111 |
|
|
msg = _('These are the statistics sorted by cumulative time:') |
112 |
|
|
p.strip_dirs().sort_stats('cumulative').print_stats() |
113 |
|
|
m = f.getvalue() |
114 |
|
|
msg = '%s\n\n%s' % (msg, m) |
115 |
|
|
finally: |
116 |
|
|
sys.stdout = orig_stdout |
117 |
bh |
1896 |
|
118 |
bh |
1915 |
dlg = wxScrolledMessageDialog(context.mainwindow, msg, |
119 |
|
|
_('Profile Screen Render')) |
120 |
|
|
dlg.ShowModal() |
121 |
jan |
1901 |
|
122 |
|
|
|
123 |
|
|
registry.Add(Command("profile_screen_renderer", _('Profile Screen Render'), |
124 |
bh |
1896 |
profile_screen_renderer, |
125 |
jan |
1901 |
helptext = _('Profile the screen render'))) |
126 |
bh |
1896 |
|
127 |
|
|
|
128 |
|
|
# |
129 |
|
|
# Timing the redraw |
130 |
|
|
# |
131 |
|
|
|
132 |
|
|
def time_screen_renderer(context): |
133 |
|
|
"""Script to measure the time of a complete redraw. |
134 |
|
|
|
135 |
|
|
The time taken will be printed to stdout. |
136 |
|
|
""" |
137 |
|
|
start = time.clock() |
138 |
|
|
do_redraw(context) |
139 |
jan |
1901 |
duration = time.clock() - start |
140 |
|
|
msg = _('Redraw finished in %g seconds.') % duration |
141 |
bh |
1915 |
if popup_dialog_box: |
142 |
|
|
context.mainwindow.RunMessageBox(_('Time Screen Render'), msg) |
143 |
|
|
else: |
144 |
|
|
print msg |
145 |
bh |
1896 |
|
146 |
|
|
|
147 |
jan |
1901 |
registry.Add(Command("time_screen_renderer", _('Time Screen Render'), |
148 |
bh |
1896 |
time_screen_renderer, |
149 |
jan |
1901 |
helptext = _('Time the screen render'))) |
150 |
bh |
1896 |
|
151 |
jan |
1901 |
# find the extensions menu (create it anew if not found) |
152 |
jan |
2203 |
extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions')) |
153 |
jan |
1901 |
|
154 |
|
|
profiler_menu = extensions_menu.InsertMenu("profiler", _('&Profiler')) |
155 |
|
|
profiler_menu.InsertItem("time_screen_renderer") |
156 |
|
|
profiler_menu.InsertItem("profile_screen_renderer") |