~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to profile_imports.py

  • Committer: Danny van Heumen
  • Date: 2010-03-09 21:42:11 UTC
  • mto: (4634.139.5 2.0)
  • mto: This revision was merged to the branch mainline in revision 5160.
  • Revision ID: danny@dannyvanheumen.nl-20100309214211-iqh42x6qcikgd9p3
Reverted now-useless TODO list.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2008, 2009, 2010 by Canonical Ltd
 
1
# Copyright (C) 2006 by Canonical Ltd
2
2
# Written by John Arbash Meinel <john@arbash-meinel.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
17
17
 
18
18
"""A custom importer and regex compiler which logs time spent."""
19
19
 
 
20
import sre
20
21
import sys
21
22
import time
22
23
 
23
24
 
24
 
if sys.version_info < (2, 5, 0):
25
 
    import sre
26
 
    re = sre
27
 
else:
28
 
    import re
29
 
 
30
 
 
31
25
_parent_stack = []
32
26
_total_stack = {}
33
27
_info = {}
64
58
 
65
59
def log_stack_info(out_file, sorted=True, hide_fast=True):
66
60
    # Find all of the roots with import = 0
67
 
    out_file.write('%5s %5s %-40s @ %s:%s\n'
68
 
        % ('cum', 'inline', 'name', 'file', 'line'))
 
61
    out_file.write(' cum  inline name\t\t\t\t\t\tframe\n')
69
62
    todo = [(value[-1], key) for key,value in _info.iteritems() if value[0] == 0]
70
63
 
71
64
    if sorted:
90
83
 
91
84
        # indent, cum_time, mod_time, name,
92
85
        # scope_name, frame_name, frame_lineno
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]))
 
86
        out_file.write('%5.1f %5.1f %s %-35s\t@ %s:%d\n'
 
87
            % (info[-1]*1000., mod_time*1000., '+'*info[0], 
 
88
               cur[1][:35], info[1], info[2]))
97
89
 
98
90
        if sorted:
99
91
            c_times.sort()
104
96
 
105
97
_real_import = __import__
106
98
 
107
 
def timed_import(name, globals=None, locals=None, fromlist=None, level=None):
 
99
def timed_import(name, globals, locals, fromlist):
108
100
    """Wrap around standard importer to log import time"""
109
 
    # normally there are 4, but if this is called as __import__ eg by
110
 
    # /usr/lib/python2.6/email/__init__.py then there may be only one
111
 
    # parameter
112
 
    # level is only passed by python2.6
113
101
 
114
 
    if globals is None:
115
 
        # can't determine the scope name afaics; we could peek up the stack to
116
 
        # see where this is being called from, but it should be a rare case.
117
 
        scope_name = None
 
102
    scope_name = globals.get('__name__', None)
 
103
    if scope_name is None:
 
104
        scope_name = globals.get('__file__', None)
 
105
    if scope_name is None:
 
106
        scope_name = globals.keys()
118
107
    else:
119
 
        scope_name = globals.get('__name__', None)
120
 
        if scope_name is None:
121
 
            scope_name = globals.get('__file__', None)
122
 
        if scope_name is None:
123
 
            scope_name = globals.keys()
124
 
        else:
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')
131
 
            if loc != -1:
132
 
                scope_name = scope_name[loc:]
 
108
        # Trim out paths before bzrlib
 
109
        loc = scope_name.find('bzrlib')
 
110
        if loc != -1:
 
111
            scope_name = scope_name[loc:]
 
112
        # For stdlib, trim out early paths
 
113
        loc = scope_name.find('python2.4')
 
114
        if loc != -1:
 
115
            scope_name = scope_name[loc:]
133
116
 
134
117
    # Figure out the frame that is doing the importing
135
118
    frame = sys._getframe(1)
162
145
    return mod
163
146
 
164
147
 
165
 
_real_compile = re._compile
166
 
 
 
148
_real_compile = sre._compile
167
149
 
168
150
def timed_compile(*args, **kwargs):
169
151
    """Log how long it takes to compile a regex"""
195
177
def install():
196
178
    """Install the hooks for measuring import and regex compile time."""
197
179
    __builtins__['__import__'] = timed_import
198
 
    re._compile = timed_compile
 
180
    sre._compile = timed_compile
199
181
 
200
182
 
201
183
def uninstall():
202
184
    """Remove the import and regex compile timing hooks."""
203
185
    __builtins__['__import__'] = _real_import
204
 
    re._compile = _real_compile
 
186
    sre._compile = _real_compile
205
187