pyramid_cubicweb/profile.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Thu, 07 Jul 2016 14:30:32 +0200
changeset 11630 1400aee10df4
parent 11537 caf268942436
permissions -rw-r--r--
Port to Python3 (closes #14159555) Add py34 environments to tox configuration (only for CubicWeb >= 3.23). And depend on hg version of cubicweb-pyramid since it is not currently Python3-compatible.
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`."""
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
     4
from __future__ import print_function
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
     5
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     6
import cProfile
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     7
import itertools
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
     8
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
     9
from pyramid.view import view_config
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    10
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    11
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    12
@view_config(route_name='profile_ping')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    13
def ping(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    14
    """ View that handle '/_profile/ping'
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    15
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    16
    It simply reply 'ping', without requiring connection to the repository.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    17
    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
    18
    more costly views.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    19
    """
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    20
    request.response.text = u'pong'
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    21
    return request.response
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    22
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    23
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
@view_config(route_name='profile_cnx')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
def cnx(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    26
    """ View that handle '/_profile/cnx'
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    27
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    28
    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
    29
    Useful to evaluate the overhead of opening a connection.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    30
    """
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
    request.cw_cnx
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
    request.response.text = u'pong'
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
    return request.response
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
def wsgi_profile(app, filename='program.prof', dump_every=50):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    37
    """ A WSGI middleware for profiling
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
    It enable the profiler before passing the request to the underlying
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    40
    application, and disable it just after.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    41
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    42
    The stats will be dumped after ``dump_every`` requests
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
    :param filename: The filename to dump the stats to.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    45
    :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
    46
    """
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    47
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
    profile = cProfile.Profile()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    50
    counter = itertools.count(1)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
    def application(environ, start_response):
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
        profile.enable()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
        try:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
            return app(environ, start_response)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
        finally:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    57
            profile.disable()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    58
            if not counter.next() % dump_every:
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
    59
                print("Dump profile stats to %s" % filename)
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    60
                profile.create_stats()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    61
                profile.dump_stats(filename)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    62
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    63
    return application