pyramid_cubicweb/profile.py
author Christophe de Vienne <christophe@unlish.com>
Sat, 03 Jan 2015 22:06:03 +0100
changeset 11537 caf268942436
parent 11535 dd875009cc47
child 11630 1400aee10df4
permissions -rw-r--r--
Initial documentation. Closes #4849313
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
     1
""" Tools for profiling.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
     2
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
     3
See :ref:`profiling`."""
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     4
import cProfile
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     5
import itertools
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
from pyramid.view import view_config
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     8
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
@view_config(route_name='profile_ping')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
def ping(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    11
    """ View that handle '/_profile/ping'
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    12
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    13
    It simply reply 'ping', without requiring connection to the repository.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    14
    It is a useful as a comparison point to evaluate the actual overhead of
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    15
    more costly views.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    16
    """
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    17
    request.response.text = u'pong'
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    18
    return request.response
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    19
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
@view_config(route_name='profile_cnx')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
def cnx(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    23
    """ View that handle '/_profile/cnx'
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    24
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    25
    Same as :func:`ping`, but it first ask for a connection to the repository.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    26
    Useful to evaluate the overhead of opening a connection.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    27
    """
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
    request.cw_cnx
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
    request.response.text = u'pong'
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
    return request.response
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
def wsgi_profile(app, filename='program.prof', dump_every=50):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    34
    """ A WSGI middleware for profiling
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    35
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    36
    It enable the profiler before passing the request to the underlying
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    37
    application, and disable it just after.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    38
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    39
    The stats will be dumped after ``dump_every`` requests
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    40
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    41
    :param filename: The filename to dump the stats to.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    42
    :param dump_every: Number of requests after which to dump the stats.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    43
    """
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    44
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    45
    profile = cProfile.Profile()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    46
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
    counter = itertools.count(1)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
    def application(environ, start_response):
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    50
        profile.enable()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
        try:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
            return app(environ, start_response)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
        finally:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
            profile.disable()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
            if not counter.next() % dump_every:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
                print "Dump profile stats to %s" % filename
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    57
                profile.create_stats()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    58
                profile.dump_stats(filename)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    59
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    60
    return application