~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/lsprof.py

  • Committer: Ian Clatworthy
  • Date: 2007-06-06 10:45:08 UTC
  • mto: This revision was merged to the branch mainline in revision 2527.
  • Revision ID: ian.clatworthy@internode.on.net-20070606104508-q2i41r4r23xkaebs
changes requested in jameinel's review incorporated

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# I made one modification to profile so that it returns a pair
4
4
# instead of just the Stats object
5
5
 
 
6
import cPickle
 
7
import os
6
8
import sys
7
9
import thread
8
10
import threading
104
106
        """Output profiling data in calltree format (for KCacheGrind)."""
105
107
        _CallTreeFilter(self.data).output(file)
106
108
 
 
109
    def save(self, filename, format=None):
 
110
        """Save profiling data to a file.
 
111
 
 
112
        :param filename: the name of the output file
 
113
        :param format: 'txt' for a text representation;
 
114
            'callgrind' for calltree format;
 
115
            otherwise a pickled Python object. A format of None indicates
 
116
            that the format to use is to be found from the extension of
 
117
            filename.
 
118
        """
 
119
        if format is None:
 
120
            ext = os.path.splitext(filename)[1]
 
121
            if len(ext) > 1:
 
122
                format = ext[1:]
 
123
        outfile = open(filename, 'wb')
 
124
        try:
 
125
            if format == "callgrind":
 
126
                self.calltree(outfile)
 
127
            elif format == "txt":
 
128
                self.pprint(file=outfile)
 
129
            else:
 
130
                self.freeze()
 
131
                cPickle.dump(self, outfile, 2)
 
132
        finally:
 
133
            outfile.close()
 
134
 
107
135
 
108
136
class _CallTreeFilter(object):
109
137
    """Converter of a Stats object to input suitable for KCacheGrind.