Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 1.0, 1.0.1, and 1.0.2

hgtk: add --profile global argument

When --profile is enabled, forking and stderr capture are disabled.
To capture to a file: [profiling] output=C:\profout.txt

Changeset ee84f338d974

Parent 2a4aaac0a523

by Steve Borho

Changes to 2 files · Browse files at ee84f338d974 Showing diff from parent 2a4aaac0a523 Diff from another changeset...

Change 1 of 1 Show Entire File hgtk Stacked
 
51
52
53
54
 
55
56
57
 
51
52
53
 
54
55
56
57
@@ -51,7 +51,7 @@
  _ui.warn('abort: %s\n' % err)   sys.exit(1)   -if not capt or 'THGDEBUG' in os.environ: +if not capt or 'THGDEBUG' in os.environ or '--profile' in sys.argv:   sys.exit(tortoisehg.hgtk.hgtk.dispatch(sys.argv[1:]))  else:   mystderr = cStringIO.StringIO()
 
232
233
234
235
 
236
237
238
 
254
255
256
257
258
259
260
261
262
263
264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
266
267
 
680
681
682
 
683
684
685
 
232
233
234
 
235
236
237
238
 
254
255
256
 
 
 
 
 
 
 
 
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
 
725
726
727
728
729
730
731
@@ -232,7 +232,7 @@
  os.chdir(path)   if options['fork']:   cmdoptions['fork'] = True - if options['nofork']: + if options['nofork'] or options['profile']:   cmdoptions['nofork'] = True   path = paths.find_root(os.getcwd())   if path: @@ -254,14 +254,59 @@
  " (.hg not found)"))     cmdoptions['mainapp'] = True - try: - return func(ui, *args, **cmdoptions) - except TypeError, inst: - # was this an argument error? - tb = traceback.extract_tb(sys.exc_info()[2]) - if len(tb) != 1: # no - raise - raise error.ParseError(cmd, _("invalid arguments")) + d = lambda: util.checksignature(func)(ui, *args, **cmdoptions) + return _runcommand(lui, options, cmd, d) + +def _runcommand(ui, options, cmd, cmdfunc): + def checkargs(): + try: + return cmdfunc() + except error.SignatureError: + raise error.ParseError(cmd, _("invalid arguments")) + + if options['profile']: + format = ui.config('profiling', 'format', default='text') + + if not format in ['text', 'kcachegrind']: + ui.warn(_("unrecognized profiling format '%s'" + " - Ignored\n") % format) + format = 'text' + + output = ui.config('profiling', 'output') + + if output: + path = ui.expandpath(output) + ostream = open(path, 'wb') + else: + ostream = sys.stderr + + try: + from mercurial import lsprof + except ImportError: + raise util.Abort(_( + 'lsprof not available - install from ' + 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) + p = lsprof.Profiler() + p.enable(subcalls=True) + try: + return checkargs() + finally: + p.disable() + + if format == 'kcachegrind': + import lsprofcalltree + calltree = lsprofcalltree.KCacheGrind(p) + calltree.output(ostream) + else: + # format == 'text' + stats = lsprof.Stats(p.getstats()) + stats.sort() + stats.pprint(top=10, file=ostream, climit=5) + + if output: + ostream.close() + else: + return checkargs()    mainwindow = None  def thgexit(win): @@ -680,6 +725,7 @@
  ('q', 'quiet', None, _('suppress output')),   ('h', 'help', None, _('display help and exit')),   ('', 'debugger', None, _('start debugger')), + ('', 'profile', None, _('print command execution profile')),   ('', 'nofork', None, _('do not fork GUI process')),   ('', 'fork', None, _('always fork GUI process')),   ('', 'listfile', '', _('read file list from file')),