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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1915 - (hide annotations)
Mon Nov 3 14:19:40 2003 UTC (21 years, 4 months ago) by bh
Original Path: trunk/thuban/Extensions/profiling/profiling.py
File MIME type: text/x-python
File size: 4231 byte(s)
(popup_dialog_box): New config
variable to indicate whether the result should be shown in a
dialog box
(profile_screen_renderer, time_screen_renderer): Only show a
dialog box if popup_dialog_box is true.
(profile_screen_renderer): Flush stdout after the printing the
first part of the "profiling..." message

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     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 bh 1915 # Wether to pop up a dialog box with the result.
52     popup_dialog_box = True
53 bh 1896
54 bh 1915
55 bh 1896 #
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 bh 1915 sys.stdout.flush()
88 bh 1896 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 bh 1915 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 bh 1896
109 bh 1915 dlg = wxScrolledMessageDialog(context.mainwindow, msg,
110     _('Profile Screen Render'))
111     dlg.ShowModal()
112 jan 1901
113    
114     registry.Add(Command("profile_screen_renderer", _('Profile Screen Render'),
115 bh 1896 profile_screen_renderer,
116 jan 1901 helptext = _('Profile the screen render')))
117 bh 1896
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 jan 1901 duration = time.clock() - start
131     msg = _('Redraw finished in %g seconds.') % duration
132 bh 1915 if popup_dialog_box:
133     context.mainwindow.RunMessageBox(_('Time Screen Render'), msg)
134     else:
135     print msg
136 bh 1896
137    
138 jan 1901 registry.Add(Command("time_screen_renderer", _('Time Screen Render'),
139 bh 1896 time_screen_renderer,
140 jan 1901 helptext = _('Time the screen render')))
141 bh 1896
142 jan 1901
143     # find the extensions menu (create it anew if not found)
144     main_menu = Thuban.UI.mainwindow.main_menu
145     extensions_menu = main_menu.find_menu('extensions')
146     if extensions_menu is None:
147     extensions_menu = main_menu.InsertMenu('extensions', _('E&xtensions'))
148    
149     profiler_menu = extensions_menu.InsertMenu("profiler", _('&Profiler'))
150     profiler_menu.InsertItem("time_screen_renderer")
151     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