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