def startup(manager, options): if not options.debug_perf: return log.info('Enabling plugin and SQLAlchemy performance debugging') global query_count, orig_execute query_count = 0 # Monkeypatch query counter for SQLAlchemy if hasattr(Connection, 'execute'): orig_execute = Connection.execute def monkeypatched(*args, **kwargs): global query_count query_count += 1 return orig_execute(*args, **kwargs) Connection.execute = monkeypatched else: log.critical('Unable to monkeypatch sqlalchemy') add_event_handler('task.execute.before_plugin', before_plugin) add_event_handler('task.execute.after_plugin', after_plugin)