Commits

Alessio Deiana  committed 28dcb9a

Profiling of imports

Ability to print stats to be able to tell what slows done the startup
of invenio.

  • Participants
  • Parent commits 4ff07d4
  • Branches profiling-imports

Comments (0)

Files changed (2)

File src/invenio_devserver/config.py

 USE_CONSOLE = False
 
 
+# Display profiling stats after import to check what takes a lot
+# of time to import
+ENABLE_IMPORT_PROFILER = False
+
 try:
     import config_local
     for setting in dir(config_local):

File src/invenio_devserver/wsgi.py

+"""Invenio Connector
+
+We import invenio from here so that we are sure that invenio is fully
+reloaded when forking workers
+"""
+
 import traceback
 
+from invenio_devserver.config import ENABLE_IMPORT_PROFILER
+
 
 def replace_error_handler():
     from invenio import errorlib
 
-    def error_handler(stream='error',
-                      req=None,
-                      prefix='',
-                      suffix='',
-                      alert_admin=False,
-                      subject=''):
+    def cb_error_handler(stream='error',
+                        req=None,
+                        prefix='',
+                        suffix='',
+                        alert_admin=False,
+                        subject=''):
         print traceback.format_exc()[:-1]
 
-    errorlib.register_exception = error_handler
+    errorlib.register_exception = cb_error_handler
 
 
 def wrap_warn():
     warnings.warn = wrapper(warnings.warn)
 
 
+if ENABLE_IMPORT_PROFILER:
+    import cProfile
+    import pstats
+    PROFILER = cProfile.Profile()
+    PROFILER.enable()
+
 try:
     from invenio.webinterface_handler_flask import create_invenio_flask_app
 except ImportError:
-    from invenio.webinterface_handler_wsgi import application
+    from invenio.webinterface_handler_wsgi import application  # pylint: disable-msg=W0611
 else:
-    application = create_invenio_flask_app()
+    application = create_invenio_flask_app()  # pylint: disable-msg=C0103
+
+if ENABLE_IMPORT_PROFILER:
+    PROFILER.disable()
+    PSTATS = pstats.Stats(PROFILER)
+    PSTATS = PSTATS.strip_dirs()
+    PSTATS = PSTATS.sort_stats('cumtime', 'tottime')
+    PSTATS.print_stats(30)