Bird
Raised Fist0
Flaskframework~20 mins

Profiling Flask applications - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of profiling a Flask application?
easy
A. To find slow parts and improve performance
B. To add new routes to the app
C. To change the app's theme
D. To handle user authentication

Solution

  1. Step 1: Understand profiling concept

    Profiling is used to measure where the app spends time during execution.
  2. Step 2: Identify profiling goal in Flask

    In Flask, profiling helps find slow parts to optimize performance.
  3. Final Answer:

    To find slow parts and improve performance -> Option A
  4. Quick Check:

    Profiling = Find slow parts [OK]
Hint: Profiling = finding slow parts to speed up app [OK]
Common Mistakes:
  • Confusing profiling with adding features
  • Thinking profiling changes app design
  • Mixing profiling with security tasks
2. Which code snippet correctly adds profiling middleware to a Flask app using Werkzeug?
easy
A. app = ProfilerMiddleware(app, restrictions=[30])
B. from flask_profiler import Profiler app = Profiler(app)
C. from werkzeug.middleware.profiler import ProfilerMiddleware app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])
D. from werkzeug.middleware import Profiler app = Profiler(app)

Solution

  1. Step 1: Import ProfilerMiddleware correctly

    It is imported from werkzeug.middleware.profiler.
  2. Step 2: Wrap app.wsgi_app with ProfilerMiddleware

    Correct usage is app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30]).
  3. Final Answer:

    from werkzeug.middleware.profiler import ProfilerMiddleware app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30]) -> Option C
  4. Quick Check:

    Correct import and wrapping = from werkzeug.middleware.profiler import ProfilerMiddleware app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30]) [OK]
Hint: ProfilerMiddleware wraps app.wsgi_app, not app directly [OK]
Common Mistakes:
  • Importing from wrong module
  • Wrapping app instead of app.wsgi_app
  • Using wrong class names
3. Given this Flask app snippet with profiling enabled, what will be the effect when you run it?
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 home():
    return 'Hello Profiling!'

if __name__ == '__main__':
    app.run()
medium
A. The app runs but profiling is disabled silently
B. The app runs normally and prints profiling info for each request in console
C. The app crashes due to wrong middleware usage
D. The app returns profiling data as HTTP response

Solution

  1. Step 1: Analyze middleware wrapping

    ProfilerMiddleware wraps app.wsgi_app correctly with restrictions=[10].
  2. Step 2: Understand runtime behavior

    App runs normally and profiling info prints to console for each request.
  3. Final Answer:

    The app runs normally and prints profiling info for each request in console -> Option B
  4. Quick Check:

    Correct middleware = profiling info output [OK]
Hint: ProfilerMiddleware prints profile info to console during requests [OK]
Common Mistakes:
  • Expecting profiling data in HTTP response
  • Thinking app crashes with middleware
  • Assuming profiling is off without errors
4. You added ProfilerMiddleware but see no profiling output. What is the most likely mistake in this code?
from flask import Flask
from werkzeug.middleware.profiler import ProfilerMiddleware
app = Flask(__name__)
app = ProfilerMiddleware(app, restrictions=[10])

@app.route('/')
def index():
    return 'Test'
medium
A. Flask app must be run with debug=True for profiling
B. Missing import of ProfilerMiddleware
C. Restrictions list must be empty
D. ProfilerMiddleware should wrap app.wsgi_app, not app directly

Solution

  1. Step 1: Check how ProfilerMiddleware is applied

    It incorrectly wraps app instead of app.wsgi_app.
  2. Step 2: Correct usage for profiling

    ProfilerMiddleware must wrap app.wsgi_app to work properly.
  3. Final Answer:

    ProfilerMiddleware should wrap app.wsgi_app, not app directly -> Option D
  4. Quick Check:

    Wrap app.wsgi_app, not app [OK]
Hint: Always wrap app.wsgi_app with ProfilerMiddleware [OK]
Common Mistakes:
  • Wrapping app instead of app.wsgi_app
  • Forgetting to import ProfilerMiddleware
  • Misconfiguring restrictions parameter
5. You want to profile only the slowest 5 functions in your Flask app and save the results to files in a directory named 'profile'. Which code snippet correctly achieves this?
hard
A. from werkzeug.middleware.profiler import ProfilerMiddleware app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[5], profile_dir='./profile')
B. from werkzeug.middleware.profiler import ProfilerMiddleware app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[5], profile_dir='.')
C. from werkzeug.middleware.profiler import ProfilerMiddleware app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[5], profile_dir='profile.log')
D. from werkzeug.middleware.profiler import ProfilerMiddleware app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[5], profile_dir=None)

Solution

  1. Step 1: Understand restrictions parameter

    restrictions=[5] limits output to slowest 5 functions.
  2. Step 2: Set profile_dir correctly

    profile_dir must be a directory path, not a file name, to save profile files.
  3. Step 3: Choose correct directory path

    './profile' is a directory path suitable for saving logs.
  4. Final Answer:

    from werkzeug.middleware.profiler import ProfilerMiddleware app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[5], profile_dir='./profile') -> Option A
  5. Quick Check:

    restrictions=5 + directory path = from werkzeug.middleware.profiler import ProfilerMiddleware app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[5], profile_dir='./profile') [OK]
Hint: profile_dir must be a folder, restrictions limits slowest funcs [OK]
Common Mistakes:
  • Using file name instead of directory for profile_dir
  • Setting profile_dir to None disables saving
  • Misunderstanding restrictions parameter
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard