Application Profiler

This module provides a middleware that profiles each request with thecProfile module. This can help identify bottlenecks in your codethat may be slowing down your application.

  • class werkzeug.middleware.profiler.ProfilerMiddleware(app, stream=<_io.TextIOWrapper name='' mode='w' encoding='UTF-8'>, sort_by=('time', 'calls'), restrictions=(), profile_dir=None, filename_format='{method}.{path}.{elapsed:.0f}ms.{time:.0f}.prof')
  • Wrap a WSGI application and profile the execution of eachrequest. Responses are buffered so that timings are more exact.

If stream is given, pstats.Stats are written to itafter each request. If profile_dir is given, cProfiledata files are saved to that directory, one file per request.

The filename can be customized by passing filename_format. Ifit is a string, it will be formatted using str.format() withthe 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 environdict and should return a filename.

Parameters:

  • app – The WSGI application to wrap.
  • stream – Write stats to this stream. Disable with None.
  • sort_by – A tuple of columns to sort stats by. Seepstats.Stats.sort_stats().
  • restrictions – A tuple of restrictions to filter stats by. Seepstats.Stats.print_stats().
  • profile_dir – Save profile data files to this directory.
  • filename_format – Format string for profile data file names,or a callable returning a name. See explanation above.
  1. from werkzeug.middleware.profiler import ProfilerMiddleware
  2. app = ProfilerMiddleware(app)

Changed in version 0.15: Stats are written even if profile_dir is given, and can bedisable by passing stream=None.

New in version 0.15: Added filename_format.

New in version 0.9: Added restrictions and profile_dir.