Advertisement
matiascelasco

Some profiling helpers for Python and Django

Oct 31st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. import time
  2. from django.db import connection
  3. from django.conf import settings
  4. from contextlib import contextmanager
  5.  
  6.  
  7. def timer(what=None):
  8.     """
  9.    This decorator count the number of seconds
  10.    it takes to run the decorated function.
  11.    It also can be used as a context manager
  12.    """
  13.  
  14.     # ignore in production
  15.     if not settings.DEBUG:
  16.         return what
  17.  
  18.     @contextmanager
  19.     def benchmark(name):
  20.         start_time = time.time()
  21.         yield
  22.         secs = time.time() - start_time
  23.         print 'execution of %s: %.2f seconds' % (name, secs)
  24.  
  25.     if hasattr(what, "__call__"):
  26.         def wrapped_function(*args, **kwargs):
  27.             with benchmark('function "%s"' % what.__name__):
  28.                 return what(*args, **kwargs)
  29.         wrapped_function.__doc__ = what.__doc__
  30.         wrapped_function.__name__ = what.__name__
  31.         return wrapped_function
  32.     else:
  33.         return benchmark(('block "%s"' % what) if what else "anonymous block")
  34.  
  35.  
  36. def queries_counter(what=None):
  37.     """
  38.    This decorator count the number of database queries
  39.    performed by Django during the execution of the decorated function.
  40.    It also can be used as a context manager.
  41.    """
  42.  
  43.     # ignore in production
  44.     if not settings.DEBUG:
  45.         return what
  46.  
  47.     @contextmanager
  48.     def benchmark(name):
  49.         num_queries_before = len(connection.queries)
  50.         yield
  51.         num_queries = len(connection.queries) - num_queries_before
  52.         print 'execution of %s: %d queries performed' % (name, num_queries)
  53.  
  54.     if hasattr(what, "__call__"):
  55.         def wrapped_function(*args, **kwargs):
  56.             with benchmark('function "%s"' % what.__name__):
  57.                 return what(*args, **kwargs)
  58.         wrapped_function.__doc__ = what.__doc__
  59.         wrapped_function.__name__ = what.__name__
  60.         return wrapped_function
  61.     else:
  62.         return benchmark(('block "%s"' % what) if what else "anonymous block")
  63.  
  64. # Usage:
  65. # @timer            
  66. # def my_function():
  67. #     x = 0
  68. #     for i in range(1, 100000):
  69. #         x += i
  70. #     return x
  71. #
  72. # >>> print my_function()
  73. # execution of function "my_function": 0.02 seconds
  74. # 4999950000
  75.  
  76.  
  77. # >>> with timer():
  78. # >>>     x = 0
  79. # >>>     for i in range(100000):
  80. # >>>         x += i
  81. #        
  82. # execution of anonymous block: 0.01 seconds
  83.  
  84.  
  85. # >>> with timer("my block"):
  86. # >>>     x = 0
  87. # >>>     for i in range(100000):
  88. # >>>         x += i
  89. #
  90. # execution of block "my block": 0.01 seconds
  91.  
  92.  
  93. # >>> with queries_counter():
  94. # >>>     print Country.objects.count()
  95. #        
  96. # execution of anonymous block: 1 queries performed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement