Show More
@@ -141,35 +141,41 b' def statprofile(ui, fp):' | |||||
141 |
|
141 | |||
142 | statprof.display(fp, data=data, format=displayformat, **kwargs) |
|
142 | statprof.display(fp, data=data, format=displayformat, **kwargs) | |
143 |
|
143 | |||
144 | @contextlib.contextmanager |
|
144 | class profile(object): | |
145 | def profile(ui): |
|
|||
146 | """Start profiling. |
|
145 | """Start profiling. | |
147 |
|
146 | |||
148 | Profiling is active when the context manager is active. When the context |
|
147 | Profiling is active when the context manager is active. When the context | |
149 | manager exits, profiling results will be written to the configured output. |
|
148 | manager exits, profiling results will be written to the configured output. | |
150 | """ |
|
149 | """ | |
151 | profiler = encoding.environ.get('HGPROF') |
|
150 | def __init__(self, ui): | |
152 | proffn = None |
|
151 | self._ui = ui | |
153 | if profiler is None: |
|
152 | self._output = None | |
154 | profiler = ui.config('profiling', 'type', default='stat') |
|
153 | self._fp = None | |
155 | if profiler not in ('ls', 'stat', 'flame'): |
|
154 | self._profiler = None | |
156 | # try load profiler from extension with the same name |
|
|||
157 | proffn = _loadprofiler(ui, profiler) |
|
|||
158 | if proffn is None: |
|
|||
159 | ui.warn(_("unrecognized profiler '%s' - ignored\n") % profiler) |
|
|||
160 | profiler = 'stat' |
|
|||
161 |
|
155 | |||
162 | output = ui.config('profiling', 'output') |
|
156 | def __enter__(self): | |
|
157 | profiler = encoding.environ.get('HGPROF') | |||
|
158 | proffn = None | |||
|
159 | if profiler is None: | |||
|
160 | profiler = self._ui.config('profiling', 'type', default='stat') | |||
|
161 | if profiler not in ('ls', 'stat', 'flame'): | |||
|
162 | # try load profiler from extension with the same name | |||
|
163 | proffn = _loadprofiler(self._ui, profiler) | |||
|
164 | if proffn is None: | |||
|
165 | self._ui.warn(_("unrecognized profiler '%s' - ignored\n") | |||
|
166 | % profiler) | |||
|
167 | profiler = 'stat' | |||
163 |
|
168 | |||
164 | if output == 'blackbox': |
|
169 | self._output = self._ui.config('profiling', 'output') | |
165 | fp = util.stringio() |
|
|||
166 | elif output: |
|
|||
167 | path = ui.expandpath(output) |
|
|||
168 | fp = open(path, 'wb') |
|
|||
169 | else: |
|
|||
170 | fp = ui.ferr |
|
|||
171 |
|
170 | |||
172 | try: |
|
171 | if self._output == 'blackbox': | |
|
172 | self._fp = util.stringio() | |||
|
173 | elif self._output: | |||
|
174 | path = self._ui.expandpath(self._output) | |||
|
175 | self._fp = open(path, 'wb') | |||
|
176 | else: | |||
|
177 | self._fp = self._ui.ferr | |||
|
178 | ||||
173 | if proffn is not None: |
|
179 | if proffn is not None: | |
174 | pass |
|
180 | pass | |
175 | elif profiler == 'ls': |
|
181 | elif profiler == 'ls': | |
@@ -179,18 +185,21 b' def profile(ui):' | |||||
179 | else: |
|
185 | else: | |
180 | proffn = statprofile |
|
186 | proffn = statprofile | |
181 |
|
187 | |||
182 |
|
|
188 | self._profiler = proffn(self._ui, self._fp) | |
183 | yield |
|
189 | self._profiler.__enter__() | |
184 |
|
190 | |||
185 | finally: |
|
191 | def __exit__(self, exception_type, exception_value, traceback): | |
186 | if output: |
|
192 | if self._profiler is None: | |
187 | if output == 'blackbox': |
|
193 | return | |
188 | val = 'Profile:\n%s' % fp.getvalue() |
|
194 | self._profiler.__exit__(exception_type, exception_value, traceback) | |
|
195 | if self._output: | |||
|
196 | if self._output == 'blackbox': | |||
|
197 | val = 'Profile:\n%s' % self._fp.getvalue() | |||
189 | # ui.log treats the input as a format string, |
|
198 | # ui.log treats the input as a format string, | |
190 | # so we need to escape any % signs. |
|
199 | # so we need to escape any % signs. | |
191 | val = val.replace('%', '%%') |
|
200 | val = val.replace('%', '%%') | |
192 | ui.log('profile', val) |
|
201 | self._ui.log('profile', val) | |
193 | fp.close() |
|
202 | self._fp.close() | |
194 |
|
203 | |||
195 | @contextlib.contextmanager |
|
204 | @contextlib.contextmanager | |
196 | def maybeprofile(ui): |
|
205 | def maybeprofile(ui): |
General Comments 0
You need to be logged in to leave comments.
Login now