6

I've never done code coverage in Python, but I'm looking for something like GCC's gcov, which tells me how many times each line executes, or Apple's Shark which gives a hierarchial breakdown of how long each function is taking.

My problem is that I have a live server which is experiencing high load, and I can't tell from the logs what's causing it. I would like to attach something to my Django instance to monitor which lines are the hottest and/or which functions are taking the longest time.

This is something like, but not exactly, code coverage. I would like to introduce it to a live running server, preferably without modifying too much.

Ideas?

Joe
  • 46,419
  • 33
  • 155
  • 245

3 Answers3

3

Django-live-profiler is a drop-in Django app that lets you profile your running application using statprof and visualize the results.

atereshkin
  • 696
  • 5
  • 8
2

cProfile + RunSnakeRun: http://www.vrplumber.com/programming/runsnakerun/

catavaran
  • 44,703
  • 8
  • 98
  • 85
  • 2
    How do I attach cProfile to a running Django process? – Joe Nov 21 '11 at 14:43
  • You can't attach profiler to running process. You need to start django process from `cProfile.py`: http://docs.python.org/library/profile.html#instant-user-s-manual – catavaran Nov 21 '11 at 14:58
  • Yep, the answer was to fiddle with my launch scripts and put this in. Not very intrusive. Thanks! – Joe Nov 23 '11 at 09:26
1

I use this pattern: A monitor threads writes stacktraces to a log file every 0.3 seconds. After some hours I can see where the interpreter spends the most time. It does not slow down the my server:

Live Profiling of Python Server

Community
  • 1
  • 1
guettli
  • 25,042
  • 81
  • 346
  • 663