/[thuban]/branches/WIP-pyshapelib-bramz/Extensions/profiling/profiling.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Extensions/profiling/profiling.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2203 - (show annotations)
Tue May 11 22:34:49 2004 UTC (20 years, 9 months ago) by jan
Original Path: trunk/thuban/Extensions/profiling/profiling.py
File MIME type: text/x-python
File size: 4124 byte(s)
Use FindOrInsertMenu() instead of finding menu on its own.

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 from Thuban.UI.mainwindow import main_menu
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 # 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
57 #
58
59 def do_redraw(context):
60 """Perform a complete redraw in the canvas in context"""
61 canvas = context.mainwindow.canvas
62
63 # Make sure there are no no finished bitmaps and active renderer
64 canvas.full_redraw()
65
66 # Iterate until all is drawn
67 for c in canvas._render_iterator():
68 pass
69
70
71 #
72 # Profiling the redraw
73 #
74
75
76 def profile_screen_renderer(context):
77 """Script to run the redraw in the profiler
78
79 The data gathered by the profiler will be written to
80 <TMPDIR>/thuban-render.profile (<TMPDIR> is your system specific
81 temporary directory.
82
83 See the python documentation of the profile and pstats modules for
84 how to access the data in the generated .profile file.
85 """
86 print "profiling screen renderer...",
87 sys.stdout.flush()
88 prof = profile.Profile(bias = profiler_bias)
89 prof.runctx("do_redraw(context)", globals(), locals())
90 filename = os.path.join(profile_dir, "thuban-render.profile")
91 prof.dump_stats(filename)
92 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
114 registry.Add(Command("profile_screen_renderer", _('Profile Screen Render'),
115 profile_screen_renderer,
116 helptext = _('Profile the screen render')))
117
118
119 #
120 # Timing the redraw
121 #
122
123 def time_screen_renderer(context):
124 """Script to measure the time of a complete redraw.
125
126 The time taken will be printed to stdout.
127 """
128 start = time.clock()
129 do_redraw(context)
130 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'),
139 time_screen_renderer,
140 helptext = _('Time the screen render')))
141
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")

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26