[profile] Add a profiling tool
authorChristophe de Vienne <christophe@unlish.com>
Sat, 03 Jan 2015 16:51:32 +0100
changeset 11535 dd875009cc47
parent 11534 ceb1a5baca4f
child 11536 6618408c0629
[profile] Add a profiling tool
pyramid_cubicweb/__init__.py
pyramid_cubicweb/profile.py
--- a/pyramid_cubicweb/__init__.py	Sat Jan 03 02:36:06 2015 +0100
+++ b/pyramid_cubicweb/__init__.py	Sat Jan 03 16:51:32 2015 +0100
@@ -58,8 +58,16 @@
     return config
 
 
-def wsgi_application_from_cwconfig(cwconfig):
+def wsgi_application_from_cwconfig(
+        cwconfig,
+        profile=False, profile_output=None, profile_dump_every=None):
     config = make_cubicweb_application(cwconfig)
+    profile = profile or asbool(config.registry.settings.get(
+        'cubicweb.profile.enable', False))
+    if profile:
+        config.add_route('profile_ping', '_profile/ping')
+        config.add_route('profile_cnx', '_profile/cnx')
+        config.scan('pyramid_cubicweb.profile')
     app = config.make_wsgi_app()
     # This replaces completely web/cors.py, which is not used by
     # pyramid_cubicweb anymore
@@ -69,6 +77,14 @@
         headers=cwconfig['access-control-allow-headers'],
         methods=cwconfig['access-control-allow-methods'],
         credentials='true')
+
+    if profile:
+        from pyramid_cubicweb.profile import wsgi_profile
+        filename = profile_output or config.registry.settings.get(
+            'cubicweb.profile.output', 'program.prof')
+        dump_every = profile_dump_every or config.registry.settings.get(
+            'cubicweb.profile.dump_every', 100)
+        app = wsgi_profile(app, filename=filename, dump_every=dump_every)
     return app
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyramid_cubicweb/profile.py	Sat Jan 03 16:51:32 2015 +0100
@@ -0,0 +1,35 @@
+import cProfile
+import itertools
+from pyramid.view import view_config
+
+
+@view_config(route_name='profile_ping')
+def ping(request):
+    request.response.text = u'pong'
+    return request.response
+
+
+@view_config(route_name='profile_cnx')
+def cnx(request):
+    request.cw_cnx
+    request.response.text = u'pong'
+    return request.response
+
+
+def wsgi_profile(app, filename='program.prof', dump_every=50):
+    profile = cProfile.Profile()
+
+    counter = itertools.count(1)
+
+    def application(environ, start_response):
+        profile.enable()
+        try:
+            return app(environ, start_response)
+        finally:
+            profile.disable()
+            if not counter.next() % dump_every:
+                print "Dump profile stats to %s" % filename
+                profile.create_stats()
+                profile.dump_stats(filename)
+
+    return application