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 tempfile |
25 |
import profile |
26 |
import time |
27 |
|
28 |
from Thuban.UI.command import registry, Command |
29 |
import Thuban.UI.mainwindow |
30 |
menu = Thuban.UI.mainwindow.main_menu.InsertMenu("profiler", "&Profiler") |
31 |
|
32 |
# |
33 |
# Customization |
34 |
# |
35 |
# Assign to these in your ~/.thuban/thubanstart |
36 |
|
37 |
# The machine specific profiler bias. See the standard python profile |
38 |
# module for details on how to find out which value to use. |
39 |
profiler_bias = 0 |
40 |
|
41 |
# The directory the profile output is to be written to |
42 |
# (Call mktemp once to initialize tempfile.tempdir) |
43 |
tempfile.mktemp() |
44 |
profile_dir = tempfile.tempdir |
45 |
|
46 |
|
47 |
# |
48 |
# Timing and profiling a complete redraw |
49 |
# |
50 |
|
51 |
def do_redraw(context): |
52 |
"""Perform a complete redraw in the canvas in context""" |
53 |
canvas = context.mainwindow.canvas |
54 |
|
55 |
# Make sure there are no no finished bitmaps and active renderer |
56 |
canvas.full_redraw() |
57 |
|
58 |
# Iterate until all is drawn |
59 |
for c in canvas._render_iterator(): |
60 |
pass |
61 |
|
62 |
|
63 |
# |
64 |
# Profiling the redraw |
65 |
# |
66 |
|
67 |
|
68 |
def profile_screen_renderer(context): |
69 |
"""Script to run the redraw in the profiler |
70 |
|
71 |
The data gathered by the profiler will be written to |
72 |
<TMPDIR>/thuban-render.profile (<TMPDIR> is your system specific |
73 |
temporary directory. |
74 |
|
75 |
See the python documentation of the profile and pstats modules for |
76 |
how to access the data in the generated .profile file. |
77 |
""" |
78 |
print "profiling screen renderer...", |
79 |
prof = profile.Profile(bias = profiler_bias) |
80 |
prof.runctx("do_redraw(context)", globals(), locals()) |
81 |
filename = os.path.join(profile_dir, "thuban-render.profile") |
82 |
prof.dump_stats(filename) |
83 |
print "done and saved to", filename |
84 |
|
85 |
|
86 |
registry.Add(Command("profile_screen_renderer", "Profile Screen Render", |
87 |
profile_screen_renderer, |
88 |
helptext = "Profile the screen render")) |
89 |
menu.InsertItem("profile_screen_renderer") |
90 |
|
91 |
|
92 |
# |
93 |
# Timing the redraw |
94 |
# |
95 |
|
96 |
def time_screen_renderer(context): |
97 |
"""Script to measure the time of a complete redraw. |
98 |
|
99 |
The time taken will be printed to stdout. |
100 |
""" |
101 |
start = time.clock() |
102 |
do_redraw(context) |
103 |
print "redraw finished in", time.clock() - start, "s" |
104 |
|
105 |
|
106 |
registry.Add(Command("time_screen_renderer", "Time Screen Render", |
107 |
time_screen_renderer, |
108 |
helptext = "Time the screen render")) |
109 |
menu.InsertItem("time_screen_renderer") |
110 |
|