I would like to enable a side-logging in our real-world Trac/mod_python installation that has gotten quite slow (lots of plugins, not so many tickets/pages).
Can I proxy the request object, or add a python trace (with timestamps for each call) somehow? Is there e mechanism for these kind of wrappers?
The main entry point in Trac/mod_python is
def handler(req):
pkg_resources.require('Trac==%s' % VERSION)
gateway = ModPythonGateway(req, req.get_options())
from trac.web.main import dispatch_request
gateway.run(dispatch_request)
return apache.OK
and there I guess I should install a wrapper, that can trace python calls through all plugins for timing analysis. Possible?
Using the cProfile
module, something like this should work:
def handler(req):
pkg_resources.require('Trac==%s' % VERSION)
gateway = ModPythonGateway(req, req.get_options())
from trac.web.main import dispatch_request
import cProfile
from datetime import datetime
def profile_request(*args, **kwargs):
p = cProfile.Profile()
p.runcall(dispatch_request, *args, **kwargs)
# log to a file
timestamp = datetime.now().strftime('%Y-%m-%d_%H%M%S.%f')
p.dump_stats('/var/log/trac_profile/%s' % timestamp)
gateway.run(profile_request)
return apache.OK
Then each request should be profiled and the profiling data (showing what takes up time) saved in the specified location, one timestamped file per request. The running process, of course, must have write privileges to the file.
You can change the file in the source.