1

I am trying to profile our django unittests (if the tests are faster, we'll run 'em more often). I've ran it through python's built in cProfile profiler, producing a pstats file.

However the signal to noise ration is bad. There are too many functions listed. Lots and lots of django internal functions are called when I make one database query. This makes it hard to see what's going on.

Is there anyway I can "roll up" all function calls that are outside a certain directory?

e.g. if I call a python function outside my directory, and it then calls 5 other functions (all outside my directory), then it should roll all those up, so it looks like there was only one function call, and it should show the cumulative time for the whole thing.

This, obviously, is bad if you want to profile (say) Django, but I don't want to do that.

I looked at the pstats.Stats object, but can't see an obvious way to modify this data.

Amandasaurus
  • 58,203
  • 71
  • 188
  • 248

1 Answers1

1

I have little experience with python, but a lot in performance tuning, so here's a possibility:

  1. Rather than run profiling as part of the unit tests, just do overall execution time measurements. If those don't change much, you're OK.

  2. When you do detect a change, or if you just want to make things run faster, use this method. It has a much higher "signal-to-noise ratio", as you put it.

The difference is you're not hoping the profiler can figure out what you need to be looking at. It's more like a break point in a debugger, where the break occurs not at a place of your choosing, but with good probability at a time of unnecessary slowness. If on two or more occasions you see it doing something that might be replaced with something better, on average it will pay off, and fixing it will make other problems easier to find by the same method.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135
  • Our unittests have been slowly added to and grown as our code base has grown, which is a good thing. We're at the stage where the ~ 2 hours it takes to retest is annoying. It would be nice if it was faster. I'm hoping there are some low hanging fruit, and the best way to find out is to profile it. There's bound to be some helper functions that are using time, that I want to find out. – Amandasaurus Nov 04 '12 at 14:33
  • @Rory: "hoping there are some low hanging fruit, and the best way to find out is to profile it". Let me put that another way: "The kinds of optimization opportunities that typical programs have are effectively found with profilers." You're not alone in thinking that, but it's *not so*. It has no justification, formal or practical. Bearing that out, SO is full of questions from people trying to make sense of profiler output. [*Here's more on all that.*](http://stackoverflow.com/a/1779343/23771) – Mike Dunlavey Nov 04 '12 at 15:40