##// END OF EJS Templates
profiling: make profiling functions context managers (API)...
Gregory Szorc -
r29783:5d44197c default
parent child Browse files
Show More
@@ -909,7 +909,8 b' def _runcommand(ui, options, cmd, cmdfun'
909 raise error.CommandError(cmd, _("invalid arguments"))
909 raise error.CommandError(cmd, _("invalid arguments"))
910
910
911 if ui.configbool('profiling', 'enabled'):
911 if ui.configbool('profiling', 'enabled'):
912 return profiling.profile(ui, checkargs)
912 with profiling.profile(ui):
913 return checkargs()
913 else:
914 else:
914 return checkargs()
915 return checkargs()
915
916
@@ -7,6 +7,7 b''
7
7
8 from __future__ import absolute_import, print_function
8 from __future__ import absolute_import, print_function
9
9
10 import contextlib
10 import os
11 import os
11 import sys
12 import sys
12 import time
13 import time
@@ -17,7 +18,8 b' from . import ('
17 util,
18 util,
18 )
19 )
19
20
20 def lsprofile(ui, func, fp):
21 @contextlib.contextmanager
22 def lsprofile(ui, fp):
21 format = ui.config('profiling', 'format', default='text')
23 format = ui.config('profiling', 'format', default='text')
22 field = ui.config('profiling', 'sort', default='inlinetime')
24 field = ui.config('profiling', 'sort', default='inlinetime')
23 limit = ui.configint('profiling', 'limit', default=30)
25 limit = ui.configint('profiling', 'limit', default=30)
@@ -37,7 +39,7 b' def lsprofile(ui, func, fp):'
37 p = lsprof.Profiler()
39 p = lsprof.Profiler()
38 p.enable(subcalls=True)
40 p.enable(subcalls=True)
39 try:
41 try:
40 return func()
42 yield
41 finally:
43 finally:
42 p.disable()
44 p.disable()
43
45
@@ -51,7 +53,8 b' def lsprofile(ui, func, fp):'
51 stats.sort(field)
53 stats.sort(field)
52 stats.pprint(limit=limit, file=fp, climit=climit)
54 stats.pprint(limit=limit, file=fp, climit=climit)
53
55
54 def flameprofile(ui, func, fp):
56 @contextlib.contextmanager
57 def flameprofile(ui, fp):
55 try:
58 try:
56 from flamegraph import flamegraph
59 from flamegraph import flamegraph
57 except ImportError:
60 except ImportError:
@@ -67,7 +70,7 b' def flameprofile(ui, func, fp):'
67 start_time = time.clock()
70 start_time = time.clock()
68 try:
71 try:
69 thread.start()
72 thread.start()
70 func()
73 yield
71 finally:
74 finally:
72 thread.stop()
75 thread.stop()
73 thread.join()
76 thread.join()
@@ -75,7 +78,8 b' def flameprofile(ui, func, fp):'
75 time.clock() - start_time, thread.num_frames(),
78 time.clock() - start_time, thread.num_frames(),
76 thread.num_frames(unique=True)))
79 thread.num_frames(unique=True)))
77
80
78 def statprofile(ui, func, fp):
81 @contextlib.contextmanager
82 def statprofile(ui, fp):
79 try:
83 try:
80 import statprof
84 import statprof
81 except ImportError:
85 except ImportError:
@@ -90,13 +94,18 b' def statprofile(ui, func, fp):'
90
94
91 statprof.start()
95 statprof.start()
92 try:
96 try:
93 return func()
97 yield
94 finally:
98 finally:
95 statprof.stop()
99 statprof.stop()
96 statprof.display(fp)
100 statprof.display(fp)
97
101
98 def profile(ui, fn):
102 @contextlib.contextmanager
99 """Profile a function call."""
103 def profile(ui):
104 """Start profiling.
105
106 Profiling is active when the context manager is active. When the context
107 manager exits, profiling results will be written to the configured output.
108 """
100 profiler = os.getenv('HGPROF')
109 profiler = os.getenv('HGPROF')
101 if profiler is None:
110 if profiler is None:
102 profiler = ui.config('profiling', 'type', default='ls')
111 profiler = ui.config('profiling', 'type', default='ls')
@@ -116,11 +125,15 b' def profile(ui, fn):'
116
125
117 try:
126 try:
118 if profiler == 'ls':
127 if profiler == 'ls':
119 return lsprofile(ui, fn, fp)
128 proffn = lsprofile
120 elif profiler == 'flame':
129 elif profiler == 'flame':
121 return flameprofile(ui, fn, fp)
130 proffn = flameprofile
122 else:
131 else:
123 return statprofile(ui, fn, fp)
132 proffn = statprofile
133
134 with proffn(ui, fp):
135 yield
136
124 finally:
137 finally:
125 if output:
138 if output:
126 if output == 'blackbox':
139 if output == 'blackbox':
General Comments 0
You need to be logged in to leave comments. Login now