views:

61

answers:

2

I have an Apache2 + mod_python setup which has begun responding impossibly slow since some days - two seconds of processor time for any request of my app. Some interesting points:

  • Debugbar says it's ~15ms of query time. DB is not the main suspect
  • Logging with datetime.now() shows 0.1s is spent inside the view, and 40ms more are spent inside a requestcontext
  • I could not find an easy way to time template rendering.

Any idea of where can I look?

+1  A: 

Just profile the application. There is a full guide here

Arthur Debert
That thing is just warmed-over gprof. http://stackoverflow.com/questions/1777556/alternatives-to-gprof/1779343#1779343
Mike Dunlavey
A: 

Suppose it had an infinite loop. How would you find it?

I imagine you would just pause it in the debugger and look at the code at the various levels of the stack, because you know the loop is somewhere on the stack. Right?

Suppose the loop isn't infinite, merely taking a long time. Is that much different?

No matter what the problem is, if it is costing you some percent of time, like 90%, or 50%, or 20%, that's the probability you will catch it in the act when you pause it. So if you pause it several times, you're going to see it. The worse it is, the fewer times you have to pause it and look. It will be obvious.

So forget about timing. Just find out what it's doing.


In response to question, here is some mod_python doc:

5.4.1 PythonEnablePdb Syntax:
PythonEnablePdb {On, Off}
Default: PythonEnablePdb Off
Context: server config, virtual host, directory, htaccess Override: not None Module: mod python.c When On, mod python will execute the handler functions within the Python debugger pdb using the pdb.runcall() function. Because pdb is an interactive tool, start httpd from the command line with the -DONE PROCESS option when using this directive. As soon as your handler code is entered, you will see a Pdb prompt allowing you to step through the code and examine variables.

5.4.2 PythonDebug Syntax:
PythonDebug {On, Off} Default: PythonDebug Off Context: server config, virtual host, directory, htaccess Override: not None Module: mod python.c Normally, the traceback output resulting from uncaught Python errors is sent to the error log. With PythonDebug On directive specified, the output will be sent to the client (as well as the log), except when the error is IOError while writing, in which case it will go to the error log. This directive is very useful during the development process. It is recommended that you do not use it production environment as it may reveal to the client unintended, possibly sensitive security information.

Mike Dunlavey
If I had infinite (or too long) loops I'd take the time before and after, and log it. How would you suggest I pause the execution?
Agos
@Agos: I quoted some doc in the answer. You can run the Python debugger PDB interactively, and you can get a stack trace when you interrupt it. That's the basis of the technique. Don't think of it as logging time before / after. Think of it as "It's slowing way down. I wonder what it's doing."
Mike Dunlavey