diff --git a/server/djrandom/frontend/frontend.py b/server/djrandom/frontend/frontend.py
index c0ed117cb9330a5c696a9af812b41df488fc06c4..8eca9b368081cb42e99f270dd04f83757f59a09b 100644
--- a/server/djrandom/frontend/frontend.py
+++ b/server/djrandom/frontend/frontend.py
@@ -19,7 +19,7 @@ log = logging.getLogger(__name__)
 
 
 def run_frontend(port, solr_url, db_url, lastfm_api_key, album_art_dir,
-                 email_sender, markov_data_file):
+                 email_sender, markov_data_file, do_profile):
     init_db(db_url)
 
     svcs['searcher'] = Searcher(solr_url)
@@ -32,6 +32,18 @@ def run_frontend(port, solr_url, db_url, lastfm_api_key, album_art_dir,
         log.error('Could not read Markov data from %s: %s' % (
                 markov_data_file, str(e)))
 
+    # Start the WSGI profiling middleware, if requested.
+    if do_profile:
+        from repoze.profile.profiler import AccumulatingProfileMiddleware
+        app.wsgi_app = AccumulatingProfileMiddleware(
+            app.wsgi_app,
+            log_filename='/var/tmp/djrandom-profile.log',
+            cachegrind_filename='/var/tmp/djrandom-profile.cachegrind',
+            discard_first_request=True,
+            flush_at_shutdown=True,
+            path='/__profile__'
+            )
+
     http_server = WSGIServer(('0.0.0.0', port), app)
     http_server.serve_forever()
 
@@ -46,6 +58,7 @@ def main():
     parser.add_option('--album_art_dir', default='/var/tmp/album-image-cache')
     parser.add_option('--markov_data',
                       default='/var/lib/djrandom/djrandom-markov.dat')
+    parser.add_option('--profile', action='store_true')
     daemonize.add_standard_options(parser)
     utils.read_config_defaults(
         parser, os.getenv('DJRANDOM_CONF', '/etc/djrandom.conf'))
@@ -58,7 +71,7 @@ def main():
     daemonize.daemonize(opts, run_frontend,
                         (opts.port, opts.solr_url, opts.db_url,
                          opts.lastfm_api_key, opts.album_art_dir,
-                         opts.email_sender, opts.markov_data),
+                         opts.email_sender, opts.markov_data, opts.profile),
                         support_gevent=True)