U a"@sdZddlZddlZddlZddlZddlmZzddl m Z Wn e k r`ddl m Z YnXej rddlmZddlmZddlmZGdd d ZdS) a9 Application Profiler ==================== This module provides a middleware that profiles each request with the :mod:`cProfile` module. This can help identify bottlenecks in your code that may be slowing down your application. .. autoclass:: ProfilerMiddleware :copyright: 2007 Pallets :license: BSD-3-Clause N)Stats)Profile) StartResponse)WSGIApplication)WSGIEnvironmentc @speZdZdZejddddfdejeje ejej e e e fej e e dddd Zd d ejed d dZdS)ProfilerMiddlewareaWrap a WSGI application and profile the execution of each request. Responses are buffered so that timings are more exact. If ``stream`` is given, :class:`pstats.Stats` are written to it after each request. If ``profile_dir`` is given, :mod:`cProfile` data files are saved to that directory, one file per request. The filename can be customized by passing ``filename_format``. If it is a string, it will be formatted using :meth:`str.format` with the following fields available: - ``{method}`` - The request method; GET, POST, etc. - ``{path}`` - The request path or 'root' should one not exist. - ``{elapsed}`` - The elapsed time of the request. - ``{time}`` - The time of the request. If it is a callable, it will be called with the WSGI ``environ`` dict and should return a filename. :param app: The WSGI application to wrap. :param stream: Write stats to this stream. Disable with ``None``. :param sort_by: A tuple of columns to sort stats by. See :meth:`pstats.Stats.sort_stats`. :param restrictions: A tuple of restrictions to filter stats by. See :meth:`pstats.Stats.print_stats`. :param profile_dir: Save profile data files to this directory. :param filename_format: Format string for profile data file names, or a callable returning a name. See explanation above. .. code-block:: python from werkzeug.middleware.profiler import ProfilerMiddleware app = ProfilerMiddleware(app) .. versionchanged:: 0.15 Stats are written even if ``profile_dir`` is given, and can be disable by passing ``stream=None``. .. versionadded:: 0.15 Added ``filename_format``. .. versionadded:: 0.9 Added ``restrictions`` and ``profile_dir``. )timeZcallsNz/{method}.{path}.{elapsed:.0f}ms.{time:.0f}.profr)appstreamsort_by restrictions profile_dirfilename_formatreturncCs(||_||_||_||_||_||_dSN)_app_stream_sort_by _restrictions _profile_dir_filename_format)selfr r r r rrr r ]C:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-1tps7o9u\werkzeug\middleware\profiler.py__init__Ms zProfilerMiddleware.__init__rr)environstart_responserc sDgdfdd ddfdd }t}t}||d}t|}jdk rtjrz}n6jjddd  d d pd |d td }t j j|}| |j dk r>t|j d} | jjtdj ddd} td| j d| jjtddj d|gS)Ncs|||jSr)append)statusheadersexc_info) response_bodyrr rcatching_start_responsebs z.catching_start_response)rcs4td}|t|dr0|dS)Nrclose)rtcastextendhasattrr#)Zapp_iter)r"rr!rr rrunappfs   z+ProfilerMiddleware.__call__..runappREQUEST_METHODZ PATH_INFO/.rootg@@)methodpathelapsedr)r zP--------------------------------------------------------------------------------)filezPATH:  )N)rrZruncalljoinrcallablerformatstripreplaceosr/Z dump_statsrrZ sort_statsrprintgetZ print_statsr) rrrr(profilestartbodyr0filenamestatsZ path_infor )r"rr!rrr__call__]s8            zProfilerMiddleware.__call__)__name__ __module__ __qualname____doc__sysstdoutr$TextIOIterablestrUnionintfloatOptionalrbytesrAr r r rrs&0 r)rEos.pathr9rFrtypingr$ZpstatsrZcProfiler ImportErrorr< TYPE_CHECKINGZ_typeshed.wsgirrrrr r r rs