Using profiling to find bottlenecks
Profiling is an important and handy tool to measure performance when we decide to scale an application. Before scaling, we want to know whether any process is a bottleneck and affects the overall performance. Python has a built-in profiler, cProfile
, that can do the job for us, but to make life easier, werkzeug
has ProfilerMiddleware
, which is written over cProfile
. In this recipe, we will use ProfilerMiddleware
to determine whether there is anything that affects performance.
Getting ready
We will use the application from the previous recipe and add ProfilerMiddleware
to a new file named generate_profile.py
.
How to do it…
Create a new file, generate_profile.py
, alongside run.py
, which works like run.py
itself but with ProfilerMiddleware
:
from werkzeug.middleware.profiler import ProfilerMiddleware from my_app import app app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions = [10]) app.run(debug=True)
Here...