Using profiling to find bottlenecks
Profiling is an important tool when we decide to scale the application. Before scaling, we want to know whether any process is a bottleneck and affects the overall performance. Python has an inbuilt profiler, cProfile
, that can do the job for us, but to make life easier, Werkzeug has a ProfilerMiddleware
of its own, which is written over cProfile. We will use this to determine whether there is anything that affects the performance.
Getting ready
We will use the application from the previous recipe and add ProfilerMiddleware
in 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.contrib.profiler import ProfilerMiddleware from my_app import app app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions = [10]) app.run(debug=True)
Here, we imported ProfilerMiddleware
from werkzeug
and then modified wsgi_app
on our Flask...