# HG changeset patch # User Pawel Solyga # Date 1233093392 0 # Node ID b9d51be5104a53c4509684b173a0b0b6184c1ff0 # Parent 508ad99368e28d1c696de0902ff4ef7d19b88596 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 diff -r 508ad99368e2 -r b9d51be5104a 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
' + print '

Profile data

' + print '
'
+  print stream.getvalue()[:1000000]
+  print '
' + + +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()