cubicweb/pyramid/profile.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Fri, 17 Mar 2017 16:29:38 +0100
branch3.25
changeset 12074 f5fee548588d
parent 11967 83739be20fab
child 12567 26744ad37953
permissions -rw-r--r--
[pkg] Version 3.25.0rc0 Let's make a release candidate version of CubicWeb 3.25.0 (only for the Python package), in the hope that some issues that we usually discover after release will be sorted out before.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11967
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     1
# copyright 2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     2
# copyright 2014-2016 UNLISH S.A.S. (Montpellier, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     3
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     4
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     5
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     6
# This file is part of CubicWeb.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     7
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     8
# CubicWeb is free software: you can redistribute it and/or modify it under the
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
     9
# terms of the GNU Lesser General Public License as published by the Free
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    10
# Software Foundation, either version 2.1 of the License, or (at your option)
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    11
# any later version.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    12
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    13
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    15
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    16
# details.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    17
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    18
# You should have received a copy of the GNU Lesser General Public License along
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    19
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11631
diff changeset
    20
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    21
""" Tools for profiling.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    22
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    23
See :ref:`profiling`."""
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
    24
from __future__ import print_function
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
    25
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
import cProfile
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    27
import itertools
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
    28
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
from pyramid.view import view_config
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
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
@view_config(route_name='profile_ping')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
def ping(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    34
    """ View that handle '/_profile/ping'
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 simply reply 'ping', without requiring connection to the repository.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    37
    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
    38
    more costly views.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    39
    """
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
    request.response.text = u'pong'
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    41
    return request.response
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    42
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    43
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    44
@view_config(route_name='profile_cnx')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    45
def cnx(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    46
    """ View that handle '/_profile/cnx'
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    47
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    48
    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
    49
    Useful to evaluate the overhead of opening a connection.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    50
    """
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
    request.cw_cnx
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
    request.response.text = u'pong'
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
    return request.response
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
def wsgi_profile(app, filename='program.prof', dump_every=50):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    57
    """ A WSGI middleware for profiling
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    58
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    59
    It enable the profiler before passing the request to the underlying
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    60
    application, and disable it just after.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    61
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    62
    The stats will be dumped after ``dump_every`` requests
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    63
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    64
    :param filename: The filename to dump the stats to.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    65
    :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
    66
    """
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    67
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    68
    profile = cProfile.Profile()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    69
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    70
    counter = itertools.count(1)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    71
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    72
    def application(environ, start_response):
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    73
        profile.enable()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    74
        try:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    75
            return app(environ, start_response)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    76
        finally:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    77
            profile.disable()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    78
            if not counter.next() % dump_every:
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
    79
                print("Dump profile stats to %s" % filename)
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    80
                profile.create_stats()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    81
                profile.dump_stats(filename)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    82
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    83
    return application