WSGI Application Profiler

最后更新于:2022-04-01 04:08:11

# WSGI Application Profiler This module provides a simple WSGI profiler middleware for findingbottlenecks in web application. It uses the [profile](http://docs.python.org/dev/library/profile.html#module-profile "(在 Python v3.5)") or[cProfile](http://docs.python.org/dev/library/profile.html#module-cProfile "(在 Python v3.5)") module to do the profiling and writes the stats to thestream provided (defaults to stderr). Example usage: ~~~ from werkzeug.contrib.profiler import ProfilerMiddleware app = ProfilerMiddleware(app) ~~~ *class *werkzeug.contrib.profiler.MergeStream(**streams*) An object that redirects write calls to multiple streams.Use this to log to both sys.stdout and a file: ~~~ f = open('profiler.log', 'w') stream = MergeStream(sys.stdout, f) profiler = ProfilerMiddleware(app, stream) ~~~ *class *werkzeug.contrib.profiler.ProfilerMiddleware(*app*, *stream=None*, *sort_by=('time'*, *'calls')*, *restrictions=()*, *profile_dir=None*) Simple profiler middleware. Wraps a WSGI application and profilesa request. This intentionally buffers the response so that timings aremore exact. By giving the profile_dir argument, pstat.Stats files are saved to thatdirectory, one file per request. Without it, a summary is printed tostream instead. For the exact meaning of sort_by and restrictions consult the[profile](http://docs.python.org/dev/library/profile.html#module-profile "(在 Python v3.5)") documentation. 0.9 新版功能: Added support for restrictions and profile_dir. <table class="docutils field-list" frame="void" rules="none"><col class="field-name"/><col class="field-body"/><tbody valign="top"><tr class="field-odd field"><th class="field-name">参数:</th><td class="field-body"><ul class="first last simple"><li><strong>app</strong> – the WSGI application to profile.</li><li><strong>stream</strong> – the stream for the profiled stats. defaults to stderr.</li><li><strong>sort_by</strong> – a tuple of columns to sort the result by.</li><li><strong>restrictions</strong> – a tuple of profiling strictions, not used if dumpingto <cite>profile_dir</cite>.</li><li><strong>profile_dir</strong> – directory name to save pstat files</li></ul></td></tr></tbody></table> werkzeug.contrib.profiler.make_action(*app_factory*, *hostname='localhost'*, *port=5000*, *threaded=False*, *processes=1*, *stream=None*, *sort_by=('time'*, *'calls')*, *restrictions=()*) Return a new callback for werkzeug.script that starts a localserver with the profiler enabled. ~~~ from werkzeug.contrib import profiler action_profile = profiler.make_action(make_app) ~~~
';