1
# Copyright (C) 2006 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""A custom importer and regex compiler which logs time."""
25
_import_logfile = sys.stderr
26
_compile_logfile = sys.stderr
28
_real_import = __import__
30
def _custom_import(name, globals, locals, fromlist):
31
"""Wrap around standard importer to log import time"""
32
if _import_logfile is None:
33
return _real_import(name, globals, locals, fromlist)
35
scope_name = globals.get('__name__', None)
36
if scope_name is None:
37
scope_name = globals.get('__file__', None)
38
if scope_name is None:
39
scope_name = globals.keys()
41
# Trim out paths before bzrlib
42
loc = scope_name.find('bzrlib')
44
scope_name = scope_name[loc:]
45
# For stdlib, trim out early paths
46
loc = scope_name.find('python2.4')
48
scope_name = scope_name[loc:]
52
mod = _real_import(name, globals, locals, fromlist)
53
tload = time.time()-tstart
55
# Figure out the frame that is doing the importing
56
frame = sys._getframe(1)
57
frame_name = frame.f_globals.get('__name__', '<unknown>')
60
if frame_name.endswith('demandload'):
61
# If this was demandloaded, we have 3 frames to ignore
62
extra = ' (demandload)'
63
frame = sys._getframe(4)
65
frame_name = frame.f_globals.get('__name__', '<unknown>')
66
frame_lineno = frame.f_lineno
69
_import_logfile.write('%3.0fms %-24s\tfor %-24s\t@ %s:%d%s\n'
70
% ((time.time()-tstart)*1000, name, scope_name,
71
frame_name, frame_lineno, extra))
73
# If the import took a long time, log the stack that generated
74
# this import. Especially necessary for demandloaded code
77
for fnum in range(cur_frame+1, cur_frame+10):
79
f = sys._getframe(fnum)
83
% (f.f_globals.get('__name__', '<unknown>'),
87
_import_logfile.write('\t' + ' '.join(stack) + '\n')
91
_real_compile = sre._compile
93
def _custom_compile(*args, **kwargs):
94
"""Log how long it takes to compile a regex"""
95
if _compile_logfile is None:
96
return _real_compile(*args, **kwargs)
98
# Measure the compile time
100
comp = _real_compile(*args, **kwargs)
102
# And who is requesting this?
103
frame = sys._getframe(2)
104
frame_name = frame.f_globals.get('__name__', '<unknown>')
105
frame_lineno = frame.f_lineno
106
_compile_logfile.write('%3.0fms %-40r\t@ %s:%d\n'
107
% ((time.time()-tstart)*1000, args[0][:40],
108
frame_name, frame_lineno))
111
__builtins__.__import__ = _custom_import
112
sre._compile = _custom_compile