Add profiling support to Melange. By assigning profile_main_as_logs or profile_main_as_html to main variable you can turn on profiling. profile_main_as_logs will log profile data to App Engine console logs, profile_main_as_html will show profile data as html at the bottom of the page. If you want to profile app on deployed app just set the profiling function and deploy it.
Patch by: Pawel Solyga
Reviewed by: to-be-reviewed
--- a/app/main.py Tue Jan 27 20:30:26 2009 +0000
+++ b/app/main.py Tue Jan 27 21:56:32 2009 +0000
@@ -62,7 +62,56 @@
django.db._rollback_on_exception)
-def main():
+def profile_main_as_html():
+ """Main program for profiling. Profiling data added as HTML to the page.
+ """
+ import cProfile
+ import pstats
+ import StringIO
+
+ prof = cProfile.Profile()
+ prof = prof.runctx('real_main()', globals(), locals())
+ stream = StringIO.StringIO()
+ stats = pstats.Stats(prof, stream=stream)
+ # stats.strip_dirs() # Don't; too many modules are named __init__.py.
+
+ # 'time', 'cumulative' or 'calls'
+ stats.sort_stats('time')
+
+ # Optional arg: how many to print
+ stats.print_stats()
+ # The rest is optional.
+ # stats.print_callees()
+ # stats.print_callers()
+ print '\n<hr>'
+ print '<h1>Profile data</h1>'
+ print '<pre>'
+ print stream.getvalue()[:1000000]
+ print '</pre>'
+
+
+def profile_main_as_logs():
+ """Main program for profiling. Profiling data logged.
+ """
+ import cProfile
+ import pstats
+ import StringIO
+
+ prof = cProfile.Profile()
+ prof = prof.runctx("real_main()", globals(), locals())
+ stream = StringIO.StringIO()
+ stats = pstats.Stats(prof, stream=stream)
+ stats.sort_stats('time') # Or cumulative
+ stats.print_stats(80) # 80 = how many to print
+ # The rest is optional.
+ # stats.print_callees()
+ # stats.print_callers()
+ logging.info("Profile data:\n%s", stream.getvalue())
+
+
+def real_main():
+ """Main program without profiling.
+ """
global ultimate_sys_path
if ultimate_sys_path is None:
ultimate_sys_path = list(sys.path)
@@ -75,6 +124,7 @@
# Run the WSGI CGI handler with that application.
util.run_wsgi_app(application)
+main = real_main
if __name__ == '__main__':
main()