profiler.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. """
  2. Application Profiler
  3. ====================
  4. This module provides a middleware that profiles each request with the
  5. :mod:`cProfile` module. This can help identify bottlenecks in your code
  6. that may be slowing down your application.
  7. .. autoclass:: ProfilerMiddleware
  8. :copyright: 2007 Pallets
  9. :license: BSD-3-Clause
  10. """
  11. from __future__ import annotations
  12. import os.path
  13. import sys
  14. import time
  15. import typing as t
  16. from pstats import Stats
  17. try:
  18. from cProfile import Profile
  19. except ImportError:
  20. from profile import Profile # type: ignore
  21. if t.TYPE_CHECKING:
  22. from _typeshed.wsgi import StartResponse
  23. from _typeshed.wsgi import WSGIApplication
  24. from _typeshed.wsgi import WSGIEnvironment
  25. class ProfilerMiddleware:
  26. """Wrap a WSGI application and profile the execution of each
  27. request. Responses are buffered so that timings are more exact.
  28. If ``stream`` is given, :class:`pstats.Stats` are written to it
  29. after each request. If ``profile_dir`` is given, :mod:`cProfile`
  30. data files are saved to that directory, one file per request.
  31. The filename can be customized by passing ``filename_format``. If
  32. it is a string, it will be formatted using :meth:`str.format` with
  33. the following fields available:
  34. - ``{method}`` - The request method; GET, POST, etc.
  35. - ``{path}`` - The request path or 'root' should one not exist.
  36. - ``{elapsed}`` - The elapsed time of the request in milliseconds.
  37. - ``{time}`` - The time of the request.
  38. If it is a callable, it will be called with the WSGI ``environ`` and
  39. be expected to return a filename string. The ``environ`` dictionary
  40. will also have the ``"werkzeug.profiler"`` key populated with a
  41. dictionary containing the following fields (more may be added in the
  42. future):
  43. - ``{elapsed}`` - The elapsed time of the request in milliseconds.
  44. - ``{time}`` - The time of the request.
  45. :param app: The WSGI application to wrap.
  46. :param stream: Write stats to this stream. Disable with ``None``.
  47. :param sort_by: A tuple of columns to sort stats by. See
  48. :meth:`pstats.Stats.sort_stats`.
  49. :param restrictions: A tuple of restrictions to filter stats by. See
  50. :meth:`pstats.Stats.print_stats`.
  51. :param profile_dir: Save profile data files to this directory.
  52. :param filename_format: Format string for profile data file names,
  53. or a callable returning a name. See explanation above.
  54. .. code-block:: python
  55. from werkzeug.middleware.profiler import ProfilerMiddleware
  56. app = ProfilerMiddleware(app)
  57. .. versionchanged:: 3.0
  58. Added the ``"werkzeug.profiler"`` key to the ``filename_format(environ)``
  59. parameter with the ``elapsed`` and ``time`` fields.
  60. .. versionchanged:: 0.15
  61. Stats are written even if ``profile_dir`` is given, and can be
  62. disable by passing ``stream=None``.
  63. .. versionadded:: 0.15
  64. Added ``filename_format``.
  65. .. versionadded:: 0.9
  66. Added ``restrictions`` and ``profile_dir``.
  67. """
  68. def __init__(
  69. self,
  70. app: WSGIApplication,
  71. stream: t.IO[str] | None = sys.stdout,
  72. sort_by: t.Iterable[str] = ("time", "calls"),
  73. restrictions: t.Iterable[str | int | float] = (),
  74. profile_dir: str | None = None,
  75. filename_format: str = "{method}.{path}.{elapsed:.0f}ms.{time:.0f}.prof",
  76. ) -> None:
  77. self._app = app
  78. self._stream = stream
  79. self._sort_by = sort_by
  80. self._restrictions = restrictions
  81. self._profile_dir = profile_dir
  82. self._filename_format = filename_format
  83. def __call__(
  84. self, environ: WSGIEnvironment, start_response: StartResponse
  85. ) -> t.Iterable[bytes]:
  86. response_body: list[bytes] = []
  87. def catching_start_response(status, headers, exc_info=None): # type: ignore
  88. start_response(status, headers, exc_info)
  89. return response_body.append
  90. def runapp() -> None:
  91. app_iter = self._app(
  92. environ, t.cast("StartResponse", catching_start_response)
  93. )
  94. response_body.extend(app_iter)
  95. if hasattr(app_iter, "close"):
  96. app_iter.close()
  97. profile = Profile()
  98. start = time.time()
  99. profile.runcall(runapp)
  100. body = b"".join(response_body)
  101. elapsed = time.time() - start
  102. if self._profile_dir is not None:
  103. if callable(self._filename_format):
  104. environ["werkzeug.profiler"] = {
  105. "elapsed": elapsed * 1000.0,
  106. "time": time.time(),
  107. }
  108. filename = self._filename_format(environ)
  109. else:
  110. filename = self._filename_format.format(
  111. method=environ["REQUEST_METHOD"],
  112. path=environ["PATH_INFO"].strip("/").replace("/", ".") or "root",
  113. elapsed=elapsed * 1000.0,
  114. time=time.time(),
  115. )
  116. filename = os.path.join(self._profile_dir, filename)
  117. profile.dump_stats(filename)
  118. if self._stream is not None:
  119. stats = Stats(profile, stream=self._stream)
  120. stats.sort_stats(*self._sort_by)
  121. print("-" * 80, file=self._stream)
  122. path_info = environ.get("PATH_INFO", "")
  123. print(f"PATH: {path_info!r}", file=self._stream)
  124. stats.print_stats(*self._restrictions)
  125. print(f"{'-' * 80}\n", file=self._stream)
  126. return [body]