~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

  • Committer: Vincent Ladeuil
  • Date: 2009-06-22 12:52:39 UTC
  • mto: (4471.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4472.
  • Revision ID: v.ladeuil+lp@free.fr-20090622125239-kabo9smxt9c3vnir
Use a consistent scheme for naming pyrex source files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
from _lsprof import Profiler, profiler_entry
12
12
 
13
13
 
14
 
import bzrlib.osutils
15
 
 
16
 
 
17
14
__all__ = ['profile', 'Stats']
18
15
 
19
16
_g_threadmap = {}
30
27
 
31
28
 
32
29
def profile(f, *args, **kwds):
33
 
    """XXX docstring"""
 
30
    """Run a function profile.
 
31
 
 
32
    Exceptions are not caught: If you need stats even when exceptions are to be
 
33
    raised, passing in a closure that will catch the exceptions and transform
 
34
    them appropriately for your driver function.
 
35
 
 
36
    :return: The functions return value and a stats object.
 
37
    """
34
38
    global _g_threadmap
35
39
    p = Profiler()
36
40
    p.enable(subcalls=True)
37
41
    threading.setprofile(_thread_profile)
38
 
    # Note: The except clause is needed below so that profiling data still
39
 
    # gets dumped even when exceptions are encountered. The except clause code
40
 
    # is taken straight from run_bzr_catch_errrors() in commands.py and ought
41
 
    # to be kept in sync with it.
42
42
    try:
43
 
        try:
44
 
            ret = f(*args, **kwds)
45
 
        except (KeyboardInterrupt, Exception), e:
46
 
            import bzrlib.trace
47
 
            bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
48
 
            ret = 3
 
43
        ret = f(*args, **kwds)
49
44
    finally:
50
45
        p.disable()
51
46
        for pp in _g_threadmap.values():
52
47
            pp.disable()
53
48
        threading.setprofile(None)
54
 
    
 
49
 
55
50
    threads = {}
56
51
    for tid, pp in _g_threadmap.items():
57
52
        threads[tid] = Stats(pp.getstats(), {})
131
126
            otherwise the format is given by the filename extension.
132
127
        """
133
128
        if format is None:
134
 
            basename = bzrlib.osutils.basename(filename)
 
129
            basename = os.path.basename(filename)
135
130
            if basename.startswith('callgrind.out'):
136
131
                format = "callgrind"
137
132
            else:
138
 
                ext = bzrlib.osutils.splitext(filename)[1]
 
133
                ext = os.path.splitext(filename)[1]
139
134
                if len(ext) > 1:
140
135
                    format = ext[1:]
141
136
        outfile = open(filename, 'wb')
156
151
 
157
152
    This code is taken from http://ddaa.net/blog/python/lsprof-calltree
158
153
    with the changes made by J.P. Calderone and Itamar applied. Note that
159
 
    isinstance(code, str) needs to be used at times to determine if the code 
 
154
    isinstance(code, str) needs to be used at times to determine if the code
160
155
    object is actually an external code object (with a filename, etc.) or
161
156
    a Python built-in.
162
157
    """
166
161
        self.out_file = None
167
162
 
168
163
    def output(self, out_file):
169
 
        self.out_file = out_file        
 
164
        self.out_file = out_file
170
165
        out_file.write('events: Ticks\n')
171
166
        self._print_summary()
172
167
        for entry in self.data: