pyramid_cubicweb/profile.py
changeset 11537 caf268942436
parent 11535 dd875009cc47
child 11630 1400aee10df4
equal deleted inserted replaced
11536:6618408c0629 11537:caf268942436
       
     1 """ Tools for profiling.
       
     2 
       
     3 See :ref:`profiling`."""
     1 import cProfile
     4 import cProfile
     2 import itertools
     5 import itertools
     3 from pyramid.view import view_config
     6 from pyramid.view import view_config
     4 
     7 
     5 
     8 
     6 @view_config(route_name='profile_ping')
     9 @view_config(route_name='profile_ping')
     7 def ping(request):
    10 def ping(request):
       
    11     """ View that handle '/_profile/ping'
       
    12 
       
    13     It simply reply 'ping', without requiring connection to the repository.
       
    14     It is a useful as a comparison point to evaluate the actual overhead of
       
    15     more costly views.
       
    16     """
     8     request.response.text = u'pong'
    17     request.response.text = u'pong'
     9     return request.response
    18     return request.response
    10 
    19 
    11 
    20 
    12 @view_config(route_name='profile_cnx')
    21 @view_config(route_name='profile_cnx')
    13 def cnx(request):
    22 def cnx(request):
       
    23     """ View that handle '/_profile/cnx'
       
    24 
       
    25     Same as :func:`ping`, but it first ask for a connection to the repository.
       
    26     Useful to evaluate the overhead of opening a connection.
       
    27     """
    14     request.cw_cnx
    28     request.cw_cnx
    15     request.response.text = u'pong'
    29     request.response.text = u'pong'
    16     return request.response
    30     return request.response
    17 
    31 
    18 
    32 
    19 def wsgi_profile(app, filename='program.prof', dump_every=50):
    33 def wsgi_profile(app, filename='program.prof', dump_every=50):
       
    34     """ A WSGI middleware for profiling
       
    35 
       
    36     It enable the profiler before passing the request to the underlying
       
    37     application, and disable it just after.
       
    38 
       
    39     The stats will be dumped after ``dump_every`` requests
       
    40 
       
    41     :param filename: The filename to dump the stats to.
       
    42     :param dump_every: Number of requests after which to dump the stats.
       
    43     """
       
    44 
    20     profile = cProfile.Profile()
    45     profile = cProfile.Profile()
    21 
    46 
    22     counter = itertools.count(1)
    47     counter = itertools.count(1)
    23 
    48 
    24     def application(environ, start_response):
    49     def application(environ, start_response):