/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 breezy/lsprof.py

Merge test-run support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
__all__ = ['profile', 'Stats']
26
26
 
27
 
 
28
27
def profile(f, *args, **kwds):
29
28
    """Run a function profile.
30
29
 
48
47
 
49
48
class BzrProfiler(object):
50
49
    """Bzr utility wrapper around Profiler.
51
 
 
 
50
    
52
51
    For most uses the module level 'profile()' function will be suitable.
53
52
    However profiling when a simple wrapped function isn't available may
54
53
    be easier to accomplish using this class.
60
59
    Note that profiling involves a threading.Lock around the actual profiling.
61
60
    This is needed because profiling involves global manipulation of the python
62
61
    interpreter state. As such you cannot perform multiple profiles at once.
63
 
    Trying to do so will lock out the second profiler unless the global
 
62
    Trying to do so will lock out the second profiler unless the global 
64
63
    breezy.lsprof.BzrProfiler.profiler_block is set to 0. Setting it to 0 will
65
64
    cause profiling to fail rather than blocking.
66
65
    """
73
72
 
74
73
    def start(self):
75
74
        """Start profiling.
76
 
 
 
75
        
77
76
        This hooks into threading and will record all calls made until
78
77
        stop() is called.
79
78
        """
86
85
        try:
87
86
            self.p.enable(subcalls=True)
88
87
            threading.setprofile(self._thread_profile)
89
 
        except BaseException:
 
88
        except:
90
89
            self.__class__.profiler_lock.release()
91
90
            raise
92
91
 
162
161
            d = d[:top]
163
162
        cols = "% 12s %12s %11.4f %11.4f   %s\n"
164
163
        hcols = "% 12s %12s %12s %12s %s\n"
 
164
        cols2 = "+%12s %12s %11.4f %11.4f +  %s\n"
165
165
        file.write(hcols % ("CallCount", "Recursive", "Total(ms)",
166
166
                            "Inline(ms)", "module:lineno(function)"))
167
167
        for e in d:
258
258
        out_file = self.out_file
259
259
        code = entry.code
260
260
        inlinetime = int(entry.inlinetime * 1000)
 
261
        #out_file.write('ob=%s\n' % (code.co_filename,))
261
262
        if isinstance(code, str):
262
263
            out_file.write('fi=~\n')
263
264
        else:
284
285
        out_file = self.out_file
285
286
        code = subentry.code
286
287
        totaltime = int(subentry.totaltime * 1000)
 
288
        #out_file.write('cob=%s\n' % (code.co_filename,))
287
289
        if isinstance(code, str):
288
290
            out_file.write('cfi=~\n')
289
291
            out_file.write('cfn=%s\n' % (label(code, True),))
295
297
                subentry.callcount, code.co_firstlineno))
296
298
        out_file.write('%d %d\n' % (lineno, totaltime))
297
299
 
298
 
 
299
300
_fn2mod = {}
300
301
 
301
 
 
302
302
def label(code, calltree=False):
303
303
    if isinstance(code, str):
304
304
        return code
316
316
                mname = _fn2mod[code.co_filename] = k
317
317
                break
318
318
        else:
319
 
            mname = _fn2mod[code.co_filename] = '<%s>' % code.co_filename
 
319
            mname = _fn2mod[code.co_filename] = '<%s>'%code.co_filename
320
320
    if calltree:
321
321
        return '%s %s:%d' % (code.co_name, mname, code.co_firstlineno)
322
322
    else: