In general your methodology could be to do timing blocks around the sections of code and then issue logging statements. As far as the shutdown after init, I'm not familiar with the specifics of what you're using.
Edit: I've used this middleware to help me find performance sinkholes. It's currently a werkzeug middleware, you may be able to adapt it for you usage. Hope it helps
import re
re_profile = re.compile(ur'(^|&|\?)prof($|=|&)')
class ProfilerMiddleware(BaseProcessor):
def process_runner(self, runner, environ):
self.profiler = None
if (environ['REMOTE_ADDR'] in settings_static.internal_ips or settings_static.local_server) and re_profile.match(environ['QUERY_STRING']):
self.profiler = cProfile.Profile()
def wrap(*args, **kwargs):
return self.profiler.runcall(runner, *args, **kwargs)
return wrap
def process_response(self, request, response):
if self.profiler:
self.profiler.create_stats()
out = StringIO.StringIO()
old_stdout, sys.stdout = sys.stdout, out
#from dozer.profile import buildtree, write_dot_graph
#write_dot_graph(self.profiler.getstats(), buildtree(self.profiler.getstats()), "/tmp/output.gv")
self.profiler.print_stats(1)
sys.stdout = old_stdout
response.response = [u'<pre>%s</pre>' % to_unicode(out.getvalue())]
response.content_type = 'text/html'