# 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)
~~~
- 开始
- Werkzeug 文档概览
- 安装
- 过渡到 Werkzeug 1.0
- Werkzeug 教程
- API 标准
- 快速开始
- Python 3 Notes
- 服务和测试
- Debugging Applications
- 在服务器运行 WSGI 应用
- 单元测试
- 参考
- Request / Response Objects
- URL Routing
- WSGI Helpers
- HTTP Utilities
- Data Structures
- Utilities
- Context Locals
- Middlewares
- HTTP Exceptions
- 部署
- CGI
- mod_wsgi (Apache)
- FastCGI
- HTTP Proxying
- 贡献模块
- Atom Syndication
- Sessions
- Secure Cookie
- Cache
- Extra Wrappers
- Iter IO
- Fixers
- WSGI Application Profiler
- Lint Validation Middleware
- 额外说明
- Werkzeug Changelog
- Important Terms
- Unicode
- Dealing with Request Data