# HG changeset patch # User Christophe de Vienne # Date 1420300292 -3600 # Node ID dd875009cc4785d582dc12882b6284bb0996c8e7 # Parent ceb1a5baca4fe54a0ffe9adf5c8c5180bdaea9cb [profile] Add a profiling tool diff -r ceb1a5baca4f -r dd875009cc47 pyramid_cubicweb/__init__.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 diff -r ceb1a5baca4f -r dd875009cc47 pyramid_cubicweb/profile.py --- /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