/[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 2357 - (show annotations)
Tue Sep 28 19:54:50 2004 UTC (20 years, 5 months ago) by jan
Original Path: trunk/thuban/Extensions/profiling/profiling.py
File MIME type: text/x-python
File size: 4436 byte(s)
Added registration of the extension.

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 from Thuban.UI.extensionregistry import ExtensionDesc, ext_registry
37
38 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 #
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 # Wether to pop up a dialog box with the result.
61 popup_dialog_box = True
62
63
64 #
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 sys.stdout.flush()
97 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 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
118 dlg = wxScrolledMessageDialog(context.mainwindow, msg,
119 _('Profile Screen Render'))
120 dlg.ShowModal()
121
122
123 registry.Add(Command("profile_screen_renderer", _('Profile Screen Render'),
124 profile_screen_renderer,
125 helptext = _('Profile the screen render')))
126
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 duration = time.clock() - start
140 msg = _('Redraw finished in %g seconds.') % duration
141 if popup_dialog_box:
142 context.mainwindow.RunMessageBox(_('Time Screen Render'), msg)
143 else:
144 print msg
145
146
147 registry.Add(Command("time_screen_renderer", _('Time Screen Render'),
148 time_screen_renderer,
149 helptext = _('Time the screen render')))
150
151 # find the extensions menu (create it anew if not found)
152 extensions_menu = main_menu.FindOrInsertMenu('extensions', _('E&xtensions'))
153
154 profiler_menu = extensions_menu.InsertMenu("profiler", _('&Profiler'))
155 profiler_menu.InsertItem("time_screen_renderer")
156 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