Mercurial > public > mercurial-scm > hg-stable
annotate mercurial/lsprof.py @ 43077:687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Done with
python3.7 contrib/byteify-strings.py -i $(hg files 'set:mercurial/**.py - mercurial/thirdparty/** + hgext/**.py - hgext/fsmonitor/pywatchman/** - mercurial/__init__.py')
black -l 80 -t py33 -S $(hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**" - hgext/fsmonitor/pywatchman/**')
# skip-blame mass-reformatting only
Differential Revision: https://phab.mercurial-scm.org/D6972
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:48:39 -0400 |
parents | 2372284d9457 |
children | c59eb1560c44 |
rev | line source |
---|---|
27617
b1a59b80e1a3
lsprof: use print function
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27061
diff
changeset
|
1 from __future__ import absolute_import, print_function |
27061
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
2 |
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
3 import _lsprof |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
4 import sys |
27061
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
5 |
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
6 Profiler = _lsprof.Profiler |
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
7 |
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
8 # PyPy doesn't expose profiler_entry from the module. |
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
9 profiler_entry = getattr(_lsprof, 'profiler_entry', None) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
10 |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
11 __all__ = [b'profile', b'Stats'] |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
12 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
13 |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
14 def profile(f, *args, **kwds): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
15 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
16 p = Profiler() |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
17 p.enable(subcalls=True, builtins=True) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
18 try: |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
19 f(*args, **kwds) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
20 finally: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
21 p.disable() |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
22 return Stats(p.getstats()) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
23 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
24 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
25 class Stats(object): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
26 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
27 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
28 def __init__(self, data): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
29 self.data = data |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
30 |
35876
d4e5b2653693
lsprof: use native string when peeking in __dict__
Augie Fackler <augie@google.com>
parents:
27617
diff
changeset
|
31 def sort(self, crit=r"inlinetime"): |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
32 """XXX docstring""" |
27061
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
33 # profiler_entries isn't defined when running under PyPy. |
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
34 if profiler_entry: |
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
35 if crit not in profiler_entry.__dict__: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
36 raise ValueError(b"Can't sort by %s" % crit) |
27061
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
37 elif self.data and not getattr(self.data[0], crit, None): |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
38 raise ValueError(b"Can't sort by %s" % crit) |
27061
9c75daf89450
lsprof: support PyPy (issue4573)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
18642
diff
changeset
|
39 |
9032
1fa80c5428b8
compat: use 'key' argument instead of 'cmp' when sorting a list
Alejandro Santos <alejolp@alejolp.com>
parents:
7875
diff
changeset
|
40 self.data.sort(key=lambda x: getattr(x, crit), reverse=True) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
41 for e in self.data: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
42 if e.calls: |
9032
1fa80c5428b8
compat: use 'key' argument instead of 'cmp' when sorting a list
Alejandro Santos <alejolp@alejolp.com>
parents:
7875
diff
changeset
|
43 e.calls.sort(key=lambda x: getattr(x, crit), reverse=True) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
44 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
45 def pprint(self, top=None, file=None, limit=None, climit=None): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
46 """XXX docstring""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
47 if file is None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
48 file = sys.stdout |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
49 d = self.data |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
50 if top is not None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
51 d = d[:top] |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
52 cols = b"% 12d %12d %11.4f %11.4f %s\n" |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
53 hcols = b"% 12s %12s %12s %12s %s\n" |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
54 file.write( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
55 hcols |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
56 % ( |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
57 b"CallCount", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
58 b"Recursive", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
59 b"Total(s)", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
60 b"Inline(s)", |
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
61 b"module:lineno(function)", |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
62 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
63 ) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
64 count = 0 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
65 for e in d: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
66 file.write( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
67 cols |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
68 % ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
69 e.callcount, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
70 e.reccallcount, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
71 e.totaltime, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
72 e.inlinetime, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
73 label(e.code), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
74 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
75 ) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
76 count += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
77 if limit is not None and count == limit: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
78 return |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
79 ccount = 0 |
16263
be92ddc636e3
profile: add undocumented config options for profiler output
Matt Mackall <mpm@selenic.com>
parents:
14959
diff
changeset
|
80 if climit and e.calls: |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
81 for se in e.calls: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
82 file.write( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
83 cols |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
84 % ( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
85 se.callcount, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
86 se.reccallcount, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
87 se.totaltime, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
88 se.inlinetime, |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
89 b" %s" % label(se.code), |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
90 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
91 ) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
92 count += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
93 ccount += 1 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
94 if limit is not None and count == limit: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
95 return |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
96 if climit is not None and ccount == climit: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
97 break |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
98 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
99 def freeze(self): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
100 """Replace all references to code objects with string |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
101 descriptions; this makes it possible to pickle the instance.""" |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
102 |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
103 # this code is probably rather ickier than it needs to be! |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
104 for i in range(len(self.data)): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
105 e = self.data[i] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
106 if not isinstance(e.code, str): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
107 self.data[i] = type(e)((label(e.code),) + e[1:]) |
5992
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
108 if e.calls: |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
109 for j in range(len(e.calls)): |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
110 se = e.calls[j] |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
111 if not isinstance(se.code, str): |
30c40ba10963
updating lsprof.py from remote repository
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
2497
diff
changeset
|
112 e.calls[j] = type(se)((label(se.code),) + se[1:]) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
113 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
114 |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
115 _fn2mod = {} |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
116 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
117 |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
118 def label(code): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
119 if isinstance(code, str): |
40202
56ea22fa55f0
py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40193
diff
changeset
|
120 if sys.version_info.major >= 3: |
56ea22fa55f0
py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40193
diff
changeset
|
121 code = code.encode('latin-1') |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
122 return code |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
123 try: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
124 mname = _fn2mod[code.co_filename] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
125 except KeyError: |
9314
3f93f6838639
lsprof: make profile not die when imported modules changes (issue1774)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
7875
diff
changeset
|
126 for k, v in list(sys.modules.iteritems()): |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
127 if v is None: |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
128 continue |
14959
b1dcc5ab86cd
lsprof: use getattr instead of hasattr
Augie Fackler <durin42@gmail.com>
parents:
12842
diff
changeset
|
129 if not isinstance(getattr(v, '__file__', None), str): |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
130 continue |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
131 if v.__file__.startswith(code.co_filename): |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
132 mname = _fn2mod[code.co_filename] = k |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
133 break |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
134 else: |
40202
56ea22fa55f0
py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40193
diff
changeset
|
135 mname = _fn2mod[code.co_filename] = r'<%s>' % code.co_filename |
56ea22fa55f0
py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40193
diff
changeset
|
136 |
56ea22fa55f0
py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40193
diff
changeset
|
137 res = r'%s:%d(%s)' % (mname, code.co_firstlineno, code.co_name) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
138 |
40202
56ea22fa55f0
py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40193
diff
changeset
|
139 if sys.version_info.major >= 3: |
56ea22fa55f0
py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40193
diff
changeset
|
140 res = res.encode('latin-1') |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
141 |
40202
56ea22fa55f0
py3: encode str to bytes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
40193
diff
changeset
|
142 return res |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
143 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
144 |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
145 if __name__ == '__main__': |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
146 import os |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
40202
diff
changeset
|
147 |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
148 sys.argv = sys.argv[1:] |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
149 if not sys.argv: |
43077
687b865b95ad
formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents:
43076
diff
changeset
|
150 print(b"usage: lsprof.py <script> <arguments...>", file=sys.stderr) |
2422
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
151 sys.exit(2) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
152 sys.path.insert(0, os.path.abspath(os.path.dirname(sys.argv[0]))) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
153 stats = profile(execfile, sys.argv[0], globals(), locals()) |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
154 stats.sort() |
6aa75e77cafe
add --lsprof option. 3x faster than --profile, more useful output.
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
diff
changeset
|
155 stats.pprint() |