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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1896 by bh, Fri Oct 31 11:55:33 2003 UTC revision 2357 by jan, Tue Sep 28 19:54:50 2004 UTC
# Line 21  __version__ = "$Revision$" Line 21  __version__ = "$Revision$"
21  # $Id$  # $Id$
22    
23  import os  import os
24    import StringIO
25    import sys
26  import tempfile  import tempfile
27  import profile  import profile
28  import time  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  from Thuban.UI.command import registry, Command
35  import Thuban.UI.mainwindow  from Thuban.UI.mainwindow import main_menu
36  menu = Thuban.UI.mainwindow.main_menu.InsertMenu("profiler", "&Profiler")  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  # Customization
# Line 43  profiler_bias = 0 Line 57  profiler_bias = 0
57  tempfile.mktemp()  tempfile.mktemp()
58  profile_dir = tempfile.tempdir  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  #       Timing and profiling a complete redraw
# Line 76  def profile_screen_renderer(context): Line 93  def profile_screen_renderer(context):
93      how to access the data in the generated .profile file.      how to access the data in the generated .profile file.
94      """      """
95      print "profiling screen renderer...",      print "profiling screen renderer...",
96        sys.stdout.flush()
97      prof = profile.Profile(bias = profiler_bias)      prof = profile.Profile(bias = profiler_bias)
98      prof.runctx("do_redraw(context)", globals(), locals())      prof.runctx("do_redraw(context)", globals(), locals())
99      filename = os.path.join(profile_dir, "thuban-render.profile")      filename = os.path.join(profile_dir, "thuban-render.profile")
100      prof.dump_stats(filename)      prof.dump_stats(filename)
101      print "done and saved to", filename      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  registry.Add(Command("profile_screen_renderer", "Profile Screen Render",  
123    registry.Add(Command("profile_screen_renderer", _('Profile Screen Render'),
124                       profile_screen_renderer,                       profile_screen_renderer,
125                       helptext = "Profile the screen render"))                       helptext = _('Profile the screen render')))
 menu.InsertItem("profile_screen_renderer")  
126    
127    
128  #  #
# Line 100  def time_screen_renderer(context): Line 136  def time_screen_renderer(context):
136      """      """
137      start = time.clock()      start = time.clock()
138      do_redraw(context)      do_redraw(context)
139      print "redraw finished in", time.clock() - start, "s"      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",  registry.Add(Command("time_screen_renderer", _('Time Screen Render'),
148                       time_screen_renderer,                       time_screen_renderer,
149                       helptext = "Time the screen render"))                       helptext = _('Time the screen render')))
150  menu.InsertItem("time_screen_renderer")  
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")

Legend:
Removed from v.1896  
changed lines
  Added in v.2357

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26