/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to profile_imports.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""A custom importer and regex compiler which logs time spent."""
19
19
 
20
 
import re
21
20
import sys
22
21
import time
23
22
 
24
23
 
 
24
if sys.version_info < (2, 5, 0):
 
25
    import sre
 
26
    re = sre
 
27
else:
 
28
    import re
 
29
 
 
30
 
25
31
_parent_stack = []
26
32
_total_stack = {}
27
33
_info = {}
41
47
        _total_stack[_parent_stack[-1]].append(this_stack)
42
48
    _total_stack[this_stack] = []
43
49
    _parent_stack.append(this_stack)
44
 
    _info[this_stack] = [len(_parent_stack) - 1, frame_name, frame_lineno,
45
 
                         scope_name]
 
50
    _info[this_stack] = [len(_parent_stack)-1, frame_name, frame_lineno, scope_name]
46
51
 
47
52
    return this_stack
48
53
 
59
64
 
60
65
def log_stack_info(out_file, sorted=True, hide_fast=True):
61
66
    # Find all of the roots with import = 0
62
 
    out_file.write(
63
 
        '%5s %5s %-40s @ %s:%s\n'
64
 
        % ('cum', 'local', 'name', 'file', 'line'))
65
 
    todo = [(value[-1], key) for key, value in _info.items() if value[0] == 0]
 
67
    out_file.write('%5s %5s %-40s @ %s:%s\n'
 
68
        % ('cum', 'inline', 'name', 'file', 'line'))
 
69
    todo = [(value[-1], key) for key,value in _info.iteritems() if value[0] == 0]
66
70
 
67
71
    if sorted:
68
72
        todo.sort()
86
90
 
87
91
        # indent, cum_time, mod_time, name,
88
92
        # scope_name, frame_name, frame_lineno
89
 
        out_file.write(
90
 
            '%5.1f %5.1f %-40s @ %s:%d\n' % (
91
 
                info[-1] * 1000., mod_time * 1000.,
92
 
                ('+' * info[0] + cur[1]), info[1], info[2]))
 
93
        out_file.write('%5.1f %5.1f %-40s @ %s:%d\n'
 
94
            % (info[-1]*1000., mod_time*1000.,
 
95
               ('+'*info[0] + cur[1]),
 
96
               info[1], info[2]))
93
97
 
94
98
        if sorted:
95
99
            c_times.sort()
100
104
 
101
105
_real_import = __import__
102
106
 
103
 
def timed_import(name, globals=None, locals=None, fromlist=None, level=0):
 
107
def timed_import(name, globals=None, locals=None, fromlist=None, level=None):
104
108
    """Wrap around standard importer to log import time"""
105
109
    # normally there are 4, but if this is called as __import__ eg by
106
110
    # /usr/lib/python2.6/email/__init__.py then there may be only one
107
111
    # parameter
108
 
    # level has different default between Python 2 and 3, but codebase
 
112
    # level is only passed by python2.6
 
113
 
109
114
    if globals is None:
110
115
        # can't determine the scope name afaics; we could peek up the stack to
111
116
        # see where this is being called from, but it should be a rare case.
117
122
        if scope_name is None:
118
123
            scope_name = globals.keys()
119
124
        else:
120
 
            # Trim out paths before breezy
121
 
            loc = scope_name.find('breezy')
 
125
            # Trim out paths before bzrlib
 
126
            loc = scope_name.find('bzrlib')
 
127
            if loc != -1:
 
128
                scope_name = scope_name[loc:]
 
129
            # For stdlib, trim out early paths
 
130
            loc = scope_name.find('python2.4')
122
131
            if loc != -1:
123
132
                scope_name = scope_name[loc:]
124
133
 
145
154
    tstart = _timer()
146
155
    try:
147
156
        # Do the import
148
 
        return _real_import(name, globals, locals, fromlist, level=level)
 
157
        mod = _real_import(name, globals, locals, fromlist)
149
158
    finally:
150
 
        tload = _timer() - tstart
 
159
        tload = _timer()-tstart
151
160
        stack_finish(this, tload)
152
161
 
153
 
 
154
 
def _repr_regexp(pattern, max_len=30):
155
 
    """Present regexp pattern for logging, truncating if over max_len."""
156
 
    if len(pattern) > max_len:
157
 
        return repr(pattern[:max_len - 3]) + "..."
158
 
    return repr(pattern)
 
162
    return mod
159
163
 
160
164
 
161
165
_real_compile = re._compile
175
179
        frame = sys._getframe(5)
176
180
        frame_name = frame.f_globals.get('__name__', '<unknown>')
177
181
    frame_lineno = frame.f_lineno
178
 
    this = stack_add(extra + _repr_regexp(args[0]), frame_name, frame_lineno)
 
182
    this = stack_add(extra+repr(args[0]), frame_name, frame_lineno)
179
183
 
180
184
    tstart = _timer()
181
185
    try:
198
202
    """Remove the import and regex compile timing hooks."""
199
203
    __builtins__['__import__'] = _real_import
200
204
    re._compile = _real_compile
 
205