cubicweb/pyramid/profile.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Mon, 30 Mar 2020 15:30:02 +0200
changeset 12961 01810941a4be
parent 12567 26744ad37953
permissions -rw-r--r--
[server] use a LifoQueue in _CnxSetPool In postgresql, some cache is attached to the connection. Using a LifoQueue (last-in, first-out) makes a few connections to get the most load which give best performance.
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
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    25
import cProfile
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    26
import itertools
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
    27
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
from pyramid.view import view_config
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
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
@view_config(route_name='profile_ping')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
def ping(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    33
    """ View that handle '/_profile/ping'
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    34
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    35
    It simply reply 'ping', without requiring connection to the repository.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    36
    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
    37
    more costly views.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    38
    """
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
    request.response.text = u'pong'
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
    return request.response
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    41
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
@view_config(route_name='profile_cnx')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    44
def cnx(request):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    45
    """ View that handle '/_profile/cnx'
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
    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
    48
    Useful to evaluate the overhead of opening a connection.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    49
    """
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    50
    request.cw_cnx
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
    request.response.text = u'pong'
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
    return request.response
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
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
def wsgi_profile(app, filename='program.prof', dump_every=50):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    56
    """ A WSGI middleware for profiling
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    57
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    58
    It enable the profiler before passing the request to the underlying
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    59
    application, and disable it just after.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    60
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    61
    The stats will be dumped after ``dump_every`` requests
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    62
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    63
    :param filename: The filename to dump the stats to.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    64
    :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
    65
    """
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    66
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    67
    profile = cProfile.Profile()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    68
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    69
    counter = itertools.count(1)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    70
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    71
    def application(environ, start_response):
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    72
        profile.enable()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    73
        try:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    74
            return app(environ, start_response)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    75
        finally:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    76
            profile.disable()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    77
            if not counter.next() % dump_every:
11630
1400aee10df4 Port to Python3 (closes #14159555)
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11537
diff changeset
    78
                print("Dump profile stats to %s" % filename)
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    79
                profile.create_stats()
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    80
                profile.dump_stats(filename)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    81
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    82
    return application