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.
authorPawel Solyga <Pawel.Solyga@gmail.com>
Tue, 27 Jan 2009 21:56:32 +0000
changeset 1015 b9d51be5104a
parent 1014 508ad99368e2
child 1016 15a2f644725f
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
app/main.py
--- 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()