On Tuesday, October 18, 2016 at 9:29:37 AM UTC-4, Mike Bayer wrote:
This approach can also be made extremely performant by making sure you > load up that profile datafile just once, and cache it in memory in a > dictionary. > What's also nice about the decorator approach in the aspect, is that you can use it as a factory and disable/enable it on startup to eliminate runtime performance issues when not needed: def monitor_sqlcalls(wrapped): if DO_NOT_DECORATE: return wrapped def wrapper(): result = wrapped() return result return wrapper @monitor_sqlcalls def foo(): return True If you toggle it off, you'll only have a performance hit on the application startup, while Python compiles the functions and returns the underlying function, essentially leaving it undecorated. This can let you decorate profiling code on everything you want, then only run it on a few select deployments / processes. If you really want to shave off some milliseconds and are using cPython, you can set the env variable `PYTHONOPTIMIZE` to change the `__debug__` constant from True to False (or invoke Python with `-o/-oo`), and nest any random debugging work below it. Anything in an `if __debug__` block will only be compiled if PYTHONOPTIMIZE is unset. The compiler only handles a very simple `if __debug__:` construct. 'if not __debug__` needs to be written as `if __debug__: pass` + `else:` If you want to run something as and/or `__debug__`, you'll need to split that into two if statements. Another trick is to log stuff using `statsd` from Etsy. It's an external daemon written in node.js, and clients send data to it via UDP. We have a handful of functions wrapped in statsd decorators that track timing; some track average results or number of postgres interactions. The uwsgi containers also report into statsd, allowing us to easily correlate everything to server loads across the network at the time. The upside is that there is almost no overhead, the downside is that this performance stems from UDP transmissions not being guaranteed of receipt. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.