comparison mercurial/dispatch.py @ 5995:b913d3aacddc

profiling: allow CGI and FastCGI to be profiled
author Bryan O'Sullivan <bos@serpentine.com>
date Fri, 01 Feb 2008 13:09:45 -0800
parents 2c565b9598b8
children 637d4c089834 90e5c82a3859
comparison
equal deleted inserted replaced
5994:a445388aa554 5995:b913d3aacddc
371 # was this an argument error? 371 # was this an argument error?
372 tb = traceback.extract_tb(sys.exc_info()[2]) 372 tb = traceback.extract_tb(sys.exc_info()[2])
373 if len(tb) != 2: # no 373 if len(tb) != 2: # no
374 raise 374 raise
375 raise ParseError(cmd, _("invalid arguments")) 375 raise ParseError(cmd, _("invalid arguments"))
376 376 return profiled(ui, checkargs, options)
377 if options['profile']: 377
378 def profiled(ui, func, options={}):
379 def profile_fp():
380 outfile = ui.config('profile', 'output', untrusted=True)
381 if outfile:
382 return open(outfile, 'w')
383 else:
384 return sys.stderr
385
386 if options.get('profile') or ui.config('profile', 'enable') == 'hotshot':
378 import hotshot, hotshot.stats 387 import hotshot, hotshot.stats
379 prof = hotshot.Profile("hg.prof") 388 prof = hotshot.Profile("hg.prof")
380 try: 389 try:
381 try: 390 try:
382 return prof.runcall(checkargs) 391 return prof.runcall(checkargs)
388 pass 397 pass
389 raise 398 raise
390 finally: 399 finally:
391 prof.close() 400 prof.close()
392 stats = hotshot.stats.load("hg.prof") 401 stats = hotshot.stats.load("hg.prof")
402 stats.stream = profile_fp()
393 stats.strip_dirs() 403 stats.strip_dirs()
394 stats.sort_stats('time', 'calls') 404 stats.sort_stats('time', 'calls')
395 stats.print_stats(40) 405 stats.print_stats(40)
396 elif options['lsprof']: 406 elif options.get('lsprof') or ui.config('profile', 'enable') == 'lsprof':
397 try: 407 try:
398 from mercurial import lsprof 408 from mercurial import lsprof
399 except ImportError: 409 except ImportError:
400 raise util.Abort(_( 410 raise util.Abort(_(
401 'lsprof not available - install from ' 411 'lsprof not available - install from '
402 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/')) 412 'http://codespeak.net/svn/user/arigo/hack/misc/lsprof/'))
403 p = lsprof.Profiler() 413 p = lsprof.Profiler()
404 p.enable(subcalls=True) 414 p.enable(subcalls=True)
405 try: 415 try:
406 return checkargs() 416 return func()
407 finally: 417 finally:
408 p.disable() 418 p.disable()
409 stats = lsprof.Stats(p.getstats()) 419 stats = lsprof.Stats(p.getstats())
410 stats.sort() 420 stats.sort()
411 stats.pprint(top=10, file=sys.stderr, climit=5) 421 stats.pprint(top=10, file=profile_fp(), climit=5)
412 else: 422 else:
413 return checkargs() 423 return func()