0

Possible Duplicate:
How can you profile a Python script?

I'm developing an API with several packages collaborating together via various gui's and I'm slowly thinking about how I would be able to analyze the amount of calls the packages and modules and methods and classes are getting from the user under a normal run. Thinking I might be able to detect bottlenecks and find out where most of the work is required, while also seeing where I might be able to merge similar functionality.

I'm picturing it like a simple bar chart

enter image description here

Where each bar represents one method, module or package depending on which "depth" you're interested to look at, and the height axis is the amount of calls it has gotten.

Is there anything similar currently and/or how would you implement this in Python?

I'm thinking of putting a simple logger at the entrance of each method, under say Debug, and then filter those out when I'm making the diagram. But that means my code would get messier and I'd have to remember to do it for all of them. What would be easier is if I could apply a compile-time decorator when compiling it with say a certain flag. Is that possible with Python?

Thanks

Community
  • 1
  • 1
Marcus Ottosson
  • 3,241
  • 4
  • 28
  • 34

2 Answers2

1

I think the trace module from the stdlib would be helpful.

djc
  • 11,603
  • 5
  • 41
  • 54
0

There's decorator mentioned in the PythonDecoratorLibrary for profiling/tracing a single function. It would give you a running start in implementing what you describe.

You don't really compile things in Python, it's done on-the-fly, but you can vary the decorator definition based on a runtime flag to turn it on and off.

martineau
  • 119,623
  • 25
  • 170
  • 301