1

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?

towi
  • 21,587
  • 28
  • 106
  • 187

2 Answers2

3

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.

  • That's exactly I want to try as soon as I can. Seems perfect. I will come back to this and let you of the results. – towi Apr 02 '11 at 10:37
2

You may want to check out this page describing one person's technique for identifying and resolving Trac performance problems. I borrow from the techniques explained there whenever my Trac instance starts to get sluggish.

The TracPerformance page on Trac's wiki has many more good pointers about improving performance.

There are also some useful hints and links posted to this serverfault question.

Community
  • 1
  • 1
bta
  • 43,959
  • 6
  • 69
  • 99