~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/lsprof.py

  • Committer: John Arbash Meinel
  • Date: 2011-02-10 01:00:32 UTC
  • mto: This revision was merged to the branch mainline in revision 426.
  • Revision ID: john@arbash-meinel.com-20110210010032-3c1ngxr9rlnlla6o
Avoid getting a lazy object error by instantiating an
http transport before we do any other work.
Add a script which works against multiple concurrent services.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#
1
3
# this is copied from the lsprof distro because somehow
2
4
# it is not installed by distutils
3
5
# I made one modification to profile so that it returns a pair
24
26
 
25
27
 
26
28
def profile(f, *args, **kwds):
27
 
    """XXX docstring"""
 
29
    """FIXME: docstring"""
28
30
    global _g_threadmap
29
31
    p = Profiler()
30
32
    p.enable(subcalls=True)
45
47
 
46
48
 
47
49
class Stats(object):
48
 
    """XXX docstring"""
 
50
    """FIXME: docstring"""
49
51
 
50
52
    def __init__(self, data, threads):
51
53
        self.data = data
52
54
        self.threads = threads
53
55
 
54
56
    def sort(self, crit="inlinetime"):
55
 
        """XXX docstring"""
 
57
        """FIXME: docstring"""
56
58
        if crit not in profiler_entry.__dict__:
57
 
            raise ValueError, "Can't sort by %s" % crit
 
59
            raise ValueError("Can't sort by %s" % crit)
58
60
        self.data.sort(lambda b, a: cmp(getattr(a, crit),
59
61
                                        getattr(b, crit)))
60
62
        for e in self.data:
63
65
                                              getattr(b, crit)))
64
66
 
65
67
    def pprint(self, top=None, file=None):
66
 
        """XXX docstring"""
 
68
        """FIXME: docstring"""
67
69
        if file is None:
68
70
            file = sys.stdout
69
71
        d = self.data
71
73
            d = d[:top]
72
74
        cols = "% 12s %12s %11.4f %11.4f   %s\n"
73
75
        hcols = "% 12s %12s %12s %12s %s\n"
74
 
        cols2 = "+%12s %12s %11.4f %11.4f +  %s\n"
75
76
        file.write(hcols % ("CallCount", "Recursive", "Total(ms)",
76
77
                            "Inline(ms)", "module:lineno(function)"))
77
78
        for e in d:
91
92
        for i in range(len(self.data)):
92
93
            e = self.data[i]
93
94
            if not isinstance(e.code, str):
94
 
                self.data[i] = type(e)((label(e.code),) + e[1:])
 
95
                self.data[i] = type(e)((label(e.code)) + e[1:])
95
96
            if e.calls:
96
97
                for j in range(len(e.calls)):
97
98
                    se = e.calls[j]
98
99
                    if not isinstance(se.code, str):
99
 
                        e.calls[j] = type(se)((label(se.code),) + se[1:])
 
100
                        e.calls[j] = type(se)((label(se.code)) + se[1:])
100
101
        for s in self.threads.values():
101
102
            s.freeze()
102
103
 
123
124
        for entry in self.data:
124
125
            totaltime = int(entry.totaltime * 1000)
125
126
            max_cost = max(max_cost, totaltime)
126
 
        print >> self.out_file, 'summary: %d' % (max_cost,)
 
127
        print >> self.out_file, 'summary: %d' % (max_cost)
127
128
 
128
129
    def _entry(self, entry):
129
130
        out_file = self.out_file
130
131
        code = entry.code
131
132
        inlinetime = int(entry.inlinetime * 1000)
132
133
        #print >> out_file, 'ob=%s' % (code.co_filename,)
133
 
        print >> out_file, 'fi=%s' % (code.co_filename,)
134
 
        print >> out_file, 'fn=%s' % (label(code, True),)
 
134
        print >> out_file, 'fi=%s' % (code.co_filename)
 
135
        print >> out_file, 'fn=%s' % (label(code, True))
135
136
        print >> out_file, '%d %d' % (code.co_firstlineno, inlinetime)
136
137
        # recursive calls are counted in entry.calls
137
138
        if entry.calls:
147
148
        code = subentry.code
148
149
        totaltime = int(subentry.totaltime * 1000)
149
150
        #print >> out_file, 'cob=%s' % (code.co_filename,)
150
 
        print >> out_file, 'cfn=%s' % (label(code, True),)
151
 
        print >> out_file, 'cfi=%s' % (code.co_filename,)
 
151
        print >> out_file, 'cfn=%s' % (label(code, True))
 
152
        print >> out_file, 'cfi=%s' % (code.co_filename)
152
153
        print >> out_file, 'calls=%d %d' % (
153
154
            subentry.callcount, code.co_firstlineno)
154
155
        print >> out_file, '%d %d' % (lineno, totaltime)
156
157
 
157
158
_fn2mod = {}
158
159
 
 
160
 
159
161
def label(code, calltree=False):
160
162
    if isinstance(code, str):
161
163
        return code