1 |
# Copyright (C) 2003, 2004 by Intevation GmbH |
2 |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> (2003) |
4 |
# Jan-Oliver Wagner <[email protected]> (2003, 2004) |
5 |
# |
6 |
# This program is free software under the GPL (>=v2) |
7 |
# Read the file COPYING coming with the software for details. |
8 |
|
9 |
"""Performance Measurement |
10 |
|
11 |
This module implements two Thuban commands in a new Profiling menu: |
12 |
|
13 |
Profile Screen Render -- Run the screen rendering code in a profile |
14 |
|
15 |
Time Screen Render -- Measure the time taken for a complete redraw |
16 |
|
17 |
See the individual functions for more details. |
18 |
""" |
19 |
|
20 |
__version__ = "$Revision$" |
21 |
# $Source$ |
22 |
# $Id$ |
23 |
|
24 |
import os |
25 |
import StringIO |
26 |
import sys |
27 |
import tempfile |
28 |
import profile |
29 |
import time |
30 |
import pstats |
31 |
|
32 |
from wxPython.lib.dialogs import wxScrolledMessageDialog |
33 |
|
34 |
from Thuban import _ |
35 |
from Thuban.UI.command import registry, Command |
36 |
from Thuban.UI.mainwindow import main_menu |
37 |
|
38 |
# |
39 |
# Customization |
40 |
# |
41 |
# Assign to these in your ~/.thuban/thubanstart |
42 |
|
43 |
# The machine specific profiler bias. See the standard python profile |
44 |
# module for details on how to find out which value to use. |
45 |
profiler_bias = 0 |
46 |
|
47 |
# The directory the profile output is to be written to |
48 |
# (Call mktemp once to initialize tempfile.tempdir) |
49 |
tempfile.mktemp() |
50 |
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 |
58 |
# |
59 |
|
60 |
def do_redraw(context): |
61 |
"""Perform a complete redraw in the canvas in context""" |
62 |
canvas = context.mainwindow.canvas |
63 |
|
64 |
# Make sure there are no no finished bitmaps and active renderer |
65 |
canvas.full_redraw() |
66 |
|
67 |
# Iterate until all is drawn |
68 |
for c in canvas._render_iterator(): |
69 |
pass |
70 |
|
71 |
|
72 |
# |
73 |
# Profiling the redraw |
74 |
# |
75 |
|
76 |
|
77 |
def profile_screen_renderer(context): |
78 |
"""Script to run the redraw in the profiler |
79 |
|
80 |
The data gathered by the profiler will be written to |
81 |
<TMPDIR>/thuban-render.profile (<TMPDIR> is your system specific |
82 |
temporary directory. |
83 |
|
84 |
See the python documentation of the profile and pstats modules for |
85 |
how to access the data in the generated .profile file. |
86 |
""" |
87 |
print "profiling screen renderer...", |
88 |
sys.stdout.flush() |
89 |
prof = profile.Profile(bias = profiler_bias) |
90 |
prof.runctx("do_redraw(context)", globals(), locals()) |
91 |
filename = os.path.join(profile_dir, "thuban-render.profile") |
92 |
prof.dump_stats(filename) |
93 |
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 = wxScrolledMessageDialog(context.mainwindow, msg, |
111 |
_('Profile Screen Render')) |
112 |
dlg.ShowModal() |
113 |
|
114 |
|
115 |
registry.Add(Command("profile_screen_renderer", _('Profile Screen Render'), |
116 |
profile_screen_renderer, |
117 |
helptext = _('Profile the screen render'))) |
118 |
|
119 |
|
120 |
# |
121 |
# Timing the redraw |
122 |
# |
123 |
|
124 |
def time_screen_renderer(context): |
125 |
"""Script to measure the time of a complete redraw. |
126 |
|
127 |
The time taken will be printed to stdout. |
128 |
""" |
129 |
start = time.clock() |
130 |
do_redraw(context) |
131 |
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'), |
140 |
time_screen_renderer, |
141 |
helptext = _('Time the screen render'))) |
142 |
|
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") |