0
0
Flaskframework~20 mins

Profiling Flask applications - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flask Profiling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask profiling snippet?
Consider this Flask app snippet using the built-in Werkzeug profiler middleware. What will be the visible effect when accessing the '/' route in a browser?
Flask
from flask import Flask
from werkzeug.middleware.profiler import ProfilerMiddleware

app = Flask(__name__)
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])

@app.route('/')
def index():
    return 'Hello, profiling!'

if __name__ == '__main__':
    app.run(debug=True)
AThe app crashes with ImportError because ProfilerMiddleware is not found.
BThe browser shows a JSON with profiling data instead of 'Hello, profiling!'.
CThe browser shows 'Hello, profiling!' and the console prints detailed profiling stats for the request.
DThe browser shows 'Hello, profiling!' but no profiling data is printed anywhere.
Attempts:
2 left
💡 Hint
ProfilerMiddleware wraps the app and prints profiling info to the console, not the browser.
📝 Syntax
intermediate
2:00remaining
Which option correctly enables Flask profiling with cProfile?
You want to profile your Flask app using Python's cProfile module. Which code snippet correctly runs the app with profiling enabled?
A
from flask import Flask
import cProfile
app = Flask(__name__)

if __name__ == '__main__':
    app.run(debug=True)
cProfile.run()
B
from flask import Flask
app = Flask(__name__)

if __name__ == '__main__':
    app.run(debug=True, profiler=True)
C
import cProfile
from flask import Flask
app = Flask(__name__)

if __name__ == '__main__':
    app.run(debug=True)
cProfile.run('app')
D
import cProfile
from flask import Flask
app = Flask(__name__)

if __name__ == '__main__':
    cProfile.run('app.run(debug=True)')
Attempts:
2 left
💡 Hint
cProfile.run() takes a string with the code to run.
🔧 Debug
advanced
2:00remaining
Why does this Flask profiling setup not show any profiling output?
You added this code to profile your Flask app but see no profiling output in the console. What is the likely cause?
Flask
from flask import Flask
from werkzeug.middleware.profiler import ProfilerMiddleware

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello!'

if __name__ == '__main__':
    app.wsgi_app = ProfilerMiddleware(app.wsgi_app)
    app.run()
AProfilerMiddleware is added after app.run(), so it never wraps the running app.
BProfilerMiddleware requires debug=True to print output.
CThe route '/' must be decorated with @app.profiler to enable profiling.
DProfilerMiddleware only works with Flask versions below 1.0.
Attempts:
2 left
💡 Hint
Check the order of statements in the main block.
state_output
advanced
2:00remaining
What is the effect of adding restrictions=[10] to ProfilerMiddleware?
Given this Flask app snippet, what will be the effect of the restrictions parameter?
Flask
from flask import Flask
from werkzeug.middleware.profiler import ProfilerMiddleware

app = Flask(__name__)
app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[10])

@app.route('/')
def index():
    return 'Restricted profiling'

if __name__ == '__main__':
    app.run()
AOnly the top 10 functions by cumulative time will be shown in the profiling output.
BProfiling will only run for 10 seconds after the app starts.
CThe app will limit requests to 10 per minute for profiling.
DProfiling output will be saved to a file named 'restrictions_10.prof'.
Attempts:
2 left
💡 Hint
restrictions controls how many lines of profiling stats are printed.
🧠 Conceptual
expert
3:00remaining
Which profiling approach best isolates Flask route performance without external noise?
You want to profile only the execution time of a specific Flask route handler function, excluding middleware and server overhead. Which approach is best?
AUse cProfile to run the entire Flask app and filter the output manually for the route function.
BWrap the route function with a custom decorator that uses time.perf_counter() to measure execution time.
CUse ProfilerMiddleware on the whole app and analyze the full request profile.
DRun Flask with debug=True and rely on Flask's built-in profiler output.
Attempts:
2 left
💡 Hint
Think about isolating only the route function's execution time.