1 |
# 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 |
import StringIO |
25 |
import sys |
26 |
import tempfile |
27 |
import profile |
28 |
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 |
35 |
import Thuban.UI.mainwindow |
36 |
|
37 |
# |
38 |
# Customization |
39 |
# |
40 |
# Assign to these in your ~/.thuban/thubanstart |
41 |
|
42 |
# The machine specific profiler bias. See the standard python profile |
43 |
# module for details on how to find out which value to use. |
44 |
profiler_bias = 0 |
45 |
|
46 |
# The directory the profile output is to be written to |
47 |
# (Call mktemp once to initialize tempfile.tempdir) |
48 |
tempfile.mktemp() |
49 |
profile_dir = tempfile.tempdir |
50 |
|
51 |
|
52 |
# |
53 |
# Timing and profiling a complete redraw |
54 |
# |
55 |
|
56 |
def do_redraw(context): |
57 |
"""Perform a complete redraw in the canvas in context""" |
58 |
canvas = context.mainwindow.canvas |
59 |
|
60 |
# Make sure there are no no finished bitmaps and active renderer |
61 |
canvas.full_redraw() |
62 |
|
63 |
# Iterate until all is drawn |
64 |
for c in canvas._render_iterator(): |
65 |
pass |
66 |
|
67 |
|
68 |
# |
69 |
# Profiling the redraw |
70 |
# |
71 |
|
72 |
|
73 |
def profile_screen_renderer(context): |
74 |
"""Script to run the redraw in the profiler |
75 |
|
76 |
The data gathered by the profiler will be written to |
77 |
<TMPDIR>/thuban-render.profile (<TMPDIR> is your system specific |
78 |
temporary directory. |
79 |
|
80 |
See the python documentation of the profile and pstats modules for |
81 |
how to access the data in the generated .profile file. |
82 |
""" |
83 |
print "profiling screen renderer...", |
84 |
prof = profile.Profile(bias = profiler_bias) |
85 |
prof.runctx("do_redraw(context)", globals(), locals()) |
86 |
filename = os.path.join(profile_dir, "thuban-render.profile") |
87 |
prof.dump_stats(filename) |
88 |
print "done and saved to", filename |
89 |
|
90 |
# catch the printout to stdout so that we can present the |
91 |
# text in a dialog |
92 |
f = StringIO.StringIO() |
93 |
orig_stdout = sys.stdout |
94 |
sys.stdout = f |
95 |
try: |
96 |
p = pstats.Stats(filename) |
97 |
msg = _('These are the statistics sorted by cumulative time:') |
98 |
p.strip_dirs().sort_stats('cumulative').print_stats() |
99 |
m = f.getvalue() |
100 |
msg = '%s\n\n%s' % (msg, m) |
101 |
finally: |
102 |
sys.stdout = orig_stdout |
103 |
|
104 |
dlg = wxScrolledMessageDialog(context.mainwindow, msg, |
105 |
_('Profile Screen Render')) |
106 |
dlg.ShowModal() |
107 |
|
108 |
|
109 |
registry.Add(Command("profile_screen_renderer", _('Profile Screen Render'), |
110 |
profile_screen_renderer, |
111 |
helptext = _('Profile the screen render'))) |
112 |
|
113 |
|
114 |
# |
115 |
# Timing the redraw |
116 |
# |
117 |
|
118 |
def time_screen_renderer(context): |
119 |
"""Script to measure the time of a complete redraw. |
120 |
|
121 |
The time taken will be printed to stdout. |
122 |
""" |
123 |
start = time.clock() |
124 |
do_redraw(context) |
125 |
duration = time.clock() - start |
126 |
msg = _('Redraw finished in %g seconds.') % duration |
127 |
context.mainwindow.RunMessageBox(_('Time Screen Render'), msg) |
128 |
|
129 |
|
130 |
registry.Add(Command("time_screen_renderer", _('Time Screen Render'), |
131 |
time_screen_renderer, |
132 |
helptext = _('Time the screen render'))) |
133 |
|
134 |
|
135 |
# find the extensions menu (create it anew if not found) |
136 |
main_menu = Thuban.UI.mainwindow.main_menu |
137 |
extensions_menu = main_menu.find_menu('extensions') |
138 |
if extensions_menu is None: |
139 |
extensions_menu = main_menu.InsertMenu('extensions', _('E&xtensions')) |
140 |
|
141 |
profiler_menu = extensions_menu.InsertMenu("profiler", _('&Profiler')) |
142 |
profiler_menu.InsertItem("time_screen_renderer") |
143 |
profiler_menu.InsertItem("profile_screen_renderer") |