;idZddlmZddlZddlZddlZddlZddl m Z  ddl m Z n#e $r ddlm Z YnwxYwejrddlmZddlmZddlmZGd 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 ) annotationsN)Stats)Profile) StartResponse)WSGIApplication)WSGIEnvironmentc8eZdZdZejddddfddZddZdS)ProfilerMiddlewarea Wrap 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 in milliseconds. - ``{time}`` - The time of the request. If it is a callable, it will be called with the WSGI ``environ`` and be expected to return a filename string. The ``environ`` dictionary will also have the ``"werkzeug.profiler"`` key populated with a dictionary containing the following fields (more may be added in the future): - ``{elapsed}`` - The elapsed time of the request in milliseconds. - ``{time}`` - The time of the request. :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:: 3.0 Added the ``"werkzeug.profiler"`` key to the ``filename_format(environ)`` parameter with the ``elapsed`` and ``time`` fields. .. 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``. )timecallsNz/{method}.{path}.{elapsed:.0f}ms.{time:.0f}.profapprstreamt.IO[str] | Nonesort_byt.Iterable[str] restrictionst.Iterable[str | int | float] profile_dir str | Nonefilename_formatstrreturnNonecZ||_||_||_||_||_||_dSN)_app_stream_sort_by _restrictions _profile_dir_filename_format)selfrrrrrrs PC:\PYTHON\MyICR_Workspace\venv\Lib\site-packages\werkzeug/middleware/profiler.py__init__zProfilerMiddleware.__init__Ys7   )' /environrstart_responsert.Iterable[bytes]c g d fd d  fd }t}tj}||d }tj|z }jt jr2|dztjdd<}ngjd d d  d d pd |dztj}tj j|}| |j t|j } | jjt#dj d d} t#d| j | jjt#ddj |gS)Nc,|||jSr)append)statusheadersexc_info response_bodyr(s r$catching_start_responsez.catching_start_responsens N67H 5 5 5 ' 'r&rrctjd}|t |dr|dSdS)Nrclose)rtcastextendhasattrr3)app_iterr1r'r0r#s r$runappz+ProfilerMiddleware.__call__..runapprspyy1HIIH   * * *x)) !      ! !r&r&g@@)elapsedr zwerkzeug.profilerREQUEST_METHOD PATH_INFO/.root)methodpathr:r )rzP--------------------------------------------------------------------------------)filezPATH:  r)rr)rr runcalljoinr!callabler"formatstripreplaceosrA dump_statsrr sort_statsrprintget print_statsr ) r#r'r(r9profilestartbodyr:filenamestats path_infor1r0s ``` @@r$__call__zProfilerMiddleware.__call__is6&(  ( ( ( ( ( ( ( ! ! ! ! ! ! ! ! !)) xx &&)++%   (-.. &/ IKK00+, 0099077"#34 -33C88@@cJJTf#f, 8 w||D$5x@@H   x ( ( ( < #'$,777E E dm , , ( . . . . K44I (9((t| < < < < E t1 2 2 X/// 5 5 5 5v r&)rrrrrrrrrrrrrr)r'rr(rrr))__name__ __module__ __qualname____doc__sysstdoutr%rWr r&r$r r "s_44r$':#468"&P00000 222222r&r )r[ __future__ros.pathrKr\r typingr4pstatsrcProfiler ImportErrorrQ TYPE_CHECKING_typeshed.wsgirrrr r r&r$rfs'  #"""""             ?/,,,,,,............yyyyyyyyyys ' 55